Comparing version 0.0.3 to 0.0.4
43
p.js
@@ -16,7 +16,7 @@ ;(function( factory ){ | ||
// linked list with head node - used as a queue of tasks | ||
// f: task, w: ticks required, n: next node | ||
head = { f: null, w: 0, n: null }, tail = head, | ||
// f: task, w: needed a tick, n: next node | ||
head = { f: null, w: false, n: null }, tail = head, | ||
// vars for tick re-usage | ||
lastIsSafe = false, pendingTicks = 0, neededTicks = 0, | ||
nextNeedsTick = true, pendingTicks = 0, neededTicks = 0, | ||
@@ -33,3 +33,5 @@ channel, // MessageChannel | ||
head = head.n; | ||
neededTicks -= head.w; | ||
if ( head.w ) { | ||
--neededTicks; | ||
} | ||
var f = head.f; | ||
@@ -39,14 +41,12 @@ head.f = null; | ||
} | ||
lastIsSafe = false; | ||
nextNeedsTick = true; | ||
} | ||
function runLater( f, couldThrow ) { | ||
var w = 0; | ||
if ( !lastIsSafe && ++neededTicks > pendingTicks ) { | ||
if ( nextNeedsTick && ++neededTicks > pendingTicks ) { | ||
++pendingTicks; | ||
requestTick( onTick, 0 ); | ||
w = 1; | ||
}; | ||
tail = tail.n = { f: f, w: w, n: null }; | ||
lastIsSafe = couldThrow !== true; | ||
tail = tail.n = { f: f, w: nextNeedsTick, n: null }; | ||
nextNeedsTick = couldThrow === true; | ||
} | ||
@@ -240,15 +240,18 @@ | ||
function all( promises ) { | ||
var countDown = promises.length; | ||
if ( countDown === 0 ) { | ||
return resolve( promises ); | ||
} | ||
var waiting = 0; | ||
var def = defer(); | ||
each(promises, function( promise, index ) { | ||
resolve( promise ).then(function( value ) { | ||
promises[ index ] = value; | ||
if ( --countDown === 0 ) { | ||
def.fulfill( promises ); | ||
} | ||
}, def.reject ); | ||
if ( index in promises ) { | ||
++waiting; | ||
resolve( promise ).then(function( value ) { | ||
promises[ index ] = value; | ||
if ( --waiting === 0 ) { | ||
def.fulfill( promises ); | ||
} | ||
}, def.reject ); | ||
} | ||
}); | ||
if ( waiting === 0 ) { | ||
def.fulfill( promises ); | ||
} | ||
return def.promise; | ||
@@ -255,0 +258,0 @@ } |
@@ -1,1 +0,1 @@ | ||
(function(factory){if(typeof module!=="undefined"&&module&&module.exports){module.exports=factory()}else if(typeof define!=="function"){define(factory)}else{P=factory()}})(function(){"use strict";var head={f:null,w:0,n:null},tail=head,lastIsSafe=false,pendingTicks=0,neededTicks=0,channel,requestTick,wow=ot(typeof window)&&window||ot(typeof worker)&&worker;function onTick(){--pendingTicks;while(head.n){head=head.n;neededTicks-=head.w;var f=head.f;head.f=null;f()}lastIsSafe=false}function runLater(f,couldThrow){var w=0;if(!lastIsSafe&&++neededTicks>pendingTicks){++pendingTicks;requestTick(onTick,0);w=1}tail=tail.n={f:f,w:w,n:null};lastIsSafe=couldThrow!==true}function ot(type){return type==="object"||type==="function"}function ft(type){return type==="function"}if(ot(typeof process)&&process&&ft(typeof process.nextTick)){requestTick=process.nextTick}else if(wow&&ft(typeof wow.setImmediate)){requestTick=function(cb){wow.setImmediate(cb)}}else if(ft(typeof MessageChannel)){channel=new MessageChannel;channel.port1.onmessage=onTick;requestTick=function(){channel.port2.postMessage(0)}}else{requestTick=setTimeout;if(wow&&ot(typeof Image)&&Image){(function(){var c=0;var requestTickViaImage=function(cb){var img=new Image;img.onerror=cb;img.src="data:image/png,"};try{requestTickViaImage(function(){if(--c===0){requestTick=requestTickViaImage}});++c}catch(e){}c&&setTimeout(function(){c=0},0)})()}}function each(arr,cb){for(var i=0,l=arr.length;i<l;++i){cb(arr[i],i)}}var P=resolve;P.prototype=Promise.prototype;P.defer=defer;function defer(){var pending=[],rejected=false,value;function then(onFulfilled,onRejected){var def=defer();function onReslved(){var func=rejected?onRejected:onFulfilled;if(typeof func!=="function"){if(rejected){def.reject(value)}else{def.fulfill(value)}}else{var val;try{val=func(value);if(val&&typeof val.then==="function"){val.then(def.fulfill,def.reject);return}}catch(ex){def.reject(ex)}def.fulfill(val)}}if(pending){pending.push(onReslved)}else{runLater(onReslved)}return def.promise}function fulfill(val){if(pending){value=val;each(pending,runLater);pending=null}}function reject(error){if(pending){rejected=true;fulfill(error)}}return{promise:new Promise(then),fulfill:fulfill,reject:reject}}function Promise(then){this.then=then}Promise.prototype.done=function(cb,eb){var p=this;if(cb||eb){p=p.then(cb,eb)}p.then(null,function(error){runLater(function(){if(P.onerror){P.onerror(error)}else{throw error}},true)})};Promise.prototype.spread=function(cb,eb){return this.then(cb&&function(values){return cb.apply(void 0,values)},eb)};P.resolve=resolve;function resolve(val){if(val instanceof Promise){return val}var def=defer();if(val&&typeof val.then==="function"){val.then(def.fulfill,def.reject)}else{def.fulfill(val)}return def.promise}P.reject=reject;function reject(value){var def=defer();def.reject(value);return def.promise}P.all=all;function all(promises){var countDown=promises.length;if(countDown===0){return resolve(promises)}var def=defer();each(promises,function(promise,index){resolve(promise).then(function(value){promises[index]=value;if(--countDown===0){def.fulfill(promises)}},def.reject)});return def.promise}P.promise=function(makeOrPromise){var def=defer();resolve(makeOrPromise).then(function(make){try{make(def.fulfill,def.reject)}catch(ex){def.reject(ex)}},def.reject);return def.promise};P.onerror=null;P.nextTick=function(f){runLater(f,true)};return P}); | ||
(function(e){if(typeof module!=="undefined"&&module&&module.exports){module.exports=e()}else if(typeof define!=="function"){define(e)}else{P=e()}})(function(){"use strict";var e={f:null,w:false,n:null},n=e,t=true,r=0,f=0,o,i,u=s(typeof window)&&window||s(typeof worker)&&worker;function c(){--r;while(e.n){e=e.n;if(e.w){--f}var n=e.f;e.f=null;n()}t=true}function l(e,o){if(t&&++f>r){++r;i(c,0)}n=n.n={f:e,w:t,n:null};t=o===true}function s(e){return e==="object"||e==="function"}function p(e){return e==="function"}if(s(typeof process)&&process&&p(typeof process.nextTick)){i=process.nextTick}else if(u&&p(typeof u.setImmediate)){i=function(e){u.setImmediate(e)}}else if(p(typeof MessageChannel)){o=new MessageChannel;o.port1.onmessage=c;i=function(){o.port2.postMessage(0)}}else{i=setTimeout;if(u&&s(typeof Image)&&Image){(function(){var e=0;var n=function(e){var n=new Image;n.onerror=e;n.src="data:image/png,"};try{n(function(){if(--e===0){i=n}});++e}catch(t){}e&&setTimeout(function(){e=0},0)})()}}function a(e,n){for(var t=0,r=e.length;t<r;++t){n(e[t],t)}}var h=v;h.prototype=y.prototype;h.defer=m;function m(){var e=[],n=false,t;function r(r,f){var o=m();function i(){var e=n?f:r;if(typeof e!=="function"){if(n){o.reject(t)}else{o.fulfill(t)}}else{var i;try{i=e(t);if(i&&typeof i.then==="function"){i.then(o.fulfill,o.reject);return}}catch(u){o.reject(u)}o.fulfill(i)}}if(e){e.push(i)}else{l(i)}return o.promise}function f(n){if(e){t=n;a(e,l);e=null}}function o(t){if(e){n=true;f(t)}}return{promise:new y(r),fulfill:f,reject:o}}function y(e){this.then=e}y.prototype.done=function(e,n){var t=this;if(e||n){t=t.then(e,n)}t.then(null,function(e){l(function(){if(h.onerror){h.onerror(e)}else{throw e}},true)})};y.prototype.spread=function(e,n){return this.then(e&&function(n){return e.apply(void 0,n)},n)};h.resolve=v;function v(e){if(e instanceof y){return e}var n=m();if(e&&typeof e.then==="function"){e.then(n.fulfill,n.reject)}else{n.fulfill(e)}return n.promise}h.reject=d;function d(e){var n=m();n.reject(e);return n.promise}h.all=w;function w(e){var n=0;var t=m();a(e,function(r,f){if(f in e){++n;v(r).then(function(r){e[f]=r;if(--n===0){t.fulfill(e)}},t.reject)}});if(n===0){t.fulfill(e)}return t.promise}h.promise=function(e){var n=m();v(e).then(function(e){try{e(n.fulfill,n.reject)}catch(t){n.reject(t)}},n.reject);return n.promise};h.onerror=null;h.nextTick=function(e){l(e,true)};return h}); |
{ | ||
"name": "p-promise", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Simple promise-a library.", | ||
"main": "p.js", | ||
"scripts": { | ||
"test": "promises-aplus-tests test/adapter", | ||
"prepublish": "uglifyjs p.js > p.min.js" | ||
"pretest": "uglifyjs p.js -o p.min.js -m", | ||
"test": "promises-aplus-tests test/adapter && promises-aplus-tests test/adapter-min" | ||
}, | ||
@@ -10,0 +10,0 @@ "keywords": [ |
@@ -7,4 +7,4 @@ [![Build Status](https://secure.travis-ci.org/rkatic/p.png)](http://travis-ci.org/rkatic/p) | ||
- A subset of the [Q](/kriskowal/q) library. | ||
- Passing the [Promises/A+ Compliance Test Suite](/promises-aplus/promises-tests). | ||
- A subset of the [Q](https://github.com/kriskowal/q) library. | ||
- Passing the [Promises/A+ Compliance Test Suite](https://github.com/promises-aplus/promises-tests). | ||
- Small. | ||
@@ -15,3 +15,3 @@ - Fast. | ||
P is a subset of [Q](/kriskowal/q). | ||
P is a subset of [Q](https://github.com/kriskowal/q). | ||
@@ -18,0 +18,0 @@ - `P(val)` same as `P.resolve(val)` |
9
251
9824