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

Bring back sync queue with deprecation (until: 3.5.0). #16132

Merged
merged 2 commits into from
Jan 18, 2018
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
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');
}
});