Skip to content

Commit f3bee51

Browse files
committed
fix(cancellation): make cancellation per-fork
1 parent 59e1242 commit f3bee51

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/Task/Task.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,31 @@ export class Task<E, S> implements PromiseLike<S> {
3939
public isCanceled = false;
4040
constructor(private computation: Fork<E, S>) {}
4141

42-
fork(reject: Reject<E>, resolve: Resolve<S>) {
43-
if (this.isCanceled) {
44-
return this;
42+
fork(reject: Reject<E>, resolve: Resolve<S>): { cancel: () => void } {
43+
let localCancel = this.isCanceled;
44+
45+
const result = {
46+
cancel: () => (localCancel = true)
47+
};
48+
49+
if (localCancel) {
50+
return result;
4551
}
4652

4753
this.computation(
4854
err => {
49-
if (!this.isCanceled) {
55+
if (!localCancel) {
5056
reject(err);
5157
}
5258
},
5359
value => {
54-
if (!this.isCanceled) {
60+
if (!localCancel) {
5561
resolve(value);
5662
}
5763
}
5864
);
5965

60-
return this;
66+
return result;
6167
}
6268

6369
cancel() {
@@ -313,7 +319,7 @@ export function fork<E, S>(
313319
reject: Reject<E>,
314320
resolve: Resolve<S>,
315321
task: Task<E, S>
316-
): Task<E, S> {
322+
): { cancel: () => void } {
317323
return task.fork(reject, resolve);
318324
}
319325

0 commit comments

Comments
 (0)