Skip to content

Commit

Permalink
fix(cancellation): make cancellation per-fork
Browse files Browse the repository at this point in the history
  • Loading branch information
tdreyno committed Feb 25, 2020
1 parent 59e1242 commit f3bee51
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/Task/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,31 @@ export class Task<E, S> implements PromiseLike<S> {
public isCanceled = false;
constructor(private computation: Fork<E, S>) {}

fork(reject: Reject<E>, resolve: Resolve<S>) {
if (this.isCanceled) {
return this;
fork(reject: Reject<E>, resolve: Resolve<S>): { cancel: () => void } {
let localCancel = this.isCanceled;

const result = {
cancel: () => (localCancel = true)
};

if (localCancel) {
return result;
}

this.computation(
err => {
if (!this.isCanceled) {
if (!localCancel) {
reject(err);
}
},
value => {
if (!this.isCanceled) {
if (!localCancel) {
resolve(value);
}
}
);

return this;
return result;
}

cancel() {
Expand Down Expand Up @@ -313,7 +319,7 @@ export function fork<E, S>(
reject: Reject<E>,
resolve: Resolve<S>,
task: Task<E, S>
): Task<E, S> {
): { cancel: () => void } {
return task.fork(reject, resolve);
}

Expand Down

0 comments on commit f3bee51

Please sign in to comment.