Permalink
Browse files

Use rsvp.js in Deferred mixin

  • Loading branch information...
1 parent c52773d commit 1b34a2a115fa7fbc65de37f867ab8e4db410c5d1 @tchak tchak committed Oct 16, 2012
View
@@ -5,6 +5,7 @@
"DS",
"Handlebars",
"Metamorph",
+ "RSVP",
"ember_assert",
"ember_warn",
"ember_deprecate",
View
@@ -85,7 +85,7 @@ end
distros = {
:runtime => %w(ember-metal ember-runtime),
- :full => %w(ember-metal ember-runtime ember-application ember-views ember-states ember-routing ember-viewstates metamorph ember-handlebars)
+ :full => %w(ember-metal rsvp ember-runtime ember-application ember-views ember-states ember-routing ember-viewstates metamorph ember-handlebars)
}
output "dist"
@@ -1,50 +1,12 @@
+require("rsvp");
+
/**
@module ember
@submodule ember-runtime
*/
-var get = Ember.get, set = Ember.set,
- slice = Array.prototype.slice,
- forEach = Ember.ArrayPolyfills.forEach;
-
-var Callbacks = function(target, once) {
- this.target = target;
- this.once = once || false;
- this.list = [];
- this.fired = false;
- this.off = false;
-};
-
-Callbacks.prototype = {
- add: function(callback) {
- if (this.off) { return; }
-
- this.list.push(callback);
-
- if (this.fired) { this.flush(); }
- },
-
- fire: function() {
- if (this.off || this.once && this.fired) { return; }
- if (!this.fired) { this.fired = true; }
-
- this.args = slice.call(arguments);
-
- if (this.list.length > 0) { this.flush(); }
- },
-
- flush: function() {
- Ember.run.once(this, 'flushCallbacks');
- },
-
- flushCallbacks: function() {
- forEach.call(this.list, function(callback) {
- callback.apply(this.target, this.args);
- }, this);
- if (this.once) { this.list = []; }
- }
-};
-
+var get = Ember.get,
+ slice = Array.prototype.slice;
/**
@class Deferred
@@ -59,71 +21,30 @@ Ember.Deferred = Ember.Mixin.create({
@method then
@param {Function} doneCallback a callback function to be called when done
@param {Function} failCallback a callback function to be called when failed
- @param {Function} progressCallback a callback function to be called when progressed
- */
- then: function(doneCallback, failCallback, progressCallback) {
- if (doneCallback) {
- get(this, 'deferredDone').add(doneCallback);
- }
- if (failCallback) {
- get(this, 'deferredFail').add(failCallback);
- }
- if (progressCallback) {
- get(this, 'deferredProgress').add(progressCallback);
- }
-
- return this;
- },
-
- /**
- Call the progressCallbacks on a Deferred object with the given args.
-
- @method notify
*/
- notify: function() {
- var callbacks = get(this, 'deferredProgress');
- callbacks.fire.apply(callbacks, slice.call(arguments));
-
- return this;
+ then: function(doneCallback, failCallback) {
+ return get(this, 'promise').then(doneCallback, failCallback);
},
/**
Resolve a Deferred object and call any doneCallbacks with the given args.
@method resolve
*/
- resolve: function() {
- var callbacks = get(this, 'deferredDone');
- callbacks.fire.apply(callbacks, slice.call(arguments));
- set(this, 'deferredProgress.off', true);
- set(this, 'deferredFail.off', true);
-
- return this;
+ resolve: function(value) {
+ get(this, 'promise').resolve(value);
},
/**
Reject a Deferred object and call any failCallbacks with the given args.
@method reject
*/
- reject: function() {
- var callbacks = get(this, 'deferredFail');
- callbacks.fire.apply(callbacks, slice.call(arguments));
- set(this, 'deferredProgress.off', true);
- set(this, 'deferredDone.off', true);
-
- return this;
+ reject: function(value) {
+ get(this, 'promise').reject(value);
},
- deferredDone: Ember.computed(function() {
- return new Callbacks(this, true);
- }).cacheable(),
-
- deferredFail: Ember.computed(function() {
- return new Callbacks(this, true);
- }).cacheable(),
-
- deferredProgress: Ember.computed(function() {
- return new Callbacks(this);
+ promise: Ember.computed(function() {
+ return new RSVP.Promise();
}).cacheable()
});
@@ -123,43 +123,7 @@ test("can call resolve multiple times", function() {
}, 20);
});
-test("deferred has progress", function() {
-
- var deferred, count = 0;
-
- Ember.run(function() {
- deferred = Ember.Object.create(Ember.Deferred);
- });
-
- deferred.then(function() {}, function() {}, function() {
- count++;
- });
-
- stop();
- Ember.run(function() {
- deferred.notify();
- deferred.notify();
- deferred.notify();
- });
- Ember.run(function() {
- deferred.notify();
- });
- Ember.run(function() {
- deferred.notify();
- deferred.resolve();
- deferred.notify();
- });
- Ember.run(function() {
- deferred.notify();
- });
-
- setTimeout(function() {
- start();
- equal(count, 3, "progress called three times");
- }, 20);
-});
-
-test("resolve prevent reject and stop progress", function() {
+test("resolve prevent reject", function() {
var deferred, resolved = false, rejected = false, progress = 0;
Ember.run(function() {
@@ -170,33 +134,24 @@ test("resolve prevent reject and stop progress", function() {
resolved = true;
}, function() {
rejected = true;
- }, function() {
- progress++;
});
stop();
Ember.run(function() {
- deferred.notify();
- });
- Ember.run(function() {
deferred.resolve();
});
Ember.run(function() {
deferred.reject();
});
- Ember.run(function() {
- deferred.notify();
- });
setTimeout(function() {
start();
equal(resolved, true, "is resolved");
equal(rejected, false, "is not rejected");
- equal(progress, 1, "progress called once");
}, 20);
});
-test("reject prevent resolve and stop progress", function() {
+test("reject prevent resolve", function() {
var deferred, resolved = false, rejected = false, progress = 0;
Ember.run(function() {
@@ -207,29 +162,20 @@ test("reject prevent resolve and stop progress", function() {
resolved = true;
}, function() {
rejected = true;
- }, function() {
- progress++;
});
stop();
Ember.run(function() {
- deferred.notify();
- });
- Ember.run(function() {
deferred.reject();
});
Ember.run(function() {
deferred.resolve();
});
- Ember.run(function() {
- deferred.notify();
- });
setTimeout(function() {
start();
equal(resolved, false, "is not resolved");
equal(rejected, true, "is rejected");
- equal(progress, 1, "progress called once");
}, 20);
});
Oops, something went wrong.

0 comments on commit 1b34a2a

Please sign in to comment.