Skip to content

Commit

Permalink
set delay before function execution in Function#every so that the
Browse files Browse the repository at this point in the history
executing context can properly call Function#cancel (Issue andrewplummer#488)
  • Loading branch information
andrewplummer committed Apr 6, 2015
1 parent 9efd045 commit 818955a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
4 changes: 3 additions & 1 deletion lib/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,10 @@
var fn = this, args = arguments;
args = args.length > 1 ? multiArgs(args, null, 1) : [];
function execute () {
fn.apply(fn, args);
// Set the delay first here, so that cancel
// can be called within the executing function.
setDelay(fn, ms, execute);
fn.apply(fn, args);
}
setDelay(fn, ms, execute);
return fn;
Expand Down
24 changes: 20 additions & 4 deletions test/environments/sugar/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,18 +478,34 @@ package('Function', function () {
async(function(wrap, finish) {
// Issue #348
var counter = 0;
var check1Finished = false;
var check2Finished = false;
function checkFinished() {
if (check1Finished && check2Finished) {
finish();
}
}
var fn = wrap(function(one, two) {
equal(this, fn, 'this object should be the function');
equal(one, 'one', 'first argument should be curried');
equal(two, 'two', 'second argument should be curried');
counter++;
if (counter === 5) {
run(fn, 'cancel');
setTimeout(wrap(function() {
// Issue #488
equal(counter, 5, 'should not have been called since cancel was run');
check1Finished = true;
checkFinished();
}), 50);
}
});
run(fn, 'every' , [10, 'one', 'two']);
setTimeout(wrap(function() {
run(fn, 'cancel');
equal(counter > 6, true, 'should have been called at least 7 times');
finish();
}), 150);
equal(counter, 5, 'should have been called 5 times');
check2Finished = true;
checkFinished();
}), 100);
});
});

Expand Down

0 comments on commit 818955a

Please sign in to comment.