Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert the removal of the Eager structure #119

Merged
merged 1 commit into from
Jun 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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