diff options
| author | Caio Cunha | 2013-03-24 22:18:10 -0300 | 
|---|---|---|
| committer | Chirayu Krishnappa | 2013-07-14 23:11:46 -0700 | 
| commit | 2a5c3555829da51f55abd810a828c73b420316d3 (patch) | |
| tree | 5e5dc32e42284c63aba7486259b48a69f205e97b /src | |
| parent | d884eb80a10a336f6765d5fe20554933eda23545 (diff) | |
| download | angular.js-2a5c3555829da51f55abd810a828c73b420316d3.tar.bz2 | |
feat($q): added support to promise notification
It is now possible to notify a promise through deferred.notify() method.
Notifications are useful to provide a way to send progress information
to promise holders.
Diffstat (limited to 'src')
| -rw-r--r-- | src/ng/q.js | 48 | 
1 files changed, 42 insertions, 6 deletions
| diff --git a/src/ng/q.js b/src/ng/q.js index 994ecd8d..fe05b37f 100644 --- a/src/ng/q.js +++ b/src/ng/q.js @@ -199,7 +199,7 @@ function qFactory(nextTick, exceptionHandler) {                var callback;                for (var i = 0, ii = callbacks.length; i < ii; i++) {                  callback = callbacks[i]; -                value.then(callback[0], callback[1]); +                value.then(callback[0], callback[1], callback[2]);                }              });            } @@ -212,8 +212,25 @@ function qFactory(nextTick, exceptionHandler) {        }, +      notify: function(progress) { +        if (pending) { +          var callbacks = pending; + +          if (pending.length) { +            nextTick(function() { +              var callback; +              for (var i = 0, ii = callbacks.length; i < ii; i++) { +                callback = callbacks[i]; +                callback[2](progress); +              } +            }); +          } +        } +      }, + +        promise: { -        then: function(callback, errback) { +        then: function(callback, errback, progressback) {            var result = defer();            var wrappedCallback = function(value) { @@ -234,10 +251,18 @@ function qFactory(nextTick, exceptionHandler) {              }            }; +          var wrappedProgressback = function(progress) { +            try { +              result.notify((progressback || defaultCallback)(progress)); +            } catch(e) { +              exceptionHandler(e); +            } +          }; +            if (pending) { -            pending.push([wrappedCallback, wrappedErrback]); +            pending.push([wrappedCallback, wrappedErrback, wrappedProgressback]);            } else { -            value.then(wrappedCallback, wrappedErrback); +            value.then(wrappedCallback, wrappedErrback, wrappedProgressback);            }            return result.promise; @@ -359,7 +384,7 @@ function qFactory(nextTick, exceptionHandler) {     * @param {*} value Value or a promise     * @returns {Promise} Returns a promise of the passed value or promise     */ -  var when = function(value, callback, errback) { +  var when = function(value, callback, errback, progressback) {      var result = defer(),          done; @@ -381,15 +406,26 @@ function qFactory(nextTick, exceptionHandler) {        }      }; +    var wrappedProgressback = function(progress) { +      try { +        return (progressback || defaultCallback)(progress); +      } catch (e) { +        exceptionHandler(e); +      } +    }; +      nextTick(function() {        ref(value).then(function(value) {          if (done) return;          done = true; -        result.resolve(ref(value).then(wrappedCallback, wrappedErrback)); +        result.resolve(ref(value).then(wrappedCallback, wrappedErrback, wrappedProgressback));        }, function(reason) {          if (done) return;          done = true;          result.resolve(wrappedErrback(reason)); +      }, function(progress) { +        if (done) return; +        result.notify(wrappedProgressback(progress));        });      }); | 
