Skip to content

Commit

Permalink
* Promises.js: setValue -> deliver
Browse files Browse the repository at this point in the history
  • Loading branch information
swannodette committed Mar 24, 2010
1 parent 4c3e79a commit eabf991
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions Promises.js
Expand Up @@ -28,7 +28,7 @@ Function.implement({
Options: Options:
lazy - The promise is treated as a lazy value. It only trigger a lazy - The promise is treated as a lazy value. It only trigger a
realized event when setValue is called on it. Defaults to false. realized event when deliver is called on it. Defaults to false.
reduce - A function. Convenience when a promise wraps an array of reduce - A function. Convenience when a promise wraps an array of
promises. The function should take an array and return a single value. promises. The function should take an array and return a single value.
bare - Useful when using promises to load files. Otherwise a promise bare - Useful when using promises to load files. Otherwise a promise
Expand Down Expand Up @@ -75,27 +75,27 @@ var Promise = new Class({
Promise.watch(value, function(promises) { Promise.watch(value, function(promises) {
var values = promises.map(Promise.getValue); var values = promises.map(Promise.getValue);
var result = (this.options.reduce && this.options.reduce.apply(null, values)) || values; var result = (this.options.reduce && this.options.reduce.apply(null, values)) || values;
this.setValue(result); this.deliver(result);
}.bind(this)); }.bind(this));
} else if(value && } else if(value &&
!Promise.isPromise(value) && !Promise.isPromise(value) &&
$type(value) == "object" && $type(value) == "object" &&
$H(value).getValues().some(Promise.isPromise)) { $H(value).getValues().some(Promise.isPromise)) {
// if handed an object look for promises in the values - not recursive // if handed an object look for promises in the values - not recursive
Promise.watch($H(value).getValues(), function(promises) { Promise.watch($H(value).getValues(), function(promises) {
this.setValue($H(value).map(Promise.getValue).getClean()); this.deliver($H(value).map(Promise.getValue).getClean());
}.bind(this)); }.bind(this));
} else if(Promise.isPromise(value)) { } else if(Promise.isPromise(value)) {
// if handed a promise, watch it // if handed a promise, watch it
this.__promise = value; this.__promise = value;
value.addEvent('realized', function() { value.addEvent('realized', function() {
this.setValue(value.value()); this.deliver(value.value());
}.bind(this)); }.bind(this));
} else if(typeof value != 'undefined') { } else if(typeof value != 'undefined') {
// if handed a regular value, set the value immediately but don't // if handed a regular value, set the value immediately but don't
// trigger a realized event. // trigger a realized event.
this.__plain = true; this.__plain = true;
this.setValue(value, false); this.deliver(value, false);
} else if(typeof value == 'undefined') { } else if(typeof value == 'undefined') {
this.__plain = true; this.__plain = true;
this.options.lazy = true; this.options.lazy = true;
Expand Down Expand Up @@ -126,9 +126,9 @@ var Promise = new Class({
/* /*
Function: initReq Function: initReq
*private* *private*
Initialize the request. If successfull calls setValue with the value Initialize the request. If successfull calls deliver with the value
received from the server after applying all operations on the value received from the server after applying all operations on the value
first. On request failure, setValues to undefined and fires an error first. On request failure, delivers to undefined and fires an error
event passing itself as data. event passing itself as data.
Parameters: Parameters:
Expand All @@ -139,10 +139,10 @@ var Promise = new Class({
req.addEvent('onSuccess', function(responseText) { req.addEvent('onSuccess', function(responseText) {
var json = (!req.options.bare) ? JSON.decode(responseText) : responseText, var json = (!req.options.bare) ? JSON.decode(responseText) : responseText,
v = (json.data !== null && json.data !== undefined) ? json.data : json; v = (json.data !== null && json.data !== undefined) ? json.data : json;
this.setValue(this.applyOps(v)); this.deliver(this.applyOps(v));
}.bind(this)); }.bind(this));
req.addEvent('onFailure', function(responseText) { req.addEvent('onFailure', function(responseText) {
this.setValue(undefined); this.deliver(undefined);
this.fireEvent('error', this); this.fireEvent('error', this);
}.bind(this)); }.bind(this));
}, },
Expand All @@ -151,7 +151,7 @@ var Promise = new Class({
initDelayedAsset: function(asset) { initDelayedAsset: function(asset) {
this.__asset = asset; this.__asset = asset;
asset.addEvent('onload', function() { asset.addEvent('onload', function() {
this.setValue(true); this.deliver(true);
}.bind(this)); }.bind(this));
}, },


Expand Down Expand Up @@ -179,7 +179,7 @@ var Promise = new Class({
Realize the value of the promise. If the promise is a request, sends Realize the value of the promise. If the promise is a request, sends
the request. If Promise.debug == true or the async option is set to the request. If Promise.debug == true or the async option is set to
false the request will be made synchronously. If the value is plain false the request will be made synchronously. If the value is plain
simply calls setValue with the current value of the promise. simply calls deliver with the current value of the promise.
*/ */
realize: function() { realize: function() {
if(this.__req && !this.__realizing) { if(this.__req && !this.__realizing) {
Expand All @@ -190,11 +190,11 @@ var Promise = new Class({
this.__realizing = true; this.__realizing = true;
this.__asset.load(); this.__asset.load();
} else if(this.__plain) { } else if(this.__plain) {
this.setValue(this.value()); this.deliver(this.value());
} else if(this.__promise) { } else if(this.__promise) {
if(this.__promise.isRealized()) if(this.__promise.isRealized())
{ {
this.setValue(this.__promise.value()); this.deliver(this.__promise.value());
} }
else else
{ {
Expand Down Expand Up @@ -239,22 +239,22 @@ var Promise = new Class({
if(!this.__realized) { if(!this.__realized) {
this.__ops.push(fn); this.__ops.push(fn);
} else { } else {
this.setValue(fn(this.__value)); this.deliver(fn(this.__value));
} }
if(!this.__realized) return this; if(!this.__realized) return this;
return this.value(); return this.value();
}, },


/* /*
Function: setValue Function: deliver
If the promises wraps a Request this is called automatically. If the If the promises wraps a Request this is called automatically. If the
promise is lazy, calling this will trigger the realized event. promise is lazy, calling this will trigger the realized event.
Parameters: Parameters:
value - a value. value - a value.
notify - whether to trigger the realized event. notify - whether to trigger the realized event.
*/ */
setValue: function(value, notify) { deliver: function(value, notify) {
if(value && value.xhr) { if(value && value.xhr) {
this.initReq(value); this.initReq(value);
} else if(!this.__realized && notify !== false) { } else if(!this.__realized && notify !== false) {
Expand Down Expand Up @@ -473,7 +473,7 @@ Promise.watch = function(args, cb, errCb) {
}); });
} }


// don't attempt to realize lazy values. They are realized then setValue is called on them. // don't attempt to realize lazy values. They are realized then deliver is called on them.
unrealized.filter(Function.msg('isNotLazy')).each(Function.msg('realize')); unrealized.filter(Function.msg('isNotLazy')).each(Function.msg('realize'));
} else { } else {
cb(Promise.toValues(args)); cb(Promise.toValues(args));
Expand Down Expand Up @@ -521,7 +521,7 @@ function promise(fn) {
// hack so that this.parent(...) is meaningful even after an async call // hack so that this.parent(...) is meaningful even after an async call
var temp = this._current; var temp = this._current;
this._current = decorator._wrapper; this._current = decorator._wrapper;
p.setValue(fn.apply(this, values)); p.deliver(fn.apply(this, values));
this._current = temp; this._current = temp;
}.bind(this), }.bind(this),
function(errPromise) { function(errPromise) {
Expand Down

0 comments on commit eabf991

Please sign in to comment.