Skip to content

Commit

Permalink
Feature/btc tests (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
npwork authored Jan 27, 2022
1 parent 668b94d commit 91c3c35
Show file tree
Hide file tree
Showing 13 changed files with 560 additions and 332 deletions.
94 changes: 1 addition & 93 deletions packages/api-client/src/generated/core/CancelablePromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,96 +19,4 @@ export interface OnCancel {
(cancelHandler: () => void): void
}

//export type CancelablePromise<T> = Promise<T>
export class CancelablePromise<T> implements Promise<T> {
readonly [Symbol.toStringTag]: string

#isPending: boolean
#isCancelled: boolean
readonly #cancelHandlers: (() => void)[]
readonly #promise: Promise<T>
#resolve?: (value: T | PromiseLike<T>) => void
#reject?: (reason?: any) => void

constructor(
executor: (
resolve: (value: T | PromiseLike<T>) => void,
reject: (reason?: any) => void,
onCancel: OnCancel,
) => void,
) {
this.#isPending = true
this.#isCancelled = false
this.#cancelHandlers = []
this.#promise = new Promise<T>((resolve, reject) => {
this.#resolve = resolve
this.#reject = reject

const onResolve = (value: T | PromiseLike<T>): void => {
if (!this.#isCancelled) {
this.#isPending = false
this.#resolve?.(value)
}
}

const onReject = (reason?: any): void => {
this.#isPending = false
this.#reject?.(reason)
}

const onCancel = (cancelHandler: () => void): void => {
if (this.#isPending) {
this.#cancelHandlers.push(cancelHandler)
}
}

Object.defineProperty(onCancel, 'isPending', {
get: (): boolean => this.#isPending,
})

Object.defineProperty(onCancel, 'isCancelled', {
get: (): boolean => this.#isCancelled,
})

return executor(onResolve, onReject, onCancel as OnCancel)
})
}

public then<TResult1 = T, TResult2 = never>(
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null,
): Promise<TResult1 | TResult2> {
return this.#promise.then(onFulfilled, onRejected)
}

public catch<TResult = never>(
onRejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null,
): Promise<T | TResult> {
return this.#promise.catch(onRejected)
}

public finally(onFinally?: (() => void) | null): Promise<T> {
return this.#promise.finally(onFinally)
}

public cancel(): void {
if (!this.#isPending || this.#isCancelled) {
return
}
this.#isCancelled = true
if (this.#cancelHandlers.length) {
try {
for (const cancelHandler of this.#cancelHandlers) {
cancelHandler()
}
} catch (error) {
this.#reject?.(error)
return
}
}
}

public get isCancelled(): boolean {
return this.#isCancelled
}
}
export type CancelablePromise<T> = Promise<T>
Loading

0 comments on commit 91c3c35

Please sign in to comment.