move-on
Version:
Creates a queue of sync or async functions with resolve and reject callback.
1 lines • 11.9 kB
JavaScript
module.exports=function(t){var e={};function n(i){if(e[i])return e[i].exports;var r=e[i]={i:i,l:!1,exports:{}};return t[i].call(r.exports,r,r.exports,n),r.l=!0,r.exports}return n.m=t,n.c=e,n.d=function(t,e,i){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:i})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var i=Object.create(null);if(n.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var r in t)n.d(i,r,function(e){return t[e]}.bind(null,r));return i},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=2)}([function(t,e){t.exports=require("of-type")},function(t,e){t.exports=require("typeof-arguments")},function(t,e,n){"use strict";n.r(e);var i=n(1),r=n.n(i),o=n(0),u=n.n(o);function c(t){return function(t){if(Array.isArray(t))return l(t)}(t)||function(t){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(t))return Array.from(t)}(t)||a(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function a(t,e){if(t){if("string"==typeof t)return l(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?l(t,e):void 0}}function l(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,i=new Array(e);n<e;n++)i[n]=t[n];return i}function f(t){return(f="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function s(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&h(t,e)}function h(t,e){return(h=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function m(t){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(t){return!1}}();return function(){var n,i=d(t);if(e){var r=d(this).constructor;n=Reflect.construct(i,arguments,r)}else n=i.apply(this,arguments);return y(this,n)}}function y(t,e){return!e||"object"!==f(e)&&"function"!=typeof e?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):e}function d(t){return(d=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function v(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}function b(t,e,n){return e&&v(t.prototype,e),n&&v(t,n),t}var g=function(){function t(e){p(this,t),Object.defineProperty(this,"_listLength",{value:e})}return b(t,[{key:"_add",value:function(t,e){var n=u()(e,"arguments")?Array.prototype.slice.call(e,0):e;this[t]=n}},{key:"forEach",value:function(t){r()(arguments,[Function],(function(t){throw new TypeError("".concat("move-on",": forEach: ").concat(t.message))}));for(var e=0,n=Object.keys(this);e<n.length;e++){var i=n[e];t(this[i],Number(i),this)}}},{key:"forAll",value:function(t){r()(arguments,[Function],(function(t){throw new TypeError("".concat("move-on",": forAll: ").concat(t.message))}));for(var e=0,n=Object.keys(this);e<n.length;e++){var i=n[e];for(var o in this[i])t(this[i][o],Number(i),Number(o),this)}}},{key:"missing",get:function(){for(var t=[],e=0;e<this._listLength;e++)u()(this[e],Array)||t.push(e);return t}}]),t}(),_=function(t){s(n,t);var e=m(n);function n(t){return p(this,n),e.call(this,t)}return n}(g),w=function(t){s(n,t);var e=m(n);function n(t){return p(this,n),e.call(this,t)}return n}(g),O=function(){function t(e){var n=e.time,i=e.callback;p(this,t),this._time=n,this._callback=i,this._pending=!1,this._timedOut=!1,this._quietMode=!1,this._mount()}return b(t,[{key:"_mount",value:function(){var t=this;this._pending=!0,this._timer=setTimeout((function(){t._pending=!1,t._timedOut=!0,t._quietMode||t._callback(t._timeoutError)}),this._time)}},{key:"unmount",value:function(){this._pending&&(clearTimeout(this._timer),this._pending=!1)}},{key:"quiet",value:function(){this._pending&&(this._quietMode=!0)}},{key:"timedOut",get:function(){return this._timedOut}},{key:"_timeoutError",get:function(){var t=new Error;return t.message="Timeout. The chained function did not respond in the expected time of ".concat(this._time," ms."),t.info="timeout",t.code="ETIMEDOUT",t}}]),t}(),j=function(){function t(e,n){p(this,t),this._mode=e,this._init.apply(this,c(n))}return b(t,[{key:"_validateArguments",value:function(t){var e=[Array,[Object,null],Function,Function];r()(t,e,(function(t){throw new TypeError("".concat("move-on",": ").concat(t.message))}))}},{key:"_validateFunctionList",value:function(t){var e=this;this.list=t;var n=function(t){var n=e.list[t];if(!u()(n,[Function,Array]))throw e.error.incorrectListItem(t);if(u()(n,Array)){if(0===n.length)return"continue";n.forEach((function(i,r){if(0!==r&&!u()(i,[String,Function]))throw e.error.incorrectMethodItem(t,r);if(0!==r&&u()(i,String)&&!u()(n[0][i],Function))throw e.error.incorrectMethodName(i,t,r)}))}};for(var i in this.list)n(i)}},{key:"_validateConfig",value:function(t){this.config=this.defaults,u()(t,null)||((t.hasOwnProperty("timeout")&&u()(t.timeout,null)||u()(t.timeout,Number)&t.timeout>=0&Math.round(t.timeout)===t.timeout&&!isNaN(t.timeout))&&(this.config.timeout=t.timeout),t.hasOwnProperty("bind")&&u()(t.bind,Boolean)&&(this.config.bind=t.bind),t.hasOwnProperty("context")&&(this.config.context=t.context),t.hasOwnProperty("passContext")&&u()(t.passContext,Boolean)&&(this.config.passContext=t.passContext))}},{key:"_init",value:function(t,e,n,i){this._validateArguments(arguments),this._validateFunctionList(t),this._validateConfig(e),this.finalCatch=this._bind({caller:i}),this.finalDone=this._bind({caller:n,reject:this.finalCatch}),this[this._mode]()}},{key:"_moveOn",value:function(){var t=this;this.list.reduceRight((function(e,n){if(u()(n,Function)){var i={caller:n,resolve:e,reject:t.finalCatch,timeout:!0};return t._bind(i)}var r=n[0];return n.reduceRight((function(e,n,i){return 0===i?e:t._bind({caller:[r,n],resolve:e,reject:t.finalCatch,timeout:!0})}),e)}),this.finalDone)()}},{key:"_moveAll",value:function(){var t=this,e=new Map,n=this.timeoutDefined?new O(this.timeoutConfig):null,i=new _(this.listLength),r=!1,o=0,u=function(u,c){return function(){!0===e.get(u)||r||(c?(e.set(u,!0),i._add(u,arguments),++o===this.listLength&&(n&&n.unmount(),n&&n.timedOut||this.finalDone(i))):(n&&n.unmount(),n&&n.timedOut||this.finalCatch.apply(this,arguments),r=!0))}.bind(t)};this._loopChain((function(e,n,i){if(t._bind({caller:e,resolve:u(n,!0),reject:u(n,!1)})(),r)return i()}),this.finalDone)}},{key:"_moveEach",value:function(){var t=this,e=new Map,n=this.timeoutDefined?new O(this.timeoutConfig):null,i=new _(this.listLength),r=0,o=function(o,u){return function(){!0!==e.get(o)&&(e.set(o,!0),u&&i._add(o,arguments),u||n&&n.timedOut||this.finalCatch.apply(this,arguments),++r===this.listLength&&(n&&n.unmount(),n&&n.timedOut||this.finalDone(i)))}.bind(t)};this._loopChain((function(e,n){t._bind({caller:e,resolve:o(n,!0),reject:o(n,!1)})()}),this.finalDone)}},{key:"_moveFirst",value:function(){var t=this,e=new Map,n=this.timeoutDefined?new O(this.timeoutConfig):null,i=new w(this.listLength),r=!1,o=0,u=function(u,c){return function(){!0===e.get(u)||r||(c?(n&&n.unmount(),n&&n.timedOut||this.finalDone.apply(this,arguments),r=!0):(e.set(u,!0),i._add(u,arguments),++o===this.listLength&&(n&&n.unmount(),n&&n.timedOut||this.finalCatch(i))))}.bind(t)};this._loopChain((function(e,n,i){if(t._bind({caller:e,resolve:u(n,!0),reject:u(n,!1)})(),r)return i()}),this.finalDone)}},{key:"_loopChain",value:function(t,e){var n,i=0,r=function(){return o=!0},o=!1,c=function(t,e){var n;if("undefined"==typeof Symbol||null==t[Symbol.iterator]){if(Array.isArray(t)||(n=a(t))||e&&t&&"number"==typeof t.length){n&&(t=n);var i=0,r=function(){};return{s:r,n:function(){return i>=t.length?{done:!0}:{done:!1,value:t[i++]}},e:function(t){throw t},f:r}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,u=!0,c=!1;return{s:function(){n=t[Symbol.iterator]()},n:function(){var t=n.next();return u=t.done,t},e:function(t){c=!0,o=t},f:function(){try{u||null==n.return||n.return()}finally{if(c)throw o}}}}(this.list);try{for(c.s();!(n=c.n()).done;){var l=n.value;if(u()(l,Function))t(l,i++,r);else for(var f=1;f<l.length&&(t([l[0],l[f]],i++,r),!o);f++);if(o)break}}catch(t){c.e(t)}finally{c.f()}0===i&&e()}},{key:"_bind",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=t.caller,n=t.resolve,i=t.reject,r=t.timeout,o=this.config.passContext?[this.config.context]:[];return function(){var t,a,l=this.timeoutDefined&&r?new O(this.timeoutConfig):null,f=[];this._middleChain(f,n,"resolve",l),this._middleChain(f,i,"reject",l);var s=[].concat(f,o);return u()(e,Array)?u()(e[1],Function)?(t=e[1]).call.apply(t,[e[0]].concat(c(s),Array.prototype.slice.call(arguments))):(a=e[0])[e[1]].apply(a,c(s).concat(Array.prototype.slice.call(arguments))):this.config.bind?e.call.apply(e,[this.config.context].concat(c(s),Array.prototype.slice.call(arguments))):e.apply(void 0,c(s).concat(Array.prototype.slice.call(arguments)))}.bind(this)}},{key:"_middleChain",value:function(t,e,n,i){u()(e,Function)&&t.push(Object.defineProperty(function(){i&&i.quiet(),i&&i.timedOut||e.apply(void 0,arguments)}.bind(this),"name",{value:n}))}},{key:"error",get:function(){return{incorrectListItem:function(t){return new TypeError("".concat("move-on",": Invalid argument [0]. Incorrect [").concat(t,"] item type. The item of [Function|Array] type is expected."))},incorrectObjectItem:function(t){return new TypeError("".concat("move-on",": Invalid argument [0]. Incorrect [").concat(t,"][0] item. The item of [Object] type is expected."))},incorrectMethodItem:function(t,e){return new TypeError("".concat("move-on",": Invalid argument [0]. Incorrect [").concat(t,"][").concat(e,"] item. The [Function|String] item is expected."))},incorrectMethodName:function(t,e,n){return new TypeError("".concat("move-on",": Invalid argument [0]. Incorrect [").concat(e,"][").concat(n,"] item. The given [").concat(e,'][0] object does not define ["').concat(t,'"] method.'))}}}},{key:"defaults",get:function(){return{timeout:1e4,bind:!0,context:{},passContext:!0}}},{key:"listLength",get:function(){for(var t=0,e=0;e<this.list.length;e++)u()(this.list[e],Function)?t++:u()(this.list[e],Array)&&(t+=this.list[e].length-1);return t}},{key:"timeoutConfig",get:function(){return{time:this.config.timeout,callback:this.finalCatch}}},{key:"timeoutDefined",get:function(){return!(null===this.config.timeout||this.config.timeout===1/0)}}]),t}();var k={all:{value:function(){new j("_moveAll",arguments)}},each:{value:function(){new j("_moveEach",arguments)}},first:{value:function(){new j("_moveFirst",arguments)}}};e.default=Object.defineProperties((function(){new j("_moveOn",arguments)}),k)}]).default;