refactor: remove duplicated delayed promise
This commit is contained in:
4 files changed
+3
-93
No files matched your search
@@ -76,48 +76,3 @@ export const transformChunk = () => {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export class DelayedPromise<T> {
|
||||
get promise(): Promise<T> {
|
||||
if (this._promise == null) {
|
||||
this._promise = new Promise<T>((resolve, reject) => {
|
||||
if (this.status.type === 'resolved') {
|
||||
resolve(this.status.value)
|
||||
}
|
||||
else if (this.status.type === 'rejected') {
|
||||
reject(this.status.error)
|
||||
}
|
||||
|
||||
this._resolve = resolve
|
||||
this._reject = reject
|
||||
})
|
||||
}
|
||||
|
||||
return this._promise
|
||||
}
|
||||
|
||||
private _promise: Promise<T> | undefined
|
||||
private _reject: ((error: unknown) => void) | undefined
|
||||
private _resolve: ((value: T) => void) | undefined
|
||||
|
||||
private status:
|
||||
| { error: unknown, type: 'rejected' }
|
||||
| { type: 'pending' }
|
||||
| { type: 'resolved', value: T } = { type: 'pending' }
|
||||
|
||||
reject(error: unknown): void {
|
||||
this.status = { error, type: 'rejected' }
|
||||
|
||||
if (this._promise) {
|
||||
this._reject?.(error)
|
||||
}
|
||||
}
|
||||
|
||||
resolve(value: T): void {
|
||||
this.status = { type: 'resolved', value }
|
||||
|
||||
if (this._promise) {
|
||||
this._resolve?.(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { CompletionStep, CompletionToolCall, CompletionToolResult, FinishReason, Message, StreamTextEvent, StreamTextOptions, StreamTextResult, ToolCall, Usage, WithUnknown } from 'xsai'
|
||||
|
||||
import { chat, determineStepType, executeTool, objCamelToSnake, trampoline } from 'xsai'
|
||||
import { chat, DelayedPromise, determineStepType, executeTool, objCamelToSnake, trampoline } from 'xsai'
|
||||
|
||||
import type { WithTelemetry } from '../types/options'
|
||||
|
||||
@@ -8,7 +8,7 @@ import { commonAttributes, idAttributes, metadataAttributes } from './attributes
|
||||
import { getTracer } from './get-tracer'
|
||||
import { now } from './now'
|
||||
import { recordSpan, recordSpanSync } from './record-span'
|
||||
import { DelayedPromise, transformChunk } from './stream-text-internal'
|
||||
import { transformChunk } from './stream-text-internal'
|
||||
import { stringifyTool } from './stringify-tool'
|
||||
import { wrapTool } from './wrap-tool'
|
||||
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import type { WithUnknown } from '@xsai/shared'
|
||||
import type { ChatOptions, CompletionStep, CompletionToolCall, CompletionToolResult, FinishReason, Message, ToolCall, Usage } from '@xsai/shared-chat'
|
||||
|
||||
import { objCamelToSnake, trampoline } from '@xsai/shared'
|
||||
import { DelayedPromise, objCamelToSnake, trampoline } from '@xsai/shared'
|
||||
import { chat, determineStepType, executeTool } from '@xsai/shared-chat'
|
||||
|
||||
import type { StreamTextEvent } from './types/event'
|
||||
|
||||
import { DelayedPromise } from './internal/_delayed-promise'
|
||||
import { transformChunk } from './internal/_transform-chunk'
|
||||
|
||||
export type * from './types/event'
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
export class DelayedPromise<T> {
|
||||
get promise(): Promise<T> {
|
||||
if (this._promise == null) {
|
||||
this._promise = new Promise<T>((resolve, reject) => {
|
||||
if (this.status.type === 'resolved') {
|
||||
resolve(this.status.value)
|
||||
}
|
||||
else if (this.status.type === 'rejected') {
|
||||
reject(this.status.error)
|
||||
}
|
||||
|
||||
this._resolve = resolve
|
||||
this._reject = reject
|
||||
})
|
||||
}
|
||||
|
||||
return this._promise
|
||||
}
|
||||
|
||||
private _promise: Promise<T> | undefined
|
||||
private _reject: ((error: unknown) => void) | undefined
|
||||
private _resolve: ((value: T) => void) | undefined
|
||||
|
||||
private status:
|
||||
| { error: unknown, type: 'rejected' }
|
||||
| { type: 'pending' }
|
||||
| { type: 'resolved', value: T } = { type: 'pending' }
|
||||
|
||||
reject(error: unknown): void {
|
||||
this.status = { error, type: 'rejected' }
|
||||
|
||||
if (this._promise) {
|
||||
this._reject?.(error)
|
||||
}
|
||||
}
|
||||
|
||||
resolve(value: T): void {
|
||||
this.status = { type: 'resolved', value }
|
||||
|
||||
if (this._promise) {
|
||||
this._resolve?.(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user