Skip to content

Commit

Permalink
Improve the interpretor
Browse files Browse the repository at this point in the history
* Optimize when parallel actions are executed
* Simplify the code a little bit
  • Loading branch information
Avaq committed Jun 6, 2017
1 parent 3706182 commit 9296445
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,10 @@ Sequence.prototype._fork = function Sequence$_fork(rej, res){

function early(m, terminator){
cancel();
if(action !== terminator){
cold.clear();
if(async && action !== terminator){
action.cancel();
while(async && (it = queue.shift()) && it !== terminator) it.cancel();
while((it = queue.shift()) && it !== terminator) it.cancel();
}
settle(m);
}
Expand All @@ -602,14 +603,13 @@ Sequence.prototype._fork = function Sequence$_fork(rej, res){
settled = false;
cancel = future._fork(rejected, resolved);
if(settled) continue;
action = action.run(early);
if(settled) continue;
while(it = cold.pop()){
it = it.run(early);
if(settled) break;
queue.unshift(it);
if(!settled) queue.unshift(it);
}
if(settled) continue;
action = action.run(early);
if(settled) continue;
async = true;
return;
}
Expand Down
11 changes: 11 additions & 0 deletions test/2.sequence.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,17 @@ describe('Sequence', () => {
setTimeout(done, 100, null);
});

it('does not run actions unnecessarily when one early-terminates synchronously', done => {
const broken = new Sequence(Future(_ => { console.log('broken'); done(error) }));
const m = resolvedSlow.race(broken).race(broken).race(resolved);
m.fork(noop, _ => done());
});

it('resolves the left-hand side first when running actions in parallel', () => {
const m = new Sequence(of(1)).map(x => x).chain(x => of(x));
return assertResolved(m.race(of(2)), 1);
});

it('does not forget about actions to run after early termination', () => {
const m = new Sequence(after(30, 'a'))
.race(new Sequence(after(20, 'b')))
Expand Down

0 comments on commit 9296445

Please sign in to comment.