Skip to content

Commit

Permalink
Make Future#both cancel the other when the one rejects
Browse files Browse the repository at this point in the history
  • Loading branch information
Avaq committed Jun 8, 2017
1 parent 43211e7 commit f434193
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 32 deletions.
34 changes: 2 additions & 32 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down Expand Up @@ -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 = []){
Expand Down
10 changes: 10 additions & 0 deletions test/5.both.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down

0 comments on commit f434193

Please sign in to comment.