Skip to content

Commit

Permalink
[BUGFIX beta] Revert "Remove sync runloop queue."
Browse files Browse the repository at this point in the history
This reverts commit 8627dfa.
This reverts commit 82a5da2.
  • Loading branch information
rwjblue committed Jan 16, 2018
1 parent d190957 commit 8db050f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
35 changes: 34 additions & 1 deletion packages/ember-metal/lib/run_loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { assert, 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 @@ -323,6 +331,31 @@ run.later = function(/*target, method*/) {
return backburner.later(...arguments);
};

/**
Immediately flushes any events scheduled in the 'sync' queue. Bindings
use this queue so this method is a useful way to immediately force all
bindings in the application to sync.
You should call this method anytime you need any changed state to propagate
throughout the app immediately without repainting the UI (which happens
in the later 'render' queue added by the `ember-views` package).
```javascript
run.sync();
```
@method sync
@static
@for @ember/runloop
@return {void}
@private
*/
run.sync = function() {
if (backburner.currentInstance) {
backburner.currentInstance.queues.sync.flush();
}
};

/**
Schedule a function to run one time during the current RunLoop. This is equivalent
to calling `scheduleOnce` with the "actions" queue.
Expand Down
12 changes: 11 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,11 @@ moduleFor('system/run_loop/schedule_test', class extends AbstractTestCase {
let runLoop = run.currentRunLoop;
assert.ok(runLoop, 'run loop present');

run.schedule('sync', () => {
order.push('sync');
assert.equal(runLoop, run.currentRunLoop, 'same run loop used');
});

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

run.schedule('sync', () => {
order.push('sync');
assert.equal(runLoop, run.currentRunLoop, 'same run loop used');
});
});

run.schedule('destroy', () => {
Expand All @@ -64,7 +74,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
35 changes: 35 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,35 @@
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) {
run.schedule('sync', syncfunc);
}
run.schedule('actions', cntup);
}

syncfunc();

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

assert.equal(cnt, 5, 'should have run sync queue continuously');
});

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

['@test calling sync() outside a run loop does not cause an error'](assert) {
assert.expect(0);

run.sync();
}
});

0 comments on commit 8db050f

Please sign in to comment.