diff --git a/packages/ember-metal/lib/run_loop.js b/packages/ember-metal/lib/run_loop.js index 986fe5a141a..cb30c8f0439 100644 --- a/packages/ember-metal/lib/run_loop.js +++ b/packages/ember-metal/lib/run_loop.js @@ -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) { @@ -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, @@ -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); }; @@ -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); }; diff --git a/packages/ember-metal/tests/run_loop/schedule_test.js b/packages/ember-metal/tests/run_loop/schedule_test.js index 0e0af27fe67..29c311e3e2c 100644 --- a/packages/ember-metal/tests/run_loop/schedule_test.js +++ b/packages/ember-metal/tests/run_loop/schedule_test.js @@ -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'); @@ -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', () => { @@ -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']() { diff --git a/packages/ember-metal/tests/run_loop/sync_test.js b/packages/ember-metal/tests/run_loop/sync_test.js new file mode 100644 index 00000000000..9cebd494e33 --- /dev/null +++ b/packages/ember-metal/tests/run_loop/sync_test.js @@ -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'); + } +}); +