Skip to content

Commit

Permalink
fix(task): dispose of event listeners on all resolution branches
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcourtice committed Mar 2, 2022
1 parent 776170a commit f293123
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions packages/task/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,22 @@ export default class Task<TResult = void> extends Promise<TResult> {
let isAborting = false;
let finaliser: Product | undefined;

const execResolution = (callback: Product) => {
if (isAborting) {
finaliser = finaliser || callback;
} else {
callback();
}
};

super((_resolve, _reject) => {
const defaultFinaliser: TaskAbortCallback = (reason) => _reject(new TaskAbortError(reason));
const resolve = (value: TResult | PromiseLike<TResult>) => execResolution(() => _resolve(value));
const reject = (reason?: any) => execResolution(() => _reject(reason));
const onAbort = (callback: TaskAbortCallback) => listeners.add(callback);
const dispose = () => controller.signal.removeEventListener('abort', abort);

const execResolution = (resolution: Product) => {
if (isAborting) {
finaliser = finaliser || resolution;
} else {
dispose();
resolution();
}
};

const abort = () => {
controller.signal.removeEventListener('abort', abort);

isAborting = true;

listeners.forEach(listener => {
Expand All @@ -55,7 +54,10 @@ export default class Task<TResult = void> extends Promise<TResult> {

isAborting = false;

(finaliser || defaultFinaliser)(this.abortReason);
const finalise = finaliser || (reason => _reject(new TaskAbortError(reason)));

dispose();
finalise(this.abortReason);
};

controller.signal.addEventListener('abort', abort);
Expand Down

0 comments on commit f293123

Please sign in to comment.