UNPKG

focus-flow

Version:
3 lines (2 loc) 14.4 kB
// * Released under the MIT License. !function(){"use strict";var t=function(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t};var e=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")};function n(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}var r=function(t,e,r){return e&&n(t.prototype,e),r&&n(t,r),t},i=function(){function n(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};e(this,n),t(this,"pond",[]),t(this,"threads",[]),t(this,"queue",[]),t(this,"queueExit",!0),t(this,"event",{full:null}),this.defaults(r),this.junction()}return r(n,[{key:"defaults",value:function(t){this.options=Object.assign({threadMax:1,switch:!0,life:-1,hand:null,queue:!1,queueMax:10},t)}},{key:"junction",value:function(){function t(t,e){e()}this.basic={},this.error(function(t,e,n){console.error(t),n()}).success(t).fail(t).end(function(){})}},{key:"length",get:function(){return this.pond.length}},{key:"ram",get:function(){return this.options.switch&&this.options.threadMax>this.threads.length}},{key:"queueFull",get:function(){return this.options.queue&&this.options.queueMax>this.queue.length}}]),n}();function o(t,e){return t(e={exports:{}},e.exports),e.exports}t(i,"_id",0);var s=o(function(t){function e(n){return"function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?t.exports=e=function(t){return typeof t}:t.exports=e=function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},e(n)}t.exports=e}),u=o(function(t){var e=function(t){var e,n=Object.prototype,r=n.hasOwnProperty,i="function"==typeof Symbol?Symbol:{},o=i.iterator||"@@iterator",s=i.asyncIterator||"@@asyncIterator",u=i.toStringTag||"@@toStringTag";function c(t,e,n,r){var i=e&&e.prototype instanceof d?e:d,o=Object.create(i.prototype),s=new q(r||[]);return o._invoke=function(t,e,n){var r=h;return function(i,o){if(r===l)throw new Error("Generator is already running");if(r===p){if("throw"===i)throw o;return P()}for(n.method=i,n.arg=o;;){var s=n.delegate;if(s){var u=E(s,n);if(u){if(u===v)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(r===h)throw r=p,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r=l;var c=a(t,e,n);if("normal"===c.type){if(r=n.done?p:f,c.arg===v)continue;return{value:c.arg,done:n.done}}"throw"===c.type&&(r=p,n.method="throw",n.arg=c.arg)}}}(t,n,s),o}function a(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var h="suspendedStart",f="suspendedYield",l="executing",p="completed",v={};function d(){}function y(){}function g(){}var x={};x[o]=function(){return this};var b=Object.getPrototypeOf,m=b&&b(b($([])));m&&m!==n&&r.call(m,o)&&(x=m);var w=g.prototype=d.prototype=Object.create(x);function k(t){["next","throw","return"].forEach(function(e){t[e]=function(t){return this._invoke(e,t)}})}function O(t,e){var n;this._invoke=function(i,o){function s(){return new e(function(n,s){!function n(i,o,s,u){var c=a(t[i],t,o);if("throw"!==c.type){var h=c.arg,f=h.value;return f&&"object"==typeof f&&r.call(f,"__await")?e.resolve(f.__await).then(function(t){n("next",t,s,u)},function(t){n("throw",t,s,u)}):e.resolve(f).then(function(t){h.value=t,s(h)},function(t){return n("throw",t,s,u)})}u(c.arg)}(i,o,n,s)})}return n=n?n.then(s,s):s()}}function E(t,n){var r=t.iterator[n.method];if(r===e){if(n.delegate=null,"throw"===n.method){if(t.iterator.return&&(n.method="return",n.arg=e,E(t,n),"throw"===n.method))return v;n.method="throw",n.arg=new TypeError("The iterator does not provide a 'throw' method")}return v}var i=a(r,t.iterator,n.arg);if("throw"===i.type)return n.method="throw",n.arg=i.arg,n.delegate=null,v;var o=i.arg;return o?o.done?(n[t.resultName]=o.value,n.next=t.nextLoc,"return"!==n.method&&(n.method="next",n.arg=e),n.delegate=null,v):o:(n.method="throw",n.arg=new TypeError("iterator result is not an object"),n.delegate=null,v)}function j(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function L(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function q(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(j,this),this.reset(!0)}function $(t){if(t){var n=t[o];if(n)return n.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var i=-1,s=function n(){for(;++i<t.length;)if(r.call(t,i))return n.value=t[i],n.done=!1,n;return n.value=e,n.done=!0,n};return s.next=s}}return{next:P}}function P(){return{value:e,done:!0}}return y.prototype=w.constructor=g,g.constructor=y,g[u]=y.displayName="GeneratorFunction",t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===y||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,g):(t.__proto__=g,u in t||(t[u]="GeneratorFunction")),t.prototype=Object.create(w),t},t.awrap=function(t){return{__await:t}},k(O.prototype),O.prototype[s]=function(){return this},t.AsyncIterator=O,t.async=function(e,n,r,i,o){void 0===o&&(o=Promise);var s=new O(c(e,n,r,i),o);return t.isGeneratorFunction(n)?s:s.next().then(function(t){return t.done?t.value:s.next()})},k(w),w[u]="Generator",w[o]=function(){return this},w.toString=function(){return"[object Generator]"},t.keys=function(t){var e=[];for(var n in t)e.push(n);return e.reverse(),function n(){for(;e.length;){var r=e.pop();if(r in t)return n.value=r,n.done=!1,n}return n.done=!0,n}},t.values=$,q.prototype={constructor:q,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=e,this.done=!1,this.delegate=null,this.method="next",this.arg=e,this.tryEntries.forEach(L),!t)for(var n in this)"t"===n.charAt(0)&&r.call(this,n)&&!isNaN(+n.slice(1))&&(this[n]=e)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var n=this;function i(r,i){return u.type="throw",u.arg=t,n.next=r,i&&(n.method="next",n.arg=e),!!i}for(var o=this.tryEntries.length-1;o>=0;--o){var s=this.tryEntries[o],u=s.completion;if("root"===s.tryLoc)return i("end");if(s.tryLoc<=this.prev){var c=r.call(s,"catchLoc"),a=r.call(s,"finallyLoc");if(c&&a){if(this.prev<s.catchLoc)return i(s.catchLoc,!0);if(this.prev<s.finallyLoc)return i(s.finallyLoc)}else if(c){if(this.prev<s.catchLoc)return i(s.catchLoc,!0)}else{if(!a)throw new Error("try statement without catch or finally");if(this.prev<s.finallyLoc)return i(s.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var i=this.tryEntries[n];if(i.tryLoc<=this.prev&&r.call(i,"finallyLoc")&&this.prev<i.finallyLoc){var o=i;break}}o&&("break"===t||"continue"===t)&&o.tryLoc<=e&&e<=o.finallyLoc&&(o=null);var s=o?o.completion:{};return s.type=t,s.arg=e,o?(this.method="next",this.next=o.finallyLoc,v):this.complete(s)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),v},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),L(n),v}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var i=r.arg;L(n)}return i}}throw new Error("illegal catch attempt")},delegateYield:function(t,n,r){return this.delegate={iterator:$(t),resultName:n,nextLoc:r},"next"===this.method&&(this.arg=e),v}},t}(t.exports);try{regeneratorRuntime=e}catch(t){Function("r","regeneratorRuntime = r")(e)}});function c(t,e,n,r,i,o,s){try{var u=t[o](s),c=u.value}catch(t){return void n(t)}u.done?e(c):Promise.resolve(c).then(r,i)}var a=function(t){return function(){var e=this,n=arguments;return new Promise(function(r,i){var o=t.apply(e,n);function s(t){c(o,r,i,s,u,"next",t)}function u(t){c(o,r,i,s,u,"throw",t)}s(void 0)})}};var h={use:function(t,e,n){if("function"==typeof t)e&&(n=e),e=t,t=this.pond.length;else if("number"==typeof t)t=this.pond.length;else if(t instanceof i)return this.docking(t,e);return e=this.redirect(e,n),this.pond.push({sign:t,callback:e}),this},error:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:this.options.hand,n=this.basic;return t=this.redirect(t,e),n.error=(r=a(u.mark(function e(n,r){return u.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return r.ctx.$info.index=null,e.next=3,t(n,r.ctx,r.$next,r.$close);case 3:case"end":return e.stop()}},e)})),function(t,e){return r.apply(this,arguments)}),this;var r},success:function(t,e){return this.isState("success",t,e)},fail:function(t,e){return this.isState("fail",t,e)},end:function(t){var e=this,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:this.options.hand,r=this.basic;return t=this.redirect(t,n),r.end=(i=a(u.mark(function n(r){return u.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:return e.closeThread(r),n.next=3,t(r.ctx);case 3:case"end":return n.stop()}},n)})),function(t){return i.apply(this,arguments)}),this;var i},isState:function(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:this.options.hand;return e=this.redirect(e,n),this.basic[t]=(r=a(u.mark(function t(n){return u.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return n.ctx.$info.index=null,t.next=3,e(n.ctx,n.$next,n.$close);case 3:case"end":return t.stop()}},t)})),function(t){return r.apply(this,arguments)}),this;var r},docking:function(t){var e=this,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:this.options.hand;return t.pond.forEach(function(t){return e.use(t.sign,t.callback,n)}),this},redirect:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:this.options.hand;return"object"===s(e)&&null!==e?t.bind(e):t}},f=function(){function n(r,i){e(this,n),t(this,"$next",this.next.bind(this)),t(this,"$close",this.close.bind(this)),t(this,"onceZero",!0),this.ctx=Object.assign({},i),this.ff=r,this.initInfo(r)}return r(n,[{key:"initInfo",value:function(t){var e={id:i._id++,ff:t,index:0};Object.defineProperty(this.ctx,"$info",{value:e}),this.active()}},{key:"next",value:function(t,e){if(t instanceof i)return this.span(t,e);var n=this.ctx.$info.ff;this.active(),"boolean"==typeof t?n.run(this,t):void 0!==t&&"boolean"!=typeof t?n.nextStart(this,t):n.run(this)}},{key:"span",value:function(t,e){return this.close(),t.start(this.ctx,e),this}},{key:"active",value:function(){var t=this.ctx.$info;-1!==t.ff.options.life&&(t.life=Date.now()+t.ff.options.life)}},{key:"close",value:function(){this.ff.closeThread(this)}}]),n}();var l={inspect:function(){return!this.ram&&!this.clean()},close:function(){return this.options.switch=!1,this},open:function(){return this.options.switch=!0,this.processNextMessage(),this},newThread:function(t){var e=new f(this,t);return this.threads.push(e),e},closeThread:function(t){var e=this.threads.findIndex(function(e){return e===t});this.threads.splice(e,1+e),this.processNextMessage()},emptyThreads:function(){return this.threads=[],this.processNextMessage(),this},clean:function(){var t=this.threads.length;return this.threads=this.threads.filter(function(t){return-1===t.ctx.$info.life||t.ctx.$info.life>Date.now()}),t<this.threads.length}};function p(){return(p=a(u.mark(function t(e,n){var r,i,o;return u.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(r=this.pond,i=e.ctx,o=i.$info.index<r.length,t.prev=3,null!==i.$info.index){t.next=9;break}return t.next=7,this.basic.end(e);case 7:t.next=20;break;case 9:if(!1!==n){t.next=14;break}return t.next=12,this.basic.fail(e);case 12:t.next=20;break;case 14:if(o&&!0!==n){t.next=19;break}return t.next=17,this.basic.success(e);case 17:t.next=20;break;case 19:o&&r[i.$info.index++].callback(i,e.$next,e.$close);case 20:t.next=25;break;case 22:t.prev=22,t.t0=t.catch(3),this.basic.error(t.t0,e);case 25:case"end":return t.stop()}},t,this,[[3,22]])}))).apply(this,arguments)}var v={start:function(t,e){if("object"!==s(t)){var n=[e,t];t=n[0],e=n[1]}return this.spill(t,e)?this:(this.newThread(t).next(e),this)},nextStart:function(t,e){var n=this.matching(e);t.ctx.$info.index=-1===n?t.onceZero?0:t.ctx.$info.index++:n,t.onceZero&&(t.onceZero=!1),this.run(t)},run:function(t,e){return p.apply(this,arguments)},spill:function(t,e){return!!this.inspect()&&("function"==typeof this.event.full&&this.event.full(t,this),this.queueFull?this.queue.push({ctx:t,sign:e}):this.options.queue&&"function"==typeof this.event.queueFull&&this.event.queueFull(t,this),!0)},matching:function(t){var e=this.pond;return"number"!=typeof t?e.findIndex(function(e){return e.sign===t}):t>=e.length?-1:t}};var d={onFull:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:this.options.hand;return t=this.redirect(t,e),this.event.full=t,this},onQueueFull:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:this.options.hand;return t=this.redirect(t,e),this.event.queueFull=t,this}};var y={processNextMessage:function(){this.queue.length&&this.queueExit&&(this.start(this.queue.shift()),this.ram&&this.processNextMessage())},closeQueue:function(){return this.options.queue=!1,this},openQueue:function(){return this.options.queue=!0,this},emptyQueue:function(){return this.queue=[],this},closeExit:function(){return this.queueExit=!1,this},openExit:function(){return this.queueExit=!0,this.processNextMessage(),this}};function g(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),n.push.apply(n,r)}return n}function x(e){for(var n=arguments,r=1;r<arguments.length;r++){var i=null!=n[r]?n[r]:{};r%2?g(Object(i),!0).forEach(function(n){t(e,n,i[n])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(i)):g(Object(i)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(i,t))})}return e}var b=i.prototype,m=x(x(x(x(x({},h),l),v),d),y);Object.keys(m).forEach(function(t){Object.defineProperty(b,t,{value:m[t]})})}();