UNPKG

forejs

Version:

A lightweight module which provides powerful functionality to organize asynchronous JavaScript code.

29 lines (24 loc) 10.9 kB
/* Copyright (c) 2017 Lukas Hollaender Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** * A lightweight module which provides powerful functionality to organize asynchronous JavaScript code. * @module foreJs * @version 0.7.3 */ "use strict";!function(t,e,n){function i(t){return"object"==typeof t}try{if(i(module)&&i(module.exports))return module.exports=n()}catch(t){}i(t.exports)?t.exports[e]=n():"function"==typeof t.define&&t.define.amd?t.define(n):t[e]=n()}("object"==typeof global?global:"object"==typeof window?window:this,"fore",function(){function t(t,e){for(var n=-1,i=t.length;++n<i;)e(t[n],n)}function e(t,e){for(var n=-1,i=t.length,o=new Array(i);++n<i;)o[n]=e(t[n],n);return o}function n(t,e){for(var n=-1,i=t.length;++n<i;)t[n]=e(t[n],n);return t}function i(t,e){for(var n=-1,i=t.length;++n<i;)if(!e(t[n],n))return!1;return!0}function o(t){return t}function r(t){"object"==typeof t&&Object.getPrototypeOf(t)===Object.prototype?u(t):a(arguments)}function u(e){var i=Object.getOwnPropertyNames(e),o={};t(i,function(t){o[t]=new l});var r=[];t(i,function(t){var i=[!1],u=p(e[t]),s=b(u),a=H(u),h=o[t];null!==u.injections&&n(u.injections,function(t){return c(o,s,t,i)}),null!==u.thisInjection&&(u.thisInjection=c(o,s,u.thisInjection,i)),s.injector=u,u.executor=a,a.valuePipe=h,i[0]||r.push(u)}),t(r,function(t){t.execute(!0,1)})}function c(t,n,i,o){var r=new w;if(i instanceof x){o[0]=!0;var u,c=i.id;if(c.indexOf("|")>=0){u=new v(n);var a=c.split("|");u.inputPipes=e(a,function(e){var n=s(t,e);return n.register(u),n})}else u=s(t,c),u.register(n);n.valuePipes.push(u),n.valueProviders.push(r)}else r.value=i;return r}function s(t,e){var n=t[e];if(!n)throw new Error("Unbound identifier '"+e+"'.");return n}function a(i){var o=e(i,function(){return new l}),r=null;t(i,function(t,e){var i=p(t);i.isSimpleChain=!0;var u=H(i),c=o[e];if(i.injections&&n(i.injections,function(t){var e=new w;return e.value=t,e}),e>0){var s=b(i),a=o[e-1];a.register(s);var h,f=new w;void 0===i.thisInjection?(i.thisInjection=f,h=f):(f.value=i.thisInjection,h=new w,null===i.injections?i.injections=[h]:i.injections.push(h)),s.valuePipes.push(a),s.valueProviders.push(h),i.thisInjection=f,s.injector=i}else r=i;i.executor=u,u.valuePipe=c}),r.execute(!0,1)}function h(t,e){return t=p(t),t.errorHandler||(t.errorHandler=e),t}function f(t,e){t.errorHandler(e)}function p(t){if(Array.isArray(t)){for(var e=[],n=[],i=-1,o=t.length;++i<o;){var u=t[i];"function"==typeof u||u instanceof P?e.push(u):n.push("string"==typeof u?r.ref(u):u)}var c;c=1===e.length?e[0]:function(){for(var t=arguments.length-1,n=new P(e[0]),i=n.injections=new Array(t),o=-1;++o<t;)i[o]=arguments[o];e[0]=n;var r=arguments[t];e.push(function(t){r(null,t)}),a(e)};var s=new P(c);return s.injections=n,s}return t instanceof P?t:new P(t)}function l(){this.values=[],this.observers=[],this.done=!1,this.reachedLast=!1,this.expectedLength=0,this.failedLength=0}function v(t){this.values=[],this.combinator=t,this.inputPipes=null,this.done=!1}function y(){this.injector=null,this.valuePipes=[],this.valueProviders=[]}function d(){y.call(this)}function j(){y.call(this),this.executionCounter=0}function g(){y.call(this)}function b(t){return t.mode===k.COLLECT?new g:null===t.injections||t.injections.length<=1&&null===t.thisInjection||null!==t.thisInjection&&(null===t.injections||0===t.injections.length)?new d:new j}function w(){this.value=null}function x(t){this.id=t}function P(t){this.fn=t,this.injections=null,this.thisInjection=null,this.errorHandler=null,this.mode=k.STANDARD,this.isSimpleChain=!1,this.executor=null}function m(t){for(var e=this.args=new Array(t.length-1),n=0,i=t.length;++n<i;)e[n-1]=t[n]}function O(t){this.injector=t,this.valuePipe=null}function C(t){O.call(this,t),this.fn=t.fn}function L(t,e,n,i,o){function r(t,e){null!==t?f(i,t):(arguments.length>2&&(e=new m(arguments)),o(e))}n.push(r);var u=t.apply(e,n);void 0!==u&&(u instanceof Promise?D(u,i,o):o(u))}function A(t){O.call(this,t),this.promise=t.fn}function D(t,e,n){t.then(n).catch(function(t){f(e,t)})}function E(t,e){O.call(this,e),this.iterator=t}function F(t,e){O.call(this,e),this.generator=t}function I(t){O.call(this,t),this.fn=t.fn,this.accumulationValue=t.injections[0],this.pendingExecutions=[],this.isRunnning=!1,this.executedLength=0,this.done=!1}function H(t){switch(t.mode){case k.STANDARD:case k.COLLECT:return t.fn instanceof Promise?new A(t):new C(t);case k.REDUCE:return new I(t);case k.EACH:var e=t.fn;if(Array.isArray(e)){var n;return n=V?e.values():{next:function(){var t=e,n=0;return function(){return n<t.length?{value:t[n++]}:{done:!0}}}()},new E(n,t)}if("function"==typeof e)return new F(e,t);if(U&&"object"==typeof e&&"function"==typeof e[Symbol.iterator])return new E(e[Symbol.iterator](),t)}}function R(){return new P(this)}function S(t){return R.call(t)}function T(){Object.defineProperty(Function.prototype,"inject",{get:R,configurable:!0,enumerable:!1})}function N(e){e&&t(Object.getOwnPropertyNames(e),function(t){this[t]=e[t]}.bind(this))}var U="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator,V="function"==typeof Array.prototype.values;r.try=function(){var n=arguments;return{catch:function(i){var o=n[0];"object"==typeof o&&Object.getPrototypeOf(o)===Object.prototype?(t(Object.getOwnPropertyNames(o),function(t){o[t]=h(o[t],i)}),r(o)):r.apply(null,e(n,function(t){return h(t,i)}))}}},r.each=function(t){var e=t instanceof P?t:new P(t);return e.mode=k.EACH,e},r.collect=function(t){var e=p(t);return e.mode=k.COLLECT,e},r.reduce=function(t,e){var n=p(t);return n.mode=k.REDUCE,null===n.injections?n.injections=[e]:n.injections.unshift(e),n},r.ref=function(t){return new x(t)},l.prototype.register=function(t){this.observers.indexOf(t)<0&&this.observers.push(t)},l.prototype.push=function(e,n,i){this.values.push(e),this.updateDone(n,i);var o=this;t(this.observers,function(t){t.notify(o)})},l.prototype.pushFailure=function(e,n){this.failedLength++,this.updateDone(e,n);var i=this;t(this.observers,function(t){t.notifyFailure(i)})},l.prototype.updateDone=function(t,e){t&&(this.reachedLast=!0),this.expectedLength=Math.max(this.expectedLength,e),this.done=this.reachedLast&&this.values.length===this.expectedLength-this.failedLength},v.prototype.notify=function(t){var e=t.values;this.values.push(e[e.length-1]),this.updateDone(),this.combinator.notify(this)},v.prototype.notifyFailure=function(t){this.updateDone(),this.combinator.notifyFailure(this)},v.prototype.updateDone=function(){this.done=i(this.inputPipes,function(t){return t.done})},y.prototype.notify=function(t){},y.prototype.notifyFailure=function(t){},d.prototype=Object.create(y.prototype),d.prototype.notify=function(t){var e=this.valuePipes[0],n=e.values;this.valueProviders[0].value=n[n.length-1],this.injector.execute(e.done,n.length)},j.prototype=Object.create(y.prototype),j.prototype.notify=function(t){var i=this.valuePipes,o=this.valueProviders,r=!0,u=1,c=-1,s=e(i,function(e,n){r&=e.done;var i=e.values.length;return u*=i,e===t&&(c=n),i});if(!(this.executionCounter>=u)){this.executionCounter=u,o[c].value=i[c].values[s[c]-1];for(var a=n(new Array(i.length),function(){return 0}),h=!1;!h;){h=!0;for(var f=-1,p=i.length;++f<p;)f!==c&&(o[f].value=i[f].values[a[f]],h&&(a[f]++,h=!1,a[f]===s[f]&&(a[f]=0,h=!0)));this.injector.execute(r&&h,u)}}},g.prototype=Object.create(y.prototype),g.prototype.notify=g.prototype.notifyFailure=function(n){var r=this.valuePipes;if(i(r,function(t){return t.done})){var u=this.valueProviders;t(r,function(t,n){u[n].value=e(t.values,o)}),this.injector.execute(!0,1)}};var k={STANDARD:0,EACH:1,COLLECT:2,REDUCE:3};return P.prototype.args=function(){return this.injections=e(arguments,function(t){return t}),this},P.prototype.this=function(t){return this.thisInjection=t,this},P.prototype.catch=function(t){return this.errorHandler=t,this},P.prototype.execute=function(n,i){var o,r=this.injections;this.isSimpleChain?(o=[],null!==r&&t(r,function(t){var e=t.value;e instanceof m?Array.prototype.push.apply(o,e.args):o.push(e)})):o=null===r?[]:e(r,function(t){var e=t.value;return e instanceof m?e.args:e});var u,c=this.thisInjection;c instanceof w&&(u=c.value instanceof m?c.value.args[0]:c.value),this.executor.execute(u,o,n,i)},O.prototype.execute=function(t,e,n,i){},C.prototype=Object.create(O.prototype),C.prototype.execute=function(t,e,n,i){function o(t){r.push(t,n,i)}var r=this.valuePipe;L(this.fn,t,e,this.injector,o)},A.prototype=Object.create(O.prototype),A.prototype.execute=function(t,e,n,i){var o=this.valuePipe;D(this.promise,this.injector,function(t){o.push(t,!0,1)})},E.prototype=Object.create(O.prototype),E.prototype.execute=function(t,e,n,i){for(var o=this.iterator,r=this.valuePipe,u=this.injector,c=o.next(),s=1;!c.done;s++){var a=c.value;c=o.next(),a instanceof Promise?a.then(function(t,e,n){r.push(n,t,e)}.bind(null,c.done,s)).catch(function(t,e,n){f(u,n),r.pushFailure(t,e)}.bind(null,c.done,s)):r.push(a,c.done,s)}},F.prototype=Object.create(O.prototype),F.prototype.execute=function(t,e,n,i){var o=this.generator.apply(t,e),r=new E(o,this.injector);r.valuePipe=this.valuePipe,r.execute(t,e,n,i)},I.prototype=Object.create(O.prototype),I.prototype.execute=function(t,e,n,i){var o=this.pendingExecutions;o.push([t,e]),this.done=this.done||n;var r=function(){if(0!==o.length){var t=o.pop();t[1][0]=this.accumulationValue,this.executedLength++,L(this.fn,t[0],t[1],this.injector,function(t){this.done&&0===o.length?this.valuePipe.push(t,!0,1):(this.accumulationValue=t,r())}.bind(this))}}.bind(this);this.isRunnning||(this.isRunnning=!0,r(),this.isRunnning=!1)},r.config=function(t){new N(t).dontHackFunctionPrototype?(Object.getOwnPropertyDescriptor(Function.prototype,"inject").get===R&&delete Function.prototype.inject,r.inject=S):("function"==typeof r.inject&&delete r.inject,T())},N.prototype.dontHackFunctionPrototype=!1,r.config(),r});