Skip to content

Commit

Permalink
Merge pull request #16132 from rwjblue/bring-back-sync
Browse files Browse the repository at this point in the history
Bring back `sync` queue with deprecation (until: 3.5.0).
  • Loading branch information
rwjblue authored Jan 18, 2018
2 parents 44ceba5 + 20d5297 commit 16879e0
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
26 changes: 22 additions & 4 deletions packages/ember-metal/lib/run_loop.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { assert, isTesting } from 'ember-debug';
import { assert, deprecate, isTesting } from 'ember-debug';
import {
onErrorTarget
} from './error_handler';
import {
beginPropertyChanges,
endPropertyChanges
} from './property_events';
import Backburner from 'backburner';

function onBegin(current) {
Expand All @@ -12,7 +16,11 @@ function onEnd(current, next) {
run.currentRunLoop = next;
}

const backburner = new Backburner(['actions', 'destroy'], {
const backburner = new Backburner(['sync', 'actions', 'destroy'], {
sync: {
before: beginPropertyChanges,
after: endPropertyChanges
},
defaultQueue: 'actions',
onBegin,
onEnd,
Expand Down Expand Up @@ -269,12 +277,17 @@ run.end = function() {
@return {*} Timer information for use in canceling, see `run.cancel`.
@public
*/
run.schedule = function(/* queue, target, method */) {
run.schedule = function(queue /*, target, method */) {
assert(
`You have turned on testing mode, which disabled the run-loop's autorun. ` +
`You will need to wrap any code with asynchronous side-effects in a run`,
run.currentRunLoop || !isTesting()
);
deprecate(
`Scheduling into the '${queue}' run loop queue is deprecated.`,
queue !== 'sync',
{ id: 'ember-metal.run.sync', until: '3.5.0' }
);

return backburner.schedule(...arguments);
};
Expand Down Expand Up @@ -420,12 +433,17 @@ run.once = function(...args) {
@return {Object} Timer information for use in canceling, see `run.cancel`.
@public
*/
run.scheduleOnce = function(/*queue, target, method*/) {
run.scheduleOnce = function(queue /*, target, method*/) {
assert(
`You have turned on testing mode, which disabled the run-loop's autorun. ` +
`You will need to wrap any code with asynchronous side-effects in a run`,
run.currentRunLoop || !isTesting()
);
deprecate(
`Scheduling into the '${queue}' run loop queue is deprecated.`,
queue !== 'sync',
{ id: 'ember-metal.run.sync', until: '3.5.0' }
);
return backburner.scheduleOnce(...arguments);
};

Expand Down
16 changes: 15 additions & 1 deletion packages/ember-metal/tests/run_loop/schedule_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ moduleFor('system/run_loop/schedule_test', class extends AbstractTestCase {
let runLoop = run.currentRunLoop;
assert.ok(runLoop, 'run loop present');

expectDeprecation(() => {
run.schedule('sync', () => {
order.push('sync');
assert.equal(runLoop, run.currentRunLoop, 'same run loop used');
});
}, `Scheduling into the 'sync' run loop queue is deprecated.`);

run.schedule('actions', () => {
order.push('actions');
assert.equal(runLoop, run.currentRunLoop, 'same run loop used');
Expand All @@ -56,6 +63,13 @@ moduleFor('system/run_loop/schedule_test', class extends AbstractTestCase {
order.push('actions');
assert.equal(runLoop, run.currentRunLoop, 'same run loop used');
});

expectDeprecation(() => {
run.schedule('sync', () => {
order.push('sync');
assert.equal(runLoop, run.currentRunLoop, 'same run loop used');
});
}, `Scheduling into the 'sync' run loop queue is deprecated.`);
});

run.schedule('destroy', () => {
Expand All @@ -64,7 +78,7 @@ moduleFor('system/run_loop/schedule_test', class extends AbstractTestCase {
});
});

assert.deepEqual(order, ['actions', 'actions', 'destroy']);
assert.deepEqual(order, ['sync', 'actions', 'sync', 'actions', 'destroy']);
}

['@test makes sure it does not trigger an autorun during testing']() {
Expand Down
28 changes: 28 additions & 0 deletions packages/ember-metal/tests/run_loop/sync_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { run } from '../..';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';

moduleFor('system/run_loop/sync_test', class extends AbstractTestCase {
['@test sync() will immediately flush the sync queue only'](assert) {
let cnt = 0;

run(() => {
function cntup() { cnt++; }

function syncfunc() {
if (++cnt < 5) {
expectDeprecation(() => {
run.schedule('sync', syncfunc);
}, `Scheduling into the 'sync' run loop queue is deprecated.`);
}
run.schedule('actions', cntup);
}

syncfunc();

assert.equal(cnt, 1, 'should not run action yet');
});

assert.equal(cnt, 10, 'should flush actions now too');
}
});

0 comments on commit 16879e0

Please sign in to comment.