mirror of
https://github.com/trezor/trezor-suite.git
synced 2026-02-26 11:42:16 +01:00
chore(utils): remove unused abortable promise
This commit is contained in:
@@ -1,84 +0,0 @@
|
||||
/* eslint-disable max-classes-per-file, no-underscore-dangle */
|
||||
// from https://github.com/zzdjk6/simple-abortable-promise
|
||||
|
||||
// When abort happens, this error will be throw
|
||||
export class AbortError extends Error {
|
||||
constructor(message = 'Aborted') {
|
||||
super(message);
|
||||
this.name = 'AbortError';
|
||||
}
|
||||
}
|
||||
|
||||
// General interface of Abortable
|
||||
export interface Abortable {
|
||||
abort: (reason?: string) => void;
|
||||
readonly abortReason?: string;
|
||||
}
|
||||
|
||||
// The executor function should be passed to the constructor when create a Promise
|
||||
interface ExecutorFunction<T> {
|
||||
(resolve: (value?: PromiseLike<T> | T) => void, reject: (reason?: any) => void): void;
|
||||
}
|
||||
|
||||
// The executor function should be passed to the constructor when create a AbortablePromise
|
||||
interface AbortableExecutorFunction<T> {
|
||||
(
|
||||
resolve: (value?: PromiseLike<T> | T) => void,
|
||||
reject: (reason?: any) => void,
|
||||
abortController: AbortSignal,
|
||||
): void;
|
||||
}
|
||||
|
||||
// AbortablePromise is a subclass of Promise that implements Abortable interface
|
||||
export class AbortablePromise<T> extends Promise<T> implements Abortable {
|
||||
// Method definition `.abort()`
|
||||
public abort: Abortable['abort'];
|
||||
|
||||
// Getter to access abort reason
|
||||
public get abortReason(): string | undefined {
|
||||
return this._abortReason;
|
||||
}
|
||||
|
||||
// Internal store of abort reason
|
||||
private _abortReason?: string;
|
||||
|
||||
// Constructor, note we can provide 3 args: resolve, reject, abortSignal
|
||||
constructor(
|
||||
executor: AbortableExecutorFunction<T>,
|
||||
// optionally custom abortController instance
|
||||
abortControllerParam?: AbortController,
|
||||
) {
|
||||
const abortController = abortControllerParam || new AbortController();
|
||||
const abortSignal = abortController.signal;
|
||||
|
||||
// This is the executor function to be passed to the superclass - Promise
|
||||
const normalExecutor: ExecutorFunction<T> = (resolve, reject) => {
|
||||
abortSignal.addEventListener('abort', () => {
|
||||
reject(new AbortError(this.abortReason));
|
||||
});
|
||||
|
||||
executor(resolve, reject, abortSignal);
|
||||
};
|
||||
// @ts-expect-error ?
|
||||
super(normalExecutor);
|
||||
|
||||
// Bind the abort method
|
||||
this.abort = reason => {
|
||||
this._abortReason = reason || 'Aborted';
|
||||
abortController.abort();
|
||||
};
|
||||
}
|
||||
|
||||
// Wrap other Promise instances to AbortablePromise
|
||||
// eslint-disable-next-line @typescript-eslint/no-shadow
|
||||
static from = <T>(promise: Promise<T>): AbortablePromise<T> => {
|
||||
// If promise is already an AbortablePromise, return it directly
|
||||
if (promise instanceof AbortablePromise) {
|
||||
return promise;
|
||||
}
|
||||
|
||||
return new AbortablePromise<T>((resolve, reject) => {
|
||||
promise.then(resolve).catch(reject);
|
||||
});
|
||||
};
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
export * from './abortablePromise';
|
||||
export * from './arrayDistinct';
|
||||
export * from './arrayPartition';
|
||||
export * from './arrayShuffle';
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
import { AbortablePromise, AbortError } from '../src/abortablePromise';
|
||||
|
||||
describe('AbortablePromise', () => {
|
||||
test('resolves', async () => {
|
||||
const a = new AbortablePromise(resolve => {
|
||||
setTimeout(() => {
|
||||
resolve('done');
|
||||
}, 1);
|
||||
});
|
||||
|
||||
await expect(a).resolves.toEqual('done');
|
||||
});
|
||||
|
||||
test('rejects', async () => {
|
||||
const a = new AbortablePromise((_resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
reject('failed');
|
||||
}, 1);
|
||||
});
|
||||
|
||||
await expect(a).rejects.toEqual('failed');
|
||||
});
|
||||
|
||||
test('abort from outside', () => {
|
||||
const a = new AbortablePromise(resolve => {
|
||||
setTimeout(() => {
|
||||
resolve('failed');
|
||||
}, 1);
|
||||
});
|
||||
a.abort('custom err');
|
||||
|
||||
expect(a).rejects.toBeInstanceOf(AbortError);
|
||||
});
|
||||
|
||||
test('abort listener aborts another promise', () => {
|
||||
const b = new AbortablePromise(resolve => {
|
||||
setTimeout(() => {
|
||||
resolve('success');
|
||||
});
|
||||
});
|
||||
|
||||
const a = new AbortablePromise((resolve, _reject, abortsignal) => {
|
||||
setTimeout(() => {
|
||||
resolve('failed');
|
||||
}, 1);
|
||||
abortsignal.addEventListener('abort', () => {
|
||||
b.abort();
|
||||
});
|
||||
});
|
||||
|
||||
a.abort();
|
||||
|
||||
expect(a).rejects.toBeInstanceOf(AbortError);
|
||||
expect(b).rejects.toBeInstanceOf(AbortError);
|
||||
});
|
||||
|
||||
test('abort from parent controller', () => {
|
||||
const controller = new AbortController();
|
||||
|
||||
const a = new AbortablePromise(resolve => {
|
||||
setTimeout(() => {
|
||||
resolve('mew');
|
||||
}, 1);
|
||||
}, controller);
|
||||
|
||||
controller.abort();
|
||||
expect(a).rejects.toBeInstanceOf(AbortError);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user