Skip to content

Commit

Permalink
Revert the removal of the Eager structure
Browse files Browse the repository at this point in the history
It turns out we need this to build parallel actions
that make use of transformations.

Fixes #118
  • Loading branch information
Avaq committed Jun 28, 2017
1 parent 3cf5b2d commit b7f3f8a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
35 changes: 33 additions & 2 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,37 @@ 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){ this.cancel(); return new Rejected(x) }
resolved(x){ this.cancel(); return new Resolved(x) }
Expand Down Expand Up @@ -442,7 +473,7 @@ export class OrAction extends Action{
}
export class RaceAction extends Action{
constructor(other){ super(); this.other = other }
run(early){ return new RaceActionState(early, this.other) }
run(early){ return new RaceActionState(early, new Eager(this.other)) }
toString(){ return `race(${this.other.toString()})` }
}
export class RaceActionState extends RaceAction{
Expand All @@ -453,7 +484,7 @@ export class RaceActionState extends RaceAction{
}
export class BothAction extends Action{
constructor(other){ super(); this.other = other }
run(early){ return new BothActionState(early, this.other) }
run(early){ return new BothActionState(early, new Eager(this.other)) }
resolved(x){ return this.other._map(y => [x, y]) }
toString(){ return `both(${this.other.toString()})` }
}
Expand Down
14 changes: 13 additions & 1 deletion test/5.both.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect} from 'chai';
import {Future, both, of} from '../index.es.js';
import {Future, both, of, node} from '../index.es.js';
import * as U from './util';
import * as F from './futures';
import type from 'sanctuary-type-identifiers';
Expand Down Expand Up @@ -60,6 +60,18 @@ const testInstance = both => {

});

it('[GH #118] does not call the left computation twice', done => {
let called = false;
const left = node(f => called ? done(U.error) : setTimeout(f, 20, null, called = true));
return both(left, F.resolvedSlow).done(done);
});

it('[GH #118] does not call the right computation twice', done => {
let called = false;
const right = node(f => called ? done(U.error) : setTimeout(f, 20, null, called = true));
return both(F.resolvedSlow, right).done(done);
});

it('cancels the right if the left rejects', done => {
const m = both(F.rejectedSlow, Future(() => () => done()));
m.fork(U.noop, U.noop);
Expand Down

0 comments on commit b7f3f8a

Please sign in to comment.