Skip to content

Commit

Permalink
Merge pull request #14550 from emberjs/allow-canceling-scheduled-tasks
Browse files Browse the repository at this point in the history
[BUGFIX lts-2-8] Allow canceling items queued by `run.schedule`.
  • Loading branch information
stefanpenner authored Oct 30, 2016
2 parents 62a2c1c + 6f7c4fe commit b307387
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/ember-metal/lib/run_loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ run.end = function() {
will be resolved on the target object at the time the scheduled item is
invoked allowing you to change the target function.
@param {Object} [arguments*] Optional arguments to be passed to the queued method.
@return {void}
@return {*} Timer information for use in cancelling, see `run.cancel`.
@public
*/
run.schedule = function(/* queue, target, method */) {
Expand All @@ -269,7 +269,8 @@ run.schedule = function(/* queue, target, method */) {
`You will need to wrap any code with asynchronous side-effects in a run`,
run.currentRunLoop || !isTesting()
);
backburner.schedule(...arguments);

return backburner.schedule(...arguments);
};

// Used by global test teardown
Expand Down
11 changes: 11 additions & 0 deletions packages/ember-metal/tests/run_loop/schedule_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ QUnit.test('scheduling item in queue should defer until finished', function() {
equal(cnt, 2, 'should flush actions now');
});

QUnit.test('a scheduled item can be canceled', function(assert) {
let hasRan = false;

run(() => {
let cancelId = run.schedule('actions', () => hasRan = true);
run.cancel(cancelId);
});

assert.notOk(hasRan, 'should not have ran callback run');
});

QUnit.test('nested runs should queue each phase independently', function() {
let cnt = 0;

Expand Down

0 comments on commit b307387

Please sign in to comment.