chore(utils): remove unused abortable promise

This commit is contained in:
Martin Varmuza
2023-06-14 10:00:45 +02:00
committed by martin
parent 3e6f9d3c67
commit f5e57314f0
3 changed files with 0 additions and 154 deletions

View File

@@ -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);
});
};
}

View File

@@ -1,4 +1,3 @@
export * from './abortablePromise';
export * from './arrayDistinct';
export * from './arrayPartition';
export * from './arrayShuffle';

View File

@@ -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);
});
});