Permalink
Browse files

Merge branch '0-9-stable'

  • Loading branch information...
wagenet committed May 24, 2012
2 parents 2756d58 + fa38855 commit 3e26ac99cbc1b72c90553e7ade8a7eceb01c6fd4
@@ -321,14 +321,37 @@ Ember.run.schedule = function(queue, target, method) {
loop.schedule.apply(loop, arguments);
};
-var autorunTimer;
-
+var scheduledAutorun;
/** @private */
function autorun() {
- autorunTimer = null;
+ scheduledAutorun = null;
if (run.currentRunLoop) run.end();
}
+// Used by global test teardown
+/** @private */
+Ember.run.hasScheduledTimers = function() {
+ return !!(scheduledAutorun || scheduledLater || scheduledNext);
+};
+
+// Used by global test teardown
+/** @private */
+Ember.run.cancelTimers = function () {
+ if (scheduledAutorun) {
+ clearTimeout(scheduledAutorun);
+ scheduledAutorun = null;
+ }
+ if (scheduledLater) {
+ clearTimeout(scheduledLater);
+ scheduledLater = null;
+ }
+ if (scheduledNext) {
+ clearTimeout(scheduledNext);
+ scheduledNext = null;
+ }
+ timers = {};
+};
+
/**
Begins a new RunLoop if necessary and schedules a timer to flush the
RunLoop at a later time. This method is used by parts of Ember to
@@ -344,11 +367,8 @@ Ember.run.autorun = function() {
if (!run.currentRunLoop) {
run.begin();
- if (Ember.ENV.PREVENT_AUTOMATIC_RUNLOOP_CREATION) {
- throw "A RunLoop cannot be automatically created during testing. " +
- "Wrap code that requires an active RunLoop inside an Ember.run(function{ ... }).";
- } else if (!autorunTimer) {
- autorunTimer = setTimeout(autorun, 1);
+ if (!scheduledAutorun) {
+ scheduledAutorun = setTimeout(autorun, 1);
}
}
@@ -378,9 +398,10 @@ Ember.run.sync = function() {
var timers = {}; // active timers...
-var laterScheduled = false;
+var scheduledLater;
/** @private */
function invokeLaterTimers() {
+ scheduledLater = null;
var now = (+ new Date()), earliest = -1;
for(var key in timers) {
if (!timers.hasOwnProperty(key)) continue;
@@ -396,7 +417,7 @@ function invokeLaterTimers() {
}
// schedule next timeout to fire...
- if (earliest>0) setTimeout(invokeLaterTimers, earliest-(+ new Date()));
+ if (earliest>0) scheduledLater = setTimeout(invokeLaterTimers, earliest-(+ new Date()));
}
/**
@@ -514,7 +535,7 @@ Ember.run.once = function(target, method) {
return guid;
};
-var scheduledNext = false;
+var scheduledNext;
/** @private */
function invokeNextTimers() {
scheduledNext = null;
@@ -10,6 +10,7 @@ function testBool(val, expected) {
Ember.run(function(){
Ember.set(MyApp.foo, 'value', val);
});
+
equal(Ember.get(MyApp.bar, 'value'), expected);
});
}
@@ -9,39 +9,32 @@ require('ember-metal/~tests/props_helper');
var previousPreventRunloop;
-module('system/mixin/binding/connect_test', {
- setup: function(){
- previousPreventRunloop = Ember.ENV.PREVENT_AUTOMATIC_RUNLOOP_CREATION;
- Ember.ENV.PREVENT_AUTOMATIC_RUNLOOP_CREATION = false;
- },
- teardown: function(){
- Ember.ENV.PREVENT_AUTOMATIC_RUNLOOP_CREATION = previousPreventRunloop;
- }
-});
+function performTest(binding, a, b, get, set, connect) {
+ if (connect === undefined) connect = function(){binding.connect(a);};
-function performTest(binding, a, b, get, set, skipFirst) {
+ ok(!Ember.run.currentRunLoop, 'performTest should not have a currentRunLoop');
- if (!skipFirst) {
- Ember.run.sync();
- equal(get(a, 'foo'), 'FOO', 'a should not have changed');
- equal(get(b, 'bar'), 'BAR', 'b should not have changed');
- }
+ equal(get(a, 'foo'), 'FOO', 'a should not have changed');
+ equal(get(b, 'bar'), 'BAR', 'b should not have changed');
- binding.connect(a);
- equal(get(a, 'foo'), 'FOO', 'a should not have changed before sync');
- equal(get(b, 'bar'), 'BAR', 'b should not have changed before sync');
+ Ember.run(function () {
+ connect();
+ equal(get(a, 'foo'), 'FOO', 'a should not have changed before sync');
+ equal(get(b, 'bar'), 'BAR', 'b should not have changed before sync');
+ });
- Ember.run.sync();
equal(get(a, 'foo'), 'BAR', 'a should have changed');
equal(get(b, 'bar'), 'BAR', 'b should have changed');
//
// make sure changes sync both ways
- set(b, 'bar', 'BAZZ');
- Ember.run.sync();
+ Ember.run(function () {
+ set(b, 'bar', 'BAZZ');
+ });
equal(get(a, 'foo'), 'BAZZ', 'a should have changed');
- set(a, 'foo', 'BARF');
- Ember.run.sync();
+ Ember.run(function () {
+ set(a, 'foo', 'BARF');
+ });
equal(get(b, 'bar'), 'BARF', 'a should have changed');
}
@@ -50,7 +43,7 @@ testBoth('Connecting a binding between two properties', function(get, set) {
// a.bar -> a.foo
var binding = new Ember.Binding('foo', 'bar');
-
+
performTest(binding, a, a, get, set);
});
@@ -70,11 +63,12 @@ testBoth('Connecting a binding between two objects through property defined afte
// b.bar -> a.foo
var binding = new Ember.Binding('foo', 'b.bar');
- binding.connect(a);
- Ember.defineProperty(a, 'b', Ember.SIMPLE_PROPERTY, b);
+ performTest(binding, a, b, get, set, function () {
+ binding.connect(a);
- performTest(binding, a, b, get, set, true);
+ Ember.defineProperty(a, 'b', Ember.SIMPLE_PROPERTY, b);
+ });
});
testBoth('Connecting a binding between two objects through property overriden after connect', function(get, set) {
@@ -84,15 +78,16 @@ testBoth('Connecting a binding between two objects through property overriden af
// b.bar -> a.foo
var binding = new Ember.Binding('foo', 'b.bar');
- binding.connect(a);
- equal(Ember.isWatching(c, 'bar'), true, 'should be watching bar');
+ performTest(binding, a, b, get, set, function () {
+ binding.connect(a);
- Ember.Mixin.create({b: b}).apply(a);
+ equal(Ember.isWatching(c, 'bar'), true, 'should be watching bar');
- equal(Ember.isWatching(c, 'bar'), false, 'should not be watching bar');
+ Ember.Mixin.create({b: b}).apply(a);
- performTest(binding, a, b, get, set, true);
+ equal(Ember.isWatching(c, 'bar'), false, 'should not be watching bar');
+ });
});
testBoth('Connecting a binding to path', function(get, set) {
@@ -110,8 +105,11 @@ testBoth('Connecting a binding to path', function(get, set) {
// make sure modifications update
b = { bar: 'BIFF' };
- set(GlobalB, 'b', b);
- Ember.run.sync();
+
+ Ember.run(function(){
+ set(GlobalB, 'b', b);
+ });
+
equal(get(a, 'foo'), 'BIFF', 'a should have changed');
});
@@ -122,40 +120,49 @@ testBoth('Calling connect more than once', function(get, set) {
// b.bar -> a.foo
var binding = new Ember.Binding('foo', 'b.bar');
- binding.connect(a);
- performTest(binding, a, b, get, set, true);
+ performTest(binding, a, b, get, set, function () {
+ binding.connect(a);
+
+ binding.connect(a);
+ });
});
testBoth('Bindings should be inherited', function(get, set) {
var a = { foo: 'FOO', b: { bar: 'BAR' } };
var binding = new Ember.Binding('foo', 'b.bar');
- binding.connect(a);
+ var a2;
+
+ Ember.run(function () {
+ binding.connect(a);
+
+ a2 = Ember.create(a);
+ });
- var a2 = Ember.create(a);
- Ember.run.sync();
equal(get(a2, 'foo'), "BAR", "Should have synced binding on child");
equal(get(a, 'foo'), "BAR", "Should NOT have synced binding on parent");
- set(a2, 'b', { bar: 'BAZZ' });
- Ember.run.sync();
+ Ember.run(function () {
+ set(a2, 'b', { bar: 'BAZZ' });
+ });
equal(get(a2, 'foo'), "BAZZ", "Should have synced binding on child");
equal(get(a, 'foo'), "BAR", "Should NOT have synced binding on parent");
});
test('inherited bindings should sync on create', function() {
+ var a;
+ Ember.run(function () {
+ var A = function() {
+ Ember.bind(this, 'foo', 'bar.baz');
+ };
- var A = function() {
- Ember.bind(this, 'foo', 'bar.baz');
- };
-
- var a = new A();
- Ember.set(a, 'bar', { baz: 'BAZ' });
+ a = new A();
+ Ember.set(a, 'bar', { baz: 'BAZ' });
+ });
- Ember.run.sync();
equal(Ember.get(a, 'foo'), 'BAZ', 'should have synced binding on new obj');
});
@@ -123,30 +123,32 @@ test('transform function should NOT be invoked on fwd change', function() {
});
test('transforms should chain', function() {
- binding = Ember.bind(MyApp, 'foo.value', 'bar.value');
- binding.transform({
- to: function(value) { return value+' T1'; }
- });
- binding.transform({
- to: function(value) { return value+' T2'; }
+ Ember.run(function () {
+ binding = Ember.bind(MyApp, 'foo.value', 'bar.value');
+ binding.transform({
+ to: function(value) { return value+' T1'; }
+ });
+ binding.transform({
+ to: function(value) { return value+' T2'; }
+ });
});
- Ember.run.sync();
// should have transformed...
equal(Ember.getPath('MyApp.foo.value'), 'BAR T1 T2', 'should transform');
equal(Ember.getPath('MyApp.bar.value'), 'BAR', 'should stay original');
});
test('resetTransforms() should clear', function() {
- binding = Ember.bind(MyApp, 'foo.value', 'bar.value');
- binding.transform({
- to: function(value) { return value+' T1'; }
- });
- binding.resetTransforms();
- binding.transform({
- to: function(value) { return value+' T2'; }
+ Ember.run(function () {
+ binding = Ember.bind(MyApp, 'foo.value', 'bar.value');
+ binding.transform({
+ to: function(value) { return value+' T1'; }
+ });
+ binding.resetTransforms();
+ binding.transform({
+ to: function(value) { return value+' T2'; }
+ });
});
- Ember.run.sync();
// should have transformed...
equal(Ember.getPath('MyApp.foo.value'), 'BAR T2', 'should transform');
@@ -6,17 +6,6 @@
var previousPreventRunloop;
-module('system/run_loop/later_test', {
- setup: function() {
- previousPreventRunloop = Ember.ENV.PREVENT_AUTOMATIC_RUNLOOP_CREATION;
- Ember.ENV.PREVENT_AUTOMATIC_RUNLOOP_CREATION = false;
- },
-
- teardown: function() {
- Ember.ENV.PREVENT_AUTOMATIC_RUNLOOP_CREATION = previousPreventRunloop;
- }
-});
-
test('should invoke after specified period of time - function only', function() {
var invoked = false;
@@ -29,31 +29,3 @@ test('sync() will immediately flush the sync queue only', function() {
equal(cnt, 10, 'should flush actions now too');
});
-
-
-test('sync() works outside of runloop (by fixing runloop)', function() {
- var previousPreventRunloop = Ember.ENV.PREVENT_AUTOMATIC_RUNLOOP_CREATION;
- Ember.ENV.PREVENT_AUTOMATIC_RUNLOOP_CREATION = false;
-
- try {
- var cnt = 0;
-
- function cntup() { cnt++; }
-
- function syncfunc() {
- if (++cnt<5) Ember.run.schedule('sync', syncfunc);
- Ember.run.schedule('actions', cntup);
-
- }
-
- syncfunc();
-
- equal(cnt, 1, 'should not run action yet') ;
- Ember.run.sync();
-
- equal(cnt, 5, 'should have run sync queue continuously');
- } finally {
- Ember.ENV.PREVENT_AUTOMATIC_RUNLOOP_CREATION = previousPreventRunloop;
- }
-});
-
Oops, something went wrong.

0 comments on commit 3e26ac9

Please sign in to comment.