Permalink
Browse files

Merge branch 'review.defer-client'

  • Loading branch information...
2 parents dbd87ba + 4ff31f0 commit 7d0ad98f3a96a390ff0168fdcb0e176b58e75fb7 @ConradIrwin ConradIrwin committed Feb 14, 2012
@@ -266,6 +266,17 @@
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
+ },
+
+ deferInSequence: function (functions) {
+ var funcs = _.clone(functions);
+
+ return _.defer(function () {
+ if (funcs[0]) {
+ funcs.shift().apply();
+ return _.deferInSequence(funcs);
+ }
+ });
}
});
@@ -1,6 +1,6 @@
describe('fillable', function () {
- function name (context, value) {
+ function name(context, value) {
var _public = {}, _protected = {};
lib.fillable(_public, _protected, function (value) {
@@ -34,7 +34,7 @@ describe('fillable', function () {
return _public;
}
- function profile (context, data) {
+ function profile(context, data) {
var _public = {}, _protected = {};
lib.fillable(_public, _protected, {
@@ -89,7 +89,7 @@ describe('fillable', function () {
memberships: [
{profile_url: 'http://fb.com/a', username: 'conrad.irwin'},
{profile_url: 'http://twitter.com/b', username: 'ConradIrwin'}
- ],
+ ]
});
});
@@ -1,3 +1,4 @@
+/*global window */
describe("_.", function () {
describe("andand", function () {
@@ -132,4 +133,38 @@ describe("_.", function () {
});
});
});
+
+ describe("deferInSequence", function () {
+ var a;
+ beforeEach(function () {
+ a = 0;
+ });
+
+ function first() {
+ a += 10;
+ }
+ function second() {
+ a *= 5;
+ }
+ function third() {
+ a += 10;
+ }
+ it("should run all functions", function () {
+ expect(a).toBe(0);
+ _.deferInSequence([first, second, third]);
+ expect(a).toBe(0); // they should run deferred
+
+ waitsFor(function () {
+ return a === 60;
+ }, "sequence did not complete");
+ });
+
+ it("should run the sequence in the correct order", function () {
+ _.deferInSequence([second, first, third]);
+
+ waitsFor(function () {
+ return a === 20;
+ }, "sequence did not complete");
+ });
+ });
});

0 comments on commit 7d0ad98

Please sign in to comment.