Comparing version 0.1.4 to 0.2.0
336
p.js
@@ -18,11 +18,7 @@ ;(function( factory ){ | ||
var | ||
// linked list with head node - used as a queue of tasks | ||
// f: task, w: needed a tick, n: next node | ||
head = { f: null, w: false, n: null }, tail = head, | ||
head = { f: null, n: null }, tail = head, | ||
running = false, | ||
// vars for tick re-usage | ||
nextNeedsTick = true, pendingTicks = 0, neededTicks = 0, | ||
channel, // MessageChannel | ||
requestTick, // requestTick( onTick, 0 ) is the only valid usage! | ||
requestTick, // --> requestTick( onTick, 0 ) | ||
@@ -32,12 +28,8 @@ // window or worker | ||
toStr = head.toString, | ||
toStr = ({}).toString, | ||
isArray; | ||
function onTick() { | ||
--pendingTicks; | ||
while ( head.n ) { | ||
head = head.n; | ||
if ( head.w ) { | ||
--neededTicks; | ||
} | ||
var f = head.f; | ||
@@ -47,12 +39,11 @@ head.f = null; | ||
} | ||
nextNeedsTick = true; | ||
running = false; | ||
} | ||
var runLater = function( f, couldThrow ) { | ||
if ( nextNeedsTick && ++neededTicks > pendingTicks ) { | ||
++pendingTicks; | ||
var runLater = function( f ) { | ||
tail = tail.n = { f: f, n: null }; | ||
if ( !running ) { | ||
running = true; | ||
requestTick( onTick, 0 ); | ||
}; | ||
tail = tail.n = { f: f, w: nextNeedsTick, n: null }; | ||
nextNeedsTick = couldThrow === true; | ||
} | ||
}; | ||
@@ -69,2 +60,3 @@ | ||
if ( ft(typeof setImmediate) ) { | ||
//runLater = wow ? | ||
requestTick = wow ? | ||
@@ -122,11 +114,2 @@ function( cb ) { | ||
function reportError( error ) { | ||
runLater(function() { | ||
if ( P.onerror ) { | ||
P.onerror( error ); | ||
} else { | ||
throw error; | ||
} | ||
}, true); | ||
} | ||
@@ -156,96 +139,66 @@ isArray = Array.isArray || function( val ) { | ||
function P( val ) { | ||
if ( val instanceof Promise ) { | ||
return val; | ||
function reportError( error ) { | ||
try { | ||
if ( P.onerror ) { | ||
P.onerror( error ); | ||
} else { | ||
throw error; | ||
} | ||
} catch ( e ) { | ||
setTimeout(function() { | ||
throw e; | ||
}, 0); | ||
} | ||
var def = defer(); | ||
def.resolve( val ); | ||
return def.promise; | ||
} | ||
var CHECK = {}; | ||
var RESOLVE = 0; | ||
var FULFILL = 1; | ||
var REJECT = 2; | ||
var PENDING = 0; | ||
var FULFILLED = 1; | ||
var REJECTED = 2; | ||
P.defer = defer; | ||
function defer() { | ||
var pending = [], | ||
validToken = 0, | ||
testToken = 0, | ||
fulfilled = false, | ||
value; | ||
function P( x ) { | ||
return x instanceof Promise ? | ||
x : | ||
Resolve( new Promise(), x ); | ||
} | ||
function H( action ) { | ||
var token = validToken; | ||
return function( x ) { | ||
testToken = token; | ||
resolve( x, CHECK, action ); | ||
}; | ||
function Settle( p, state, value ) { | ||
if ( p._state ) { | ||
return p; | ||
} | ||
function then( onFulfilled, onRejected, _done, _sync ) { | ||
var def = _done === CHECK ? void 0 : defer(); | ||
p._state = state; | ||
p._value = value; | ||
function onSettled() { | ||
var func = fulfilled ? onFulfilled : onRejected; | ||
forEach( p._pending, runLater ); | ||
p._pending = null; | ||
if ( typeof func === "function" ) { | ||
try { | ||
var res = func( value ); | ||
return p; | ||
} | ||
} catch ( ex ) { | ||
def ? def.reject( ex ) : reportError( ex ); | ||
return; | ||
} | ||
function Append( p, f ) { | ||
p._pending.push( f ); | ||
//p._tail = p._tail.n = { f: f, n: null }; | ||
} | ||
def && def.resolve( res ); | ||
function Resolve( p, x ) { | ||
if ( p._state ) { | ||
return p; | ||
} | ||
} else if ( def ) { | ||
def.resolve( value, CHECK, fulfilled ? FULFILL : REJECT ); | ||
if ( x instanceof Promise ) { | ||
if ( x._state ) { | ||
Settle( p, x._state, x._value ); | ||
} else if ( !fulfilled ) { | ||
reportError( value ); | ||
} | ||
} | ||
if ( pending ) { | ||
pending.push( onSettled ); | ||
} else if ( _sync === CHECK ) { | ||
onSettled(); | ||
} else { | ||
runLater( onSettled ); | ||
Append(x, function() { | ||
Settle( p, x._state, x._value ); | ||
}); | ||
} | ||
return def && def.promise; | ||
} | ||
} else if ( x !== Object(x) ) { | ||
Settle( p, FULFILLED, x ); | ||
function resolve( x, _check, _action ) { | ||
if ( testToken !== validToken ) { | ||
return; | ||
} | ||
++validToken; | ||
_action = _check === CHECK && _action; | ||
if ( _action || x !== Object(x) ) { | ||
fulfilled = _action !== REJECT; | ||
value = x; | ||
forEach( pending, runLater ); | ||
pending = null; | ||
return; | ||
} | ||
if ( x instanceof Promise ) { | ||
x.then( H(FULFILL), H(REJECT), CHECK, CHECK ); | ||
return; | ||
} | ||
} else { | ||
runLater(function() { | ||
var action = 0; | ||
try { | ||
@@ -255,34 +208,101 @@ var then = x.then; | ||
if ( typeof then === "function" ) { | ||
then.call( x, H(RESOLVE), H(REJECT) ); | ||
var r = resolverFor( p, x ); | ||
then.call( x, r.resolve, r.reject ); | ||
} else { | ||
action = FULFILL; | ||
Settle( p, FULFILLED, x ); | ||
} | ||
} catch ( ex ) { | ||
x = ex; | ||
action = REJECT; | ||
} catch ( e ) { | ||
Settle( p, REJECTED, e ); | ||
} | ||
if ( action ) { | ||
testToken = validToken; | ||
resolve( x, CHECK, action ); | ||
} | ||
}); | ||
} | ||
return p; | ||
} | ||
function resolverFor( promise, x ) { | ||
var done = false; | ||
return { | ||
promise: new Promise( then ), | ||
resolve: resolve, | ||
reject: H(REJECT) | ||
promise: promise, | ||
resolve: function( y ) { | ||
if ( !done ) { | ||
done = true; | ||
if ( x && x === y ) { | ||
Settle( promise, FULFILLED, y ); | ||
} else { | ||
Resolve( promise, y ); | ||
} | ||
} | ||
}, | ||
reject: function( reason ) { | ||
if ( !done ) { | ||
done = true; | ||
Settle( promise, REJECTED, reason ); | ||
} | ||
} | ||
}; | ||
} | ||
P.defer = defer; | ||
function defer() { | ||
return resolverFor( new Promise() ); | ||
} | ||
function Promise( then ) { | ||
this.then = then; | ||
function Promise() { | ||
this._state = 0; | ||
this._value = void 0; | ||
this._pending = []; | ||
} | ||
Promise.prototype.then = function( onFulfilled, onRejected ) { | ||
var cb = typeof onFulfilled === "function" ? onFulfilled : null; | ||
var eb = typeof onRejected === "function" ? onRejected : null; | ||
var p = this; | ||
var p2 = new Promise(); | ||
function onSettled() { | ||
var x, func = p._state === FULFILLED ? cb : eb; | ||
if ( func !== null ) { | ||
try { | ||
x = func( p._value ); | ||
} catch ( e ) { | ||
Settle( p2, REJECTED, e ); | ||
return; | ||
} | ||
Resolve( p2, x ); | ||
} else { | ||
Settle( p2, p._state, p._value ); | ||
} | ||
} | ||
if ( p._state === PENDING ) { | ||
Append( p, onSettled ); | ||
} else { | ||
runLater( onSettled ); | ||
} | ||
return p2; | ||
}; | ||
Promise.prototype.done = function( cb, eb ) { | ||
this.then( cb, eb, CHECK ); | ||
var p = this; | ||
if ( cb || eb ) { | ||
p = p.then( cb, eb ); | ||
} | ||
p.then( null, reportError ); | ||
}; | ||
@@ -298,26 +318,31 @@ | ||
Promise.prototype.timeout = function( ms ) { | ||
var def = defer(); | ||
var timeoutId = setTimeout(function() { | ||
def.reject( new Error("Timed out after " + ms + " ms") ); | ||
}, ms); | ||
Promise.prototype.timeout = function( ms, msg ) { | ||
var p = this; | ||
var p2 = new Promise(); | ||
this.then(function( value ) { | ||
clearTimeout( timeoutId ); | ||
def.resolve( value ); | ||
}, function( error ) { | ||
clearTimeout( timeoutId ); | ||
def.reject( error ); | ||
}, CHECK, CHECK); | ||
if ( p._state !== PENDING ) { | ||
Settle( p2, p._state, p._value ); | ||
return def.promise; | ||
} else { | ||
var timeoutId = setTimeout(function() { | ||
Settle( p2, REJECTED, | ||
new Error(msg || "Timed out after " + ms + " ms") ); | ||
}, ms); | ||
Append(p, function() { | ||
clearTimeout( timeoutId ); | ||
Settle( p2, p._state, p._value ); | ||
}); | ||
} | ||
return p2; | ||
}; | ||
Promise.prototype.delay = function( ms ) { | ||
var self = this; | ||
var def = defer(); | ||
var p = this; | ||
var p2 = new Promise(); | ||
setTimeout(function() { | ||
def.resolve( self ); | ||
Resolve( p2, p ); | ||
}, ms); | ||
return def.promise; | ||
return p2; | ||
}; | ||
@@ -328,16 +353,22 @@ | ||
var waiting = 0; | ||
var def = defer(); | ||
var d = defer(); | ||
each( promises, function( promise, index ) { | ||
++waiting; | ||
P( promise ).then(function( value ) { | ||
promises[ index ] = value; | ||
if ( --waiting === 0 ) { | ||
def.resolve( promises ); | ||
} | ||
}, def.reject, CHECK ); | ||
var p = P( promise ); | ||
if ( p._state === PENDING ) { | ||
++waiting; | ||
p.then(function( value ) { | ||
promises[ index ] = value; | ||
if ( --waiting === 0 ) { | ||
d.resolve( promises ); | ||
} | ||
}, d.reject); | ||
} else { | ||
promises[ index ] = p._value; | ||
} | ||
}); | ||
if ( waiting === 0 ) { | ||
def.resolve( promises ); | ||
d.resolve( promises ); | ||
} | ||
return def.promise; | ||
return d.promise; | ||
} | ||
@@ -350,3 +381,12 @@ | ||
P.nextTick = function( f ) { | ||
runLater( f, true ); | ||
runLater(function() { | ||
try { | ||
f(); | ||
} catch ( ex ) { | ||
setTimeout(function() { | ||
throw ex; | ||
}, 0); | ||
} | ||
}); | ||
}; | ||
@@ -353,0 +393,0 @@ |
@@ -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=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}); | ||
(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,n:null},t=e,n=false,r,o,i=s(typeof window)&&window||s(typeof worker)&&worker,u={}.toString,f;function a(){while(e.n){e=e.n;var t=e.f;e.f=null;t()}n=false}var c=function(e){t=t.n={f:e,n:null};if(!n){n=true;o(a,0)}};function s(e){return e==="object"||e==="function"}function l(e){return e==="function"}if(l(typeof setImmediate)){o=i?function(e){i.setImmediate(e)}:function(e){setImmediate(e)}}else if(s(typeof process)&&process&&l(typeof process.nextTick)){o=process.nextTick}else if(l(typeof MessageChannel)){r=new MessageChannel;r.port1.onmessage=a;o=function(){r.port2.postMessage(0)}}else{o=setTimeout;if(i&&s(typeof Image)&&Image){(function(){var e=0;var t=function(e){var t=new Image;t.onerror=e;t.src="data:image/png,"};try{t(function(){if(--e===0){o=t}});++e}catch(n){}e&&setTimeout(function(){e=0},0)})()}}f=Array.isArray||function(e){return!!e&&u.call(e)==="[object Array]"};function p(e,t){for(var n=0,r=e.length;n<r;++n){if(n in e){t(e[n],n)}}}function v(e,t){if(f(e)){p(e,t);return}for(var n in e){t(e[n],n)}}function h(e){try{if(d.onerror){d.onerror(e)}else{throw e}}catch(t){setTimeout(function(){throw t},0)}}var y=0;var m=1;var _=2;function d(e){return e instanceof k?e:T(new k,e)}function w(e,t,n){if(e._state){return e}e._state=t;e._value=n;p(e._pending,c);e._pending=null;return e}function g(e,t){e._pending.push(t)}function T(e,t){if(e._state){return e}if(t instanceof k){if(t._state){w(e,t._state,t._value)}else{g(t,function(){w(e,t._state,t._value)})}}else if(t!==Object(t)){w(e,m,t)}else{c(function(){try{var n=t.then;if(typeof n==="function"){var r=j(e,t);n.call(t,r.resolve,r.reject)}else{w(e,m,t)}}catch(o){w(e,_,o)}})}return e}function j(e,t){var n=false;return{promise:e,resolve:function(r){if(!n){n=true;if(t&&t===r){w(e,m,r)}else{T(e,r)}}},reject:function(t){if(!n){n=true;w(e,_,t)}}}}d.defer=I;function I(){return j(new k)}function k(){this._state=0;this._value=void 0;this._pending=[]}k.prototype.then=function(e,t){var n=typeof e==="function"?e:null;var r=typeof t==="function"?t:null;var o=this;var i=new k;function u(){var e,t=o._state===m?n:r;if(t!==null){try{e=t(o._value)}catch(u){w(i,_,u);return}T(i,e)}else{w(i,o._state,o._value)}}if(o._state===y){g(o,u)}else{c(u)}return i};k.prototype.done=function(e,t){var n=this;if(e||t){n=n.then(e,t)}n.then(null,h)};k.prototype.spread=function(e,t){return this.then(e&&function(t){return x(t).then(function(t){return e.apply(void 0,t)})},t)};k.prototype.timeout=function(e,t){var n=this;var r=new k;if(n._state!==y){w(r,n._state,n._value)}else{var o=setTimeout(function(){w(r,_,new Error(t||"Timed out after "+e+" ms"))},e);g(n,function(){clearTimeout(o);w(r,n._state,n._value)})}return r};k.prototype.delay=function(e){var t=this;var n=new k;setTimeout(function(){T(n,t)},e);return n};d.all=x;function x(e){var t=0;var n=I();v(e,function(r,o){var i=d(r);if(i._state===y){++t;i.then(function(r){e[o]=r;if(--t===0){n.resolve(e)}},n.reject)}else{e[o]=i._value}});if(t===0){n.resolve(e)}return n.promise}d.onerror=null;d.prototype=k.prototype;d.nextTick=function(e){c(function(){try{e()}catch(t){setTimeout(function(){throw t},0)}})};d._each=v;return d}); |
{ | ||
"name": "p-promise", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "A simple Promises/A+ library.", | ||
@@ -5,0 +5,0 @@ "author": "Robert Katić <robert.katic@gmail.com> (https://github.com/rkatic)", |
[![Build Status](https://travis-ci.org/rkatic/p.png?branch=master)](https://travis-ci.org/rkatic/p) | ||
<a href="http://promises-aplus.github.com/promises-spec"> | ||
<img src="http://promises-aplus.github.com/promises-spec/assets/logo-small.png" | ||
alt="Promises/A+ logo" title="Promises/A+ 1.0 compliant" /> | ||
</a> | ||
#P | ||
@@ -11,3 +16,4 @@ | ||
- Small. | ||
- [Fast](http://jsperf.com/wqfwewefewrw). | ||
- Simple. | ||
- [Ultra Fast](http://jsperf.com/wqfwewefewrw/3). | ||
@@ -14,0 +20,0 @@ ##API |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
16203
466
36
0
1