promisesds
Version:
ES6 Promises data structures and utils
1 lines • 7.52 kB
JavaScript
var PromisesDS=function(t){function e(r){if(i[r])return i[r].exports;var n=i[r]={exports:{},id:r,loaded:!1};return t[r].call(n.exports,n,n.exports,e),n.loaded=!0,n.exports}var i={};return e.m=t,e.c=i,e.p="",e(0)}([function(t,e,i){t.exports={LastAction:i(1),OrderedPromises:i(2),PromiseCache:i(3),Sequence:i(4)}},function(t,e){t.exports=function(){"use strict";var t=function(){},e=function(e,i,r){this.onError=i||t,this.onComplete=e||t,this.retries=r||0,this._deferred=null,this.lastAction=null},i=function(){var t,e=new Promise(function(e,i){t={resolve:e,reject:i}});return t.promise=e,t},r=function(t,e,i,r){return i===t._deferred?(t._deferred=null,t._lastResponse=e,r(e)):t._deferred.resolve(e),e},n=function(t,e,i){return e().then(function(e){return r(t,e,i,t.onComplete)},function(e){throw r(t,e,i,t.onError)})},o=function(t,e,i,r){s(t,e).then(function(t){r.resolve(t)},function(n){null===t._deferred&&i>0?o(t,e,i-1,r):r.reject(n)})},s=function(t,e){t.lastAction=e;var r=i();return t._deferred?(t._deferred=r,t._deferred.promise.then(function(i){return n(t,e.bind(null,i),r)})):(t._deferred=r,n(t,e.bind(null,t._lastResponse),r))};return e.prototype={push:function(t,e){e=void 0===e?this.retries:e;var r=i();return o(this,t,e,r),r.promise},lastAction:function(){return this.lastAction}},e}()},function(t,e){t.exports=function(){"use strict";function t(t,e,i,r,n){for(;e.length;){var o=e.shift();if(i===o.promise)return t[n?"_next":"_nextFail"](i,r),void(e.length||t[n?"_last":"_lastFail"](i,r));o.discarded=!0,t._discarded(o.promise)}}var e=function(t,e){var i=this;e=e||{},t=t||[],this._promises=[],this.next(e.next,e.nextFail),this.last(e.last,e.lastFail),this.discarded(e.discarded),t.forEach(function(t){i.push(t)})},i=function(){};return e.prototype={push:function(e){var i=this,r={promise:e,discarded:!1};return this._promises.push(r),e.then(function(e){r.discarded||t(i,i._promises,r.promise,e,!0)},function(e){r.discarded||t(i,i._promises,r.promise,e,!1)}),this},last:function(t,e){return this._last=t||i,this._lastFail=e||i,this},next:function(t,e){return this._next=t||i,this._nextFail=e||i,this},promises:function(){return this._promises.map(function(t){return t.promise})},discarded:function(t){return this._discarded=t||i,this}},e}()},function(t,e){t.exports=function(){"use strict";var t=function(t,e,i,r){r=Boolean(r);var n;t[0]&&(n=t[0][i]);var o=[],s=0;return Object.keys(t).forEach(function(c){var u=t[c];if(r===n<=u[i]||e>s){for(var h=1,a=e>h&&!o[h];a||o[h]&&r===o[h][i]<=u[i];)o[h-1]=o[h],h+=1,a=e>h&&!o[h];o[h-1]={propKey:c},o[h-1][i]=u[i],s+=1,n=o[0]?o[0][i]:n}}),o},e=function(){var e=function(){this.counter=0};return e.prototype.init=function(t){this.cache=t},e.prototype.set=function(t,e){e.lru=0},e.prototype.get=function(t,e){this.counter+=1,e.lru=this.counter},e.prototype.evict=function(e){var i=this.cache,r=t(i._promises,e,"lru");Object.keys(r).forEach(function(t){i.remove(r[t].propKey)})},e}(),i=function(){var e=function(){this.counter=0};return e.prototype.init=function(t){this.cache=t},e.prototype.set=function(t,e){e.mru=0},e.prototype.get=function(t,e){this.counter+=1,e.mru=this.counter},e.prototype.evict=function(e){var i=this.cache,r=t(i._promises,e,"mru",!0);Object.keys(r).forEach(function(t){i.remove(r[t].propKey)})},e}(),r=function(){var e=function(){};return e.prototype.init=function(t){this.cache=t},e.prototype.set=function(t,e){e.lfu=0},e.prototype.get=function(t,e){e.lfu+=1},e.prototype.evict=function(e){var i=this.cache,r=t(i._promises,e,"lfu");Object.keys(r).forEach(function(t){i.remove(r[t].propKey)})},e}(),n={lru:e,mru:i,lfu:r},o=function(){var t,e=new Promise(function(e,i){t={resolve:e,reject:i}});return t.promise=e,t},s=function(){},c=function(t,e){e=e||{},this._promises={},this.length=0;var i;if(e.eviction&&(i=n[e.eviction]?new n[e.eviction]:e.eviction),i=i||{},i.init=i.init||s,i.get=i.get||s,i.set=i.set||s,i.remove=i.remove||s,void 0===e.capacity)i.evict=i.evict||s;else if(!i.evict)throw new Error("There is a capacity but no evict function set");this.eviction=i,this.eviction.init(this,t,e),this.capacity=e.capacity,this.evictRate=e.evictRate||1,this.discarded=e.discarded,this.expireTime=e.expireTime,this.fail=e.fail;var r=this;Object.keys(t||{}).forEach(function(i){r.set(i,t[i],e)})};return c.prototype.set=function(t,e,i){e&&(i=i||{});var r,n=this;if(!e||!e.then)throw new Error("promise: "+e+" is not a Promise");this._promises[t]||(this.length+=1,this.capacity<this.length&&this.evict(this.evictRate));var c,u=i.fail||this.fail;if(u){var h=o();r=h.promise,c={promise:r},this._promises[t]=c,e.then(function(t){h.resolve(t)},function(){u(h,t,e)})}else c={promise:e},this._promises[t]=c,e.then(null,function(){n._promises[t]&&n._promises[t]===c&&this.remove(t)});this._promises[t].discarded=i.discarded||this.discarded||s;var a=void 0!==i.expireTime?i.expireTime:this.expireTime;void 0!==a&&setTimeout(function(){n._promises[t]&&n._promises[t]===c&&n.remove(t)},a),this.eviction.set(t,c,e,i)},c.prototype.remove=function(t){var e=this._promises[t];return void 0!==e?(delete this._promises[t],this.length-=1,this.eviction.remove(t,e),e.discarded(t,e.promise),e.promise):void 0},c.prototype.get=function(t){return this._promises[t]?(this.eviction.get(t,this._promises[t]),this._promises[t].promise):void 0},c.prototype.promises=function(){var t={},e=this._promises;return Object.keys(e).forEach(function(i){t[i]=e[i].promise}),t},c.prototype.evict=function(t){this.eviction.evict(t)},c}()},function(t,e){t.exports=function(){"use strict";var t=function(t){var e=this;if(this.lastPromise=Promise.resolve(),Array.isArray(t))t.forEach(function(t){e.pushObject(t)});else if(void 0!==t)throw new Error("actions (if passed) must be an array")},e=function(){var t,e=new Promise(function(e,i){t={resolve:e,reject:i}});return t.promise=e,t};t.prototype.pushObject=function(t){if(t&&t.call)this.push(t);else if(t.action)this.push(t.action,t.fallback);else if(t.timeout)this.setTimeout(t.timeout,t.duration);else if(t.whenEmpty)this.whenEmpty(t.whenEmpty,t.fallback);else if(t.promise)this.pushPromise(t.promise);else{if(!t.synchronous){var e=new Error("action not recognized "+t);throw e.action=t,e}this.pushSynchronous(t.synchronous,t.fallback)}return this};var i=function(t,e){t&&t.then&&t.then.call&&t.then(function(t){e.resolve(t)},function(t){e.reject(t)})};return t.prototype.push=function(t,r){var n=e(),o=this.lastPromise;return this.lastPromise=n.promise,delete n.promise,o.then(function(e){var r=t(n,e);i(r,n)}),r?o.then(null,function(t){var e=r(n,t);i(e,n)}):o.then(null,function(t){n.reject(t)}),this},t.prototype.pushPromise=function(t){var e=this.lastPromise;return this.push(function(i){e.then(function(){t.then(function(t){i.resolve(t)},function(t){i.reject(t)})},function(e){t.always(function(){i.reject(e)})})}),this},t.prototype.pushSynchronous=function(t,e){return this.push(function(e,i){var r=t(i);e.resolve(r)},function(t,i){var r=e(i);t.resolve(r)}),this},t.prototype.setTimeout=function(t,r){var n=e(),o=!1,s=setTimeout(function(){o=!0;var e=t(n);i(e,n)},r),c=this.lastPromise;this.lastPromise=n.promise;var u=function(t){return function(e){o||(clearTimeout(s),t(e))}};return c.then(u(n.resolve),u(n.reject)),this},t.prototype.whenEmpty=function(t,r){var n=this.lastPromise,o=this,s=function(c){return function(u){if(o.lastPromise===n){var h=e();o.lastPromise=h.promise;var a=c(h,u);i(a,h)}else n=o.lastPromise,n.then(s(t),s(r))}};return n.then(s(t),s(r)),this},t.prototype.promise=function(){return this.lastPromise},t}()}]);