Comparing version 0.1.3 to 0.1.4
139
p.js
@@ -32,24 +32,14 @@ ;(function( factory ){ | ||
toStr = head.toString, | ||
isArray, | ||
isArray; | ||
ALT = {}; | ||
function onTick() { | ||
--pendingTicks; | ||
if ( head.n ) { | ||
if ( !pendingTicks && head.n.n ) { | ||
// In case of multiple tasks, ensure at least one successive tick | ||
// to handle remaining task in case one throws, even if specified it will not. | ||
++pendingTicks; | ||
requestTick( onTick, 0 ); | ||
while ( head.n ) { | ||
head = head.n; | ||
if ( head.w ) { | ||
--neededTicks; | ||
} | ||
do { | ||
head = head.n; | ||
if ( head.w ) { | ||
--neededTicks; | ||
} | ||
var f = head.f; | ||
head.f = null; | ||
f(); | ||
} while ( head.n ) | ||
var f = head.f; | ||
head.f = null; | ||
f(); | ||
} | ||
@@ -139,12 +129,2 @@ nextNeedsTick = true; | ||
function T( cb, tt ) { | ||
var t = tt.t; | ||
return function( a, b, c ) { | ||
if ( t === tt.t ) { | ||
++tt.t; | ||
cb( a, b, c ); | ||
} | ||
}; | ||
} | ||
isArray = Array.isArray || function( val ) { | ||
@@ -183,12 +163,26 @@ return !!val && toStr.call( val ) === "[object Array]"; | ||
var CHECK = {}; | ||
var RESOLVE = 0; | ||
var FULFILL = 1; | ||
var REJECT = 2; | ||
P.defer = defer; | ||
function defer() { | ||
var pending = [], | ||
tt = {t: 0}, | ||
validToken = 0, | ||
testToken = 0, | ||
fulfilled = false, | ||
value; | ||
function then( onFulfilled, onRejected, alt, sync ) { | ||
var def = alt === ALT ? void 0 : defer(); | ||
function H( action ) { | ||
var token = validToken; | ||
return function( x ) { | ||
testToken = token; | ||
resolve( x, CHECK, action ); | ||
}; | ||
} | ||
function then( onFulfilled, onRejected, _done, _sync ) { | ||
var def = _done === CHECK ? void 0 : defer(); | ||
function onSettled() { | ||
@@ -209,3 +203,3 @@ var func = fulfilled ? onFulfilled : onRejected; | ||
} else if ( def ) { | ||
def.resolve( value, ALT, fulfilled ); | ||
def.resolve( value, CHECK, fulfilled ? FULFILL : REJECT ); | ||
@@ -220,3 +214,3 @@ } else if ( !fulfilled ) { | ||
} else if ( !def && sync ) { | ||
} else if ( _sync === CHECK ) { | ||
onSettled(); | ||
@@ -232,49 +226,52 @@ | ||
function resolve( x, alt, ok ) { | ||
if ( pending ) { | ||
function resolve( x, _check, _action ) { | ||
if ( testToken !== validToken ) { | ||
return; | ||
} | ||
if ( alt === ALT ) { // settle | ||
fulfilled = !!ok; | ||
value = x; | ||
forEach( pending, runLater ); | ||
pending = null; | ||
++validToken; | ||
_action = _check === CHECK && _action; | ||
} else if ( x instanceof Promise ) { | ||
x.then( fulfill, reject, ALT, true ); | ||
if ( _action || x !== Object(x) ) { | ||
fulfilled = _action !== REJECT; | ||
value = x; | ||
forEach( pending, runLater ); | ||
pending = null; | ||
return; | ||
} | ||
} else if ( x === Object(x) ) { | ||
runLater(function() { | ||
try { | ||
var then = x.then; | ||
if ( x instanceof Promise ) { | ||
x.then( H(FULFILL), H(REJECT), CHECK, CHECK ); | ||
return; | ||
} | ||
if ( typeof then === "function" ) { | ||
then.call( x, T(resolve, tt), T(reject, tt) ); | ||
runLater(function() { | ||
var action = 0; | ||
} else { | ||
fulfill( x ); | ||
} | ||
try { | ||
var then = x.then; | ||
} catch ( ex ) { | ||
reject( ex ); | ||
} | ||
}); | ||
if ( typeof then === "function" ) { | ||
then.call( x, H(RESOLVE), H(REJECT) ); | ||
} else { | ||
fulfill( x ); | ||
} else { | ||
action = FULFILL; | ||
} | ||
} catch ( ex ) { | ||
x = ex; | ||
action = REJECT; | ||
} | ||
} | ||
} | ||
function fulfill( value ) { | ||
resolve( value, ALT, true ); | ||
if ( action ) { | ||
testToken = validToken; | ||
resolve( x, CHECK, action ); | ||
} | ||
}); | ||
} | ||
function reject( reson ) { | ||
resolve( reson, ALT, false ); | ||
} | ||
return { | ||
promise: new Promise( then ), | ||
resolve: T(resolve, tt), | ||
reject: T(reject, tt) | ||
resolve: resolve, | ||
reject: H(REJECT) | ||
}; | ||
@@ -289,3 +286,3 @@ } | ||
Promise.prototype.done = function( cb, eb ) { | ||
this.then( cb, eb, ALT ); | ||
this.then( cb, eb, CHECK ); | ||
}; | ||
@@ -313,3 +310,3 @@ | ||
def.reject( error ); | ||
}, ALT, true); | ||
}, CHECK, CHECK); | ||
@@ -339,3 +336,3 @@ return def.promise; | ||
} | ||
}, def.reject, ALT ); | ||
}, def.reject, CHECK ); | ||
}); | ||
@@ -358,5 +355,3 @@ if ( waiting === 0 ) { | ||
//P.runLater = runLater; | ||
return P; | ||
}); |
@@ -1,1 +0,1 @@ | ||
(function(e){if(typeof module!=="undefined"&&module&&module.exports){module.exports=e()}else if(typeof define==="function"&&define.amd){define(e)}else{P=e()}})(function(){"use strict";var e={f:null,w:false,n:null},n=e,t=true,r=0,o=0,i,f,u=m(typeof window)&&window||m(typeof worker)&&worker,c=e.toString,s,a={};function l(){--r;if(e.n){if(!r&&e.n.n){++r;f(l,0)}do{e=e.n;if(e.w){--o}var n=e.f;e.f=null;n()}while(e.n)}t=true}var p=function(e,i){if(t&&++o>r){++r;f(l,0)}n=n.n={f:e,w:t,n:null};t=i===true};function m(e){return e==="object"||e==="function"}function v(e){return e==="function"}if(v(typeof setImmediate)){f=u?function(e){u.setImmediate(e)}:function(e){setImmediate(e)}}else if(m(typeof process)&&process&&v(typeof process.nextTick)){f=process.nextTick}else if(v(typeof MessageChannel)){i=new MessageChannel;i.port1.onmessage=l;f=function(){i.port2.postMessage(0)}}else{f=setTimeout;if(u&&m(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){f=n}});++e}catch(t){}e&&setTimeout(function(){e=0},0)})()}}function y(e){p(function(){if(g.onerror){g.onerror(e)}else{throw e}},true)}function d(e,n){var t=n.t;return function(r,o,i){if(t===n.t){++n.t;e(r,o,i)}}}s=Array.isArray||function(e){return!!e&&c.call(e)==="[object Array]"};function h(e,n){for(var t=0,r=e.length;t<r;++t){if(t in e){n(e[t],t)}}}function w(e,n){if(s(e)){h(e,n);return}for(var t in e){n(e[t],t)}}function g(e){if(e instanceof j){return e}var n=T();n.resolve(e);return n.promise}g.defer=T;function T(){var e=[],n={t:0},t=false,r;function o(n,o,i,f){var u=i===a?void 0:T();function c(){var e=t?n:o;if(typeof e==="function"){try{var i=e(r)}catch(f){u?u.reject(f):y(f);return}u&&u.resolve(i)}else if(u){u.resolve(r,a,t)}else if(!t){y(r)}}if(e){e.push(c)}else if(!u&&f){c()}else{p(c)}return u&&u.promise}function i(o,c,s){if(e){if(c===a){t=!!s;r=o;h(e,p);e=null}else if(o instanceof j){o.then(f,u,a,true)}else if(o===Object(o)){p(function(){try{var e=o.then;if(typeof e==="function"){e.call(o,d(i,n),d(u,n))}else{f(o)}}catch(t){u(t)}})}else{f(o)}}}function f(e){i(e,a,true)}function u(e){i(e,a,false)}return{promise:new j(o),resolve:d(i,n),reject:d(u,n)}}function j(e){this.then=e}j.prototype.done=function(e,n){this.then(e,n,a)};j.prototype.spread=function(e,n){return this.then(e&&function(n){return I(n).then(function(n){return e.apply(void 0,n)})},n)};j.prototype.timeout=function(e){var n=T();var t=setTimeout(function(){n.reject(new Error("Timed out after "+e+" ms"))},e);this.then(function(e){clearTimeout(t);n.resolve(e)},function(e){clearTimeout(t);n.reject(e)},a,true);return n.promise};j.prototype.delay=function(e){var n=this;var t=T();setTimeout(function(){t.resolve(n)},e);return t.promise};g.all=I;function I(e){var n=0;var t=T();w(e,function(r,o){++n;g(r).then(function(r){e[o]=r;if(--n===0){t.resolve(e)}},t.reject,a)});if(n===0){t.resolve(e)}return t.promise}g.onerror=null;g.prototype=j.prototype;g.nextTick=function(e){p(e,true)};g._each=w;return g}); | ||
(function(e){if(typeof module!=="undefined"&&module&&module.exports){module.exports=e()}else if(typeof define==="function"&&define.amd){define(e)}else{P=e()}})(function(){"use strict";var e={f:null,w:false,n:null},n=e,t=true,r=0,o=0,i,f,u=p(typeof window)&&window||p(typeof worker)&&worker,c=e.toString,s;function a(){--r;while(e.n){e=e.n;if(e.w){--o}var n=e.f;e.f=null;n()}t=true}var l=function(e,i){if(t&&++o>r){++r;f(a,0)}n=n.n={f:e,w:t,n:null};t=i===true};function p(e){return e==="object"||e==="function"}function v(e){return e==="function"}if(v(typeof setImmediate)){f=u?function(e){u.setImmediate(e)}:function(e){setImmediate(e)}}else if(p(typeof process)&&process&&v(typeof process.nextTick)){f=process.nextTick}else if(v(typeof MessageChannel)){i=new MessageChannel;i.port1.onmessage=a;f=function(){i.port2.postMessage(0)}}else{f=setTimeout;if(u&&p(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){f=n}});++e}catch(t){}e&&setTimeout(function(){e=0},0)})()}}function m(e){l(function(){if(h.onerror){h.onerror(e)}else{throw e}},true)}s=Array.isArray||function(e){return!!e&&c.call(e)==="[object Array]"};function y(e,n){for(var t=0,r=e.length;t<r;++t){if(t in e){n(e[t],t)}}}function d(e,n){if(s(e)){y(e,n);return}for(var t in e){n(e[t],t)}}function h(e){if(e instanceof k){return e}var n=I();n.resolve(e);return n.promise}var w={};var g=0;var T=1;var j=2;h.defer=I;function I(){var e=[],n=0,t=0,r=false,o;function i(e){var r=n;return function(n){t=r;u(n,w,e)}}function f(n,t,i,f){var u=i===w?void 0:I();function c(){var e=r?n:t;if(typeof e==="function"){try{var i=e(o)}catch(f){u?u.reject(f):m(f);return}u&&u.resolve(i)}else if(u){u.resolve(o,w,r?T:j)}else if(!r){m(o)}}if(e){e.push(c)}else if(f===w){c()}else{l(c)}return u&&u.promise}function u(f,c,s){if(t!==n){return}++n;s=c===w&&s;if(s||f!==Object(f)){r=s!==j;o=f;y(e,l);e=null;return}if(f instanceof k){f.then(i(T),i(j),w,w);return}l(function(){var e=0;try{var r=f.then;if(typeof r==="function"){r.call(f,i(g),i(j))}else{e=T}}catch(o){f=o;e=j}if(e){t=n;u(f,w,e)}})}return{promise:new k(f),resolve:u,reject:i(j)}}function k(e){this.then=e}k.prototype.done=function(e,n){this.then(e,n,w)};k.prototype.spread=function(e,n){return this.then(e&&function(n){return x(n).then(function(n){return e.apply(void 0,n)})},n)};k.prototype.timeout=function(e){var n=I();var t=setTimeout(function(){n.reject(new Error("Timed out after "+e+" ms"))},e);this.then(function(e){clearTimeout(t);n.resolve(e)},function(e){clearTimeout(t);n.reject(e)},w,w);return n.promise};k.prototype.delay=function(e){var n=this;var t=I();setTimeout(function(){t.resolve(n)},e);return t.promise};h.all=x;function x(e){var n=0;var t=I();d(e,function(r,o){++n;h(r).then(function(r){e[o]=r;if(--n===0){t.resolve(e)}},t.reject,w)});if(n===0){t.resolve(e)}return t.promise}h.onerror=null;h.prototype=k.prototype;h.nextTick=function(e){l(e,true)};h._each=d;return h}); |
{ | ||
"name": "p-promise", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "A simple Promises/A+ library.", | ||
@@ -24,5 +24,5 @@ "author": "Robert Katić <robert.katic@gmail.com> (https://github.com/rkatic)", | ||
"devDependencies": { | ||
"promises-aplus-tests": "*", | ||
"uglify-js": "*" | ||
"promises-aplus-tests": "~1", | ||
"uglify-js": "~2" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
[![Build Status](https://secure.travis-ci.org/rkatic/p.png)](http://travis-ci.org/rkatic/p) | ||
[![Build Status](https://travis-ci.org/rkatic/p.png?branch=master)](https://travis-ci.org/rkatic/p) | ||
@@ -3,0 +3,0 @@ #P |
15443
437