From f4341934edd9815f28d9b8ed5f85102c459f0f98 Mon Sep 17 00:00:00 2001 From: Aldwin Vlasblom Date: Thu, 8 Jun 2017 11:40:02 +0200 Subject: [PATCH] Make Future#both cancel the other when the one rejects --- src/core.js | 34 ++-------------------------------- test/5.both.test.js | 10 ++++++++++ 2 files changed, 12 insertions(+), 32 deletions(-) diff --git a/src/core.js b/src/core.js index fce2b5bf..c3144563 100644 --- a/src/core.js +++ b/src/core.js @@ -358,37 +358,6 @@ Never.prototype.toString = function Never$toString(){ export const never = new Never(); export const isNever = x => x === never; -function Eager(future){ - this.rej = noop; - this.res = noop; - this.rejected = false; - this.resolved = false; - this.value = null; - this.cancel = future._fork(x => { - this.value = x; - this.rejected = true; - this.cancel = noop; - this.rej(x); - }, x => { - this.value = x; - this.resolved = true; - this.cancel = noop; - this.res(x); - }); -} - -Eager.prototype = Object.create(Core); - -Eager.prototype._fork = function Eager$_fork(rej, res){ - if(this.rejected) rej(this.value); - else if(this.resolved) res(this.value); - else{ - this.rej = rej; - this.res = res; - } - return this.cancel; -}; - export class Action{ rejected(x){ return new Rejected(x) } resolved(x){ return new Resolved(x) } @@ -492,9 +461,10 @@ export class BothAction extends Action{ } export class BothActionState extends BothAction{ constructor(early, other){ - super(new Eager(other)); + super(other); this.cancel = this.other.fork(x => early(new Rejected(x), this), noop); } + rejected(x){ this.cancel(); return new Rejected(x) } } export function Sequence(spawn, actions = []){ diff --git a/test/5.both.test.js b/test/5.both.test.js index 51e43705..be61de4a 100644 --- a/test/5.both.test.js +++ b/test/5.both.test.js @@ -60,6 +60,16 @@ const testInstance = both => { }); + it('cancels the right if the left rejects', done => { + const m = both(F.rejectedSlow, Future(() => () => done())); + m.fork(U.noop, U.noop); + }); + + it('cancels the left if the right rejects', done => { + const m = both(Future(() => () => done()), F.rejectedSlow); + m.fork(U.noop, U.noop); + }); + it('creates a cancel function which cancels both Futures', done => { let cancelled = false; const m = Future(() => () => (cancelled ? done() : (cancelled = true)));