pubo-utils 1.0.3-alpha.0 → 1.0.5-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- !function(t,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports["pubo-utils"]=n():t["pubo-utils"]=n()}(this,(function(){return function(t){var n={};function e(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}return e.m=t,e.c=n,e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{enumerable:!0,get:r})},e.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},e.t=function(t,n){if(1&n&&(t=e(t)),8&n)return t;if(4&n&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(e.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&n&&"string"!=typeof t)for(var o in t)e.d(r,o,function(n){return t[n]}.bind(null,o));return r},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,n){return Object.prototype.hasOwnProperty.call(t,n)},e.p="",e(e.s=0)}([function(t,n,e){"use strict";e.r(n),e.d(n,"random",(function(){return r})),e.d(n,"WatchDog",(function(){return g})),e.d(n,"superFactory",(function(){return s})),e.d(n,"Emitter",(function(){return l})),e.d(n,"SyncQueue",(function(){return y})),e.d(n,"sleep",(function(){return v})),e.d(n,"ContinuousTrigger",(function(){return w})),e.d(n,"throttle",(function(){return m}));var r=function(t){void 0===t&&(t=8);var n=function(t){return Math.random().toString(32).slice(2,2+t)};if(t<=8)return n(t);for(var e="",r=8;r<=t;r+=8)e+=n(t-r>8?8:t-r);return e},o=function(t,n,e,r){return new(e||(e=Promise))((function(o,i){function u(t){try{a(r.next(t))}catch(t){i(t)}}function c(t){try{a(r.throw(t))}catch(t){i(t)}}function a(t){var n;t.done?o(t.value):(n=t.value,n instanceof e?n:new e((function(t){t(n)}))).then(u,c)}a((r=r.apply(t,n||[])).next())}))},i=function(t,n){var e,r,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(i){return function(c){return function(i){if(e)throw new TypeError("Generator is already executing.");for(;u;)try{if(e=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,r=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=n.call(t,u)}catch(t){i=[6,t],r=0}finally{e=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,c])}}},u=function(t){var n="function"==typeof Symbol&&Symbol.iterator,e=n&&t[n],r=0;if(e)return e.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}};throw new TypeError(n?"Object is not iterable.":"Symbol.iterator is not defined.")},c=function(t,n){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var r,o,i=e.call(t),u=[];try{for(;(void 0===n||n-- >0)&&!(r=i.next()).done;)u.push(r.value)}catch(t){o={error:t}}finally{try{r&&!r.done&&(e=i.return)&&e.call(i)}finally{if(o)throw o.error}}return u},a=function(){for(var t=[],n=0;n<arguments.length;n++)t=t.concat(c(arguments[n]));return t},l=function(){function t(){this.state={},this.ids={}}return t.prototype.on=function(t,n){if(this.state[t]||(this.state[t]=[]),"function"!=typeof n)throw new Error("第二个参数必须为function!");var e=this.state[t].push(n)-1,o=r(40);return this.ids[o]={event:t,index:e},o},t.prototype.cancel=function(t){var n=this.ids[t]||{},e=n.event,r=n.index;e&&Array.isArray(this.state[e])&&this.state[e].splice(r,1)},t.prototype.emit=function(t){for(var n=[],e=1;e<arguments.length;e++)n[e-1]=arguments[e];return o(this,void 0,void 0,(function(){var e,r,o,c,l,f;return i(this,(function(i){switch(i.label){case 0:if(!Array.isArray(this.state[t]))return[3,8];i.label=1;case 1:i.trys.push([1,6,7,8]),e=u(this.state[t]),r=e.next(),i.label=2;case 2:return r.done?[3,5]:"function"!=typeof(o=r.value)?[3,4]:[4,o.apply(void 0,a(n))];case 3:i.sent(),i.label=4;case 4:return r=e.next(),[3,2];case 5:return[3,8];case 6:return c=i.sent(),l={error:c},[3,8];case 7:try{r&&!r.done&&(f=e.return)&&f.call(e)}finally{if(l)throw l.error}return[7];case 8:return[2]}}))}))},t}(),f=function(t){var n="function"==typeof Symbol&&Symbol.iterator,e=n&&t[n],r=0;if(e)return e.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}};throw new TypeError(n?"Object is not iterable.":"Symbol.iterator is not defined.")},s=function(t){return function(n){var e,r,o={};try{for(var i=f(Object.keys(n)),u=i.next();!u.done;u=i.next()){var c=u.value;o[c]=t(n[c])}}catch(t){e={error:t}}finally{try{u&&!u.done&&(r=i.return)&&r.call(i)}finally{if(e)throw e.error}}return o}},p=function(t,n,e,r){return new(e||(e=Promise))((function(o,i){function u(t){try{a(r.next(t))}catch(t){i(t)}}function c(t){try{a(r.throw(t))}catch(t){i(t)}}function a(t){var n;t.done?o(t.value):(n=t.value,n instanceof e?n:new e((function(t){t(n)}))).then(u,c)}a((r=r.apply(t,n||[])).next())}))},h=function(t,n){var e,r,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(i){return function(c){return function(i){if(e)throw new TypeError("Generator is already executing.");for(;u;)try{if(e=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,r=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=n.call(t,u)}catch(t){i=[6,t],r=0}finally{e=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,c])}}},y=function(){function t(){this.cache=[],this.running=!1}return t.prototype._run=function(t){var n=t.fn,e=t.promise;return p(this,void 0,void 0,(function(){var t,r;return h(this,(function(o){switch(o.label){case 0:return o.trys.push([0,2,,3]),[4,n()];case 1:return t=o.sent(),e.resolve(t),[3,3];case 2:return r=o.sent(),e.reject(r),[3,3];case 3:return[2]}}))}))},t.prototype.run=function(){return p(this,void 0,void 0,(function(){var t;return h(this,(function(n){switch(n.label){case 0:return this.cache.length<1?(this.running=!1,[2]):(this.running=!0,"function"!=typeof(t=this.cache.shift()).fn?[3,2]:[4,this._run(t)]);case 1:n.sent(),n.label=2;case 2:return this.run(),[2]}}))}))},t.prototype.push=function(t){var n=this;return new Promise((function(e,r){n.cache.push({fn:t,promise:{resolve:e,reject:r}}),n.running||n.run()}))},Object.defineProperty(t.prototype,"length",{get:function(){return this.cache.length},enumerable:!1,configurable:!0}),t}(),b=function(t,n,e,r){return new(e||(e=Promise))((function(o,i){function u(t){try{a(r.next(t))}catch(t){i(t)}}function c(t){try{a(r.throw(t))}catch(t){i(t)}}function a(t){var n;t.done?o(t.value):(n=t.value,n instanceof e?n:new e((function(t){t(n)}))).then(u,c)}a((r=r.apply(t,n||[])).next())}))},d=function(t,n){var e,r,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(i){return function(c){return function(i){if(e)throw new TypeError("Generator is already executing.");for(;u;)try{if(e=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,r=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=n.call(t,u)}catch(t){i=[6,t],r=0}finally{e=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,c])}}},v=function(t){return b(void 0,void 0,void 0,(function(){return d(this,(function(n){switch(n.label){case 0:return[4,new Promise((function(n){return setTimeout((function(){return n()}),t)}))];case 1:return n.sent(),[2]}}))}))};function m(t,n){var e,r=!0;return function(){e||(e=setTimeout((function(){r=!0,e=null}),n)),r&&(r=!1,t())}}var w=function(){function t(t){this._count=0,this.props=t}return Object.defineProperty(t.prototype,"count",{get:function(){return this._count},enumerable:!1,configurable:!0}),t.prototype.increment=function(){var t=this;clearTimeout(this.timeout),this.timeout=setTimeout((function(){return t.clear()}),this.props.resetTime),this._count=this._count+1,this._count>this.props.count&&this.props.cb()},t.prototype.clear=function(){this._count=0},t}(),g=function(){function t(t){var n=t.limit,e=void 0===n?1:n,r=t.onTimeout;this.count=0,this.i=null,this.limit=e,this.onTimeout=r}return t.prototype.onTimeout=function(){console.log("timeout!")},t.prototype.onTimer=function(){this.count>this.limit&&"function"==typeof this.onTimeout?(this.onTimeout(),this.count=0):this.count++},t.prototype.init=function(){var t=this;this.i=setInterval((function(){return t.onTimer()}),1e3)},t.prototype.feed=function(){this.count=0},t.prototype.stop=function(){this.i&&(clearInterval(this.i),this.count=0)},t}()}])}));
1
+ !function(t,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports["pubo-utils"]=n():t["pubo-utils"]=n()}(this,(function(){return function(t){var n={};function e(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}return e.m=t,e.c=n,e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{enumerable:!0,get:r})},e.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},e.t=function(t,n){if(1&n&&(t=e(t)),8&n)return t;if(4&n&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(e.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&n&&"string"!=typeof t)for(var o in t)e.d(r,o,function(n){return t[n]}.bind(null,o));return r},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,n){return Object.prototype.hasOwnProperty.call(t,n)},e.p="",e(e.s=0)}([function(t,n,e){"use strict";e.r(n),e.d(n,"random",(function(){return r})),e.d(n,"WatchDog",(function(){return T})),e.d(n,"superFactory",(function(){return s})),e.d(n,"Emitter",(function(){return l})),e.d(n,"SyncQueue",(function(){return g})),e.d(n,"sleep",(function(){return y})),e.d(n,"ContinuousTrigger",(function(){return S})),e.d(n,"throttle",(function(){return x})),e.d(n,"loop",(function(){return v}));var r=function(t){void 0===t&&(t=8);var n=function(t){return Math.random().toString(32).slice(2,2+t)};if(t<=8)return n(t);for(var e="",r=8;r<=t;r+=8)e+=n(t-r>8?8:t-r);return e},o=function(t,n,e,r){return new(e||(e=Promise))((function(o,i){function u(t){try{a(r.next(t))}catch(t){i(t)}}function c(t){try{a(r.throw(t))}catch(t){i(t)}}function a(t){var n;t.done?o(t.value):(n=t.value,n instanceof e?n:new e((function(t){t(n)}))).then(u,c)}a((r=r.apply(t,n||[])).next())}))},i=function(t,n){var e,r,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(i){return function(c){return function(i){if(e)throw new TypeError("Generator is already executing.");for(;u;)try{if(e=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,r=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=n.call(t,u)}catch(t){i=[6,t],r=0}finally{e=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,c])}}},u=function(t){var n="function"==typeof Symbol&&Symbol.iterator,e=n&&t[n],r=0;if(e)return e.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}};throw new TypeError(n?"Object is not iterable.":"Symbol.iterator is not defined.")},c=function(t,n){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var r,o,i=e.call(t),u=[];try{for(;(void 0===n||n-- >0)&&!(r=i.next()).done;)u.push(r.value)}catch(t){o={error:t}}finally{try{r&&!r.done&&(e=i.return)&&e.call(i)}finally{if(o)throw o.error}}return u},a=function(){for(var t=[],n=0;n<arguments.length;n++)t=t.concat(c(arguments[n]));return t},l=function(){function t(){this.state={},this.ids={}}return t.prototype.on=function(t,n){if(this.state[t]||(this.state[t]=[]),"function"!=typeof n)throw new Error("第二个参数必须为function!");var e=this.state[t].push(n)-1,o=r(40);return this.ids[o]={event:t,index:e},o},t.prototype.cancel=function(t){var n=this.ids[t]||{},e=n.event,r=n.index;e&&Array.isArray(this.state[e])&&this.state[e].splice(r,1)},t.prototype.emit=function(t){for(var n=[],e=1;e<arguments.length;e++)n[e-1]=arguments[e];return o(this,void 0,void 0,(function(){var e,r,o,c,l,f;return i(this,(function(i){switch(i.label){case 0:if(!Array.isArray(this.state[t]))return[3,8];i.label=1;case 1:i.trys.push([1,6,7,8]),e=u(this.state[t]),r=e.next(),i.label=2;case 2:return r.done?[3,5]:"function"!=typeof(o=r.value)?[3,4]:[4,o.apply(void 0,a(n))];case 3:i.sent(),i.label=4;case 4:return r=e.next(),[3,2];case 5:return[3,8];case 6:return c=i.sent(),l={error:c},[3,8];case 7:try{r&&!r.done&&(f=e.return)&&f.call(e)}finally{if(l)throw l.error}return[7];case 8:return[2]}}))}))},t}(),f=function(t){var n="function"==typeof Symbol&&Symbol.iterator,e=n&&t[n],r=0;if(e)return e.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}};throw new TypeError(n?"Object is not iterable.":"Symbol.iterator is not defined.")},s=function(t){return function(n){var e,r,o={};try{for(var i=f(Object.keys(n)),u=i.next();!u.done;u=i.next()){var c=u.value;o[c]=t(n[c])}}catch(t){e={error:t}}finally{try{u&&!u.done&&(r=i.return)&&r.call(i)}finally{if(e)throw e.error}}return o}},p=function(t,n,e,r){return new(e||(e=Promise))((function(o,i){function u(t){try{a(r.next(t))}catch(t){i(t)}}function c(t){try{a(r.throw(t))}catch(t){i(t)}}function a(t){var n;t.done?o(t.value):(n=t.value,n instanceof e?n:new e((function(t){t(n)}))).then(u,c)}a((r=r.apply(t,n||[])).next())}))},h=function(t,n){var e,r,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(i){return function(c){return function(i){if(e)throw new TypeError("Generator is already executing.");for(;u;)try{if(e=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,r=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=n.call(t,u)}catch(t){i=[6,t],r=0}finally{e=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,c])}}},y=function(t){return p(void 0,void 0,void 0,(function(){return h(this,(function(n){switch(n.label){case 0:return[4,new Promise((function(n){return setTimeout((function(){return n()}),t)}))];case 1:return n.sent(),[2]}}))}))},b=function(t,n,e,r){return new(e||(e=Promise))((function(o,i){function u(t){try{a(r.next(t))}catch(t){i(t)}}function c(t){try{a(r.throw(t))}catch(t){i(t)}}function a(t){var n;t.done?o(t.value):(n=t.value,n instanceof e?n:new e((function(t){t(n)}))).then(u,c)}a((r=r.apply(t,n||[])).next())}))},d=function(t,n){var e,r,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(i){return function(c){return function(i){if(e)throw new TypeError("Generator is already executing.");for(;u;)try{if(e=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,r=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=n.call(t,u)}catch(t){i=[6,t],r=0}finally{e=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,c])}}},v=function(t,n){var e=!0,r=function(){e=!1};return function o(){return b(void 0,void 0,void 0,(function(){return d(this,(function(i){switch(i.label){case 0:return[4,t(r)];case 1:return i.sent(),[4,y(n)];case 2:return i.sent(),e&&o(),[2]}}))}))}(),r},m=function(t,n,e,r){return new(e||(e=Promise))((function(o,i){function u(t){try{a(r.next(t))}catch(t){i(t)}}function c(t){try{a(r.throw(t))}catch(t){i(t)}}function a(t){var n;t.done?o(t.value):(n=t.value,n instanceof e?n:new e((function(t){t(n)}))).then(u,c)}a((r=r.apply(t,n||[])).next())}))},w=function(t,n){var e,r,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(i){return function(c){return function(i){if(e)throw new TypeError("Generator is already executing.");for(;u;)try{if(e=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,r=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=n.call(t,u)}catch(t){i=[6,t],r=0}finally{e=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,c])}}},g=function(){function t(){this.cache=[],this.running=!1}return t.prototype._run=function(t){var n=t.fn,e=t.promise;return m(this,void 0,void 0,(function(){var t,r;return w(this,(function(o){switch(o.label){case 0:return o.trys.push([0,2,,3]),[4,n()];case 1:return t=o.sent(),e.resolve(t),[3,3];case 2:return r=o.sent(),e.reject(r),[3,3];case 3:return[2]}}))}))},t.prototype.run=function(){return m(this,void 0,void 0,(function(){var t;return w(this,(function(n){switch(n.label){case 0:return this.cache.length<1?(this.running=!1,[2]):(this.running=!0,"function"!=typeof(t=this.cache.shift()).fn?[3,2]:[4,this._run(t)]);case 1:n.sent(),n.label=2;case 2:return this.run(),[2]}}))}))},t.prototype.push=function(t){var n=this;return new Promise((function(e,r){n.cache.push({fn:t,promise:{resolve:e,reject:r}}),n.running||n.run()}))},Object.defineProperty(t.prototype,"length",{get:function(){return this.cache.length},enumerable:!1,configurable:!0}),t}();function x(t,n){var e,r=!0;return function(){e||(e=setTimeout((function(){r=!0,e=null}),n)),r&&(r=!1,t())}}var S=function(){function t(t){this._count=0,this.props=t}return Object.defineProperty(t.prototype,"count",{get:function(){return this._count},enumerable:!1,configurable:!0}),t.prototype.increment=function(){var t=this;clearTimeout(this.timeout),this.timeout=setTimeout((function(){return t.clear()}),this.props.resetTime),this._count=this._count+1,this._count>this.props.count&&this.props.cb()},t.prototype.clear=function(){this._count=0},t}(),T=function(){function t(t){var n=t.limit,e=void 0===n?1:n,r=t.onTimeout;this.count=0,this.i=null,this.limit=e,this.onTimeout=r}return t.prototype.onTimeout=function(){console.log("timeout!")},t.prototype.onTimer=function(){this.count>this.limit&&"function"==typeof this.onTimeout?(this.onTimeout(),this.count=0):this.count++},t.prototype.init=function(){var t=this;this.i=setInterval((function(){return t.onTimer()}),1e3)},t.prototype.feed=function(){this.count=0},t.prototype.stop=function(){this.i&&(clearInterval(this.i),this.count=0)},t}()}])}));
package/es/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import { Emitter } from './emitter';
2
2
  import { superFactory } from './factory';
3
+ import { loop } from './loop';
3
4
  import { SyncQueue } from './queue';
4
5
  import { random } from './random';
5
6
  import { sleep } from './sleep';
6
7
  import { throttle } from './throttle';
7
8
  import { ContinuousTrigger } from './trigger';
8
9
  import { WatchDog } from './watch-dog';
9
- export { random, WatchDog, superFactory, Emitter, SyncQueue, sleep, ContinuousTrigger, throttle };
10
+ export { random, WatchDog, superFactory, Emitter, SyncQueue, sleep, ContinuousTrigger, throttle, loop };
package/es/index.js CHANGED
@@ -1,9 +1,10 @@
1
1
  import { Emitter } from './emitter';
2
2
  import { superFactory } from './factory';
3
+ import { loop } from './loop';
3
4
  import { SyncQueue } from './queue';
4
5
  import { random } from './random';
5
6
  import { sleep } from './sleep';
6
7
  import { throttle } from './throttle';
7
8
  import { ContinuousTrigger } from './trigger';
8
9
  import { WatchDog } from './watch-dog';
9
- export { random, WatchDog, superFactory, Emitter, SyncQueue, sleep, ContinuousTrigger, throttle };
10
+ export { random, WatchDog, superFactory, Emitter, SyncQueue, sleep, ContinuousTrigger, throttle, loop };
@@ -0,0 +1 @@
1
+ export declare const loop: (cb: (stop: () => void) => Promise<void>, time: number) => () => void;
@@ -0,0 +1,185 @@
1
+ var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) {
3
+ return value instanceof P ? value : new P(function (resolve) {
4
+ resolve(value);
5
+ });
6
+ }
7
+
8
+ return new (P || (P = Promise))(function (resolve, reject) {
9
+ function fulfilled(value) {
10
+ try {
11
+ step(generator.next(value));
12
+ } catch (e) {
13
+ reject(e);
14
+ }
15
+ }
16
+
17
+ function rejected(value) {
18
+ try {
19
+ step(generator["throw"](value));
20
+ } catch (e) {
21
+ reject(e);
22
+ }
23
+ }
24
+
25
+ function step(result) {
26
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
27
+ }
28
+
29
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
30
+ });
31
+ };
32
+
33
+ var __generator = this && this.__generator || function (thisArg, body) {
34
+ var _ = {
35
+ label: 0,
36
+ sent: function sent() {
37
+ if (t[0] & 1) throw t[1];
38
+ return t[1];
39
+ },
40
+ trys: [],
41
+ ops: []
42
+ },
43
+ f,
44
+ y,
45
+ t,
46
+ g;
47
+ return g = {
48
+ next: verb(0),
49
+ "throw": verb(1),
50
+ "return": verb(2)
51
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
52
+ return this;
53
+ }), g;
54
+
55
+ function verb(n) {
56
+ return function (v) {
57
+ return step([n, v]);
58
+ };
59
+ }
60
+
61
+ function step(op) {
62
+ if (f) throw new TypeError("Generator is already executing.");
63
+
64
+ while (_) {
65
+ try {
66
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
67
+ if (y = 0, t) op = [op[0] & 2, t.value];
68
+
69
+ switch (op[0]) {
70
+ case 0:
71
+ case 1:
72
+ t = op;
73
+ break;
74
+
75
+ case 4:
76
+ _.label++;
77
+ return {
78
+ value: op[1],
79
+ done: false
80
+ };
81
+
82
+ case 5:
83
+ _.label++;
84
+ y = op[1];
85
+ op = [0];
86
+ continue;
87
+
88
+ case 7:
89
+ op = _.ops.pop();
90
+
91
+ _.trys.pop();
92
+
93
+ continue;
94
+
95
+ default:
96
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
97
+ _ = 0;
98
+ continue;
99
+ }
100
+
101
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
102
+ _.label = op[1];
103
+ break;
104
+ }
105
+
106
+ if (op[0] === 6 && _.label < t[1]) {
107
+ _.label = t[1];
108
+ t = op;
109
+ break;
110
+ }
111
+
112
+ if (t && _.label < t[2]) {
113
+ _.label = t[2];
114
+
115
+ _.ops.push(op);
116
+
117
+ break;
118
+ }
119
+
120
+ if (t[2]) _.ops.pop();
121
+
122
+ _.trys.pop();
123
+
124
+ continue;
125
+ }
126
+
127
+ op = body.call(thisArg, _);
128
+ } catch (e) {
129
+ op = [6, e];
130
+ y = 0;
131
+ } finally {
132
+ f = t = 0;
133
+ }
134
+ }
135
+
136
+ if (op[0] & 5) throw op[1];
137
+ return {
138
+ value: op[0] ? op[1] : void 0,
139
+ done: true
140
+ };
141
+ }
142
+ };
143
+
144
+ import { sleep } from '../sleep';
145
+ export var loop = function loop(cb, time) {
146
+ var onOff = true;
147
+
148
+ var stop = function stop() {
149
+ onOff = false;
150
+ };
151
+
152
+ var fn = function fn() {
153
+ return __awaiter(void 0, void 0, void 0, function () {
154
+ return __generator(this, function (_a) {
155
+ switch (_a.label) {
156
+ case 0:
157
+ return [4
158
+ /*yield*/
159
+ , cb(stop)];
160
+
161
+ case 1:
162
+ _a.sent();
163
+
164
+ return [4
165
+ /*yield*/
166
+ , sleep(time)];
167
+
168
+ case 2:
169
+ _a.sent();
170
+
171
+ if (onOff) {
172
+ fn();
173
+ }
174
+
175
+ return [2
176
+ /*return*/
177
+ ];
178
+ }
179
+ });
180
+ });
181
+ };
182
+
183
+ fn();
184
+ return stop;
185
+ };
package/lib/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import { Emitter } from './emitter';
2
2
  import { superFactory } from './factory';
3
+ import { loop } from './loop';
3
4
  import { SyncQueue } from './queue';
4
5
  import { random } from './random';
5
6
  import { sleep } from './sleep';
6
7
  import { throttle } from './throttle';
7
8
  import { ContinuousTrigger } from './trigger';
8
9
  import { WatchDog } from './watch-dog';
9
- export { random, WatchDog, superFactory, Emitter, SyncQueue, sleep, ContinuousTrigger, throttle };
10
+ export { random, WatchDog, superFactory, Emitter, SyncQueue, sleep, ContinuousTrigger, throttle, loop };
package/lib/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.throttle = exports.ContinuousTrigger = exports.sleep = exports.SyncQueue = exports.Emitter = exports.superFactory = exports.WatchDog = exports.random = void 0;
6
+ exports.loop = exports.throttle = exports.ContinuousTrigger = exports.sleep = exports.SyncQueue = exports.Emitter = exports.superFactory = exports.WatchDog = exports.random = void 0;
7
7
 
8
8
  var emitter_1 = require("./emitter");
9
9
 
@@ -23,6 +23,15 @@ Object.defineProperty(exports, "superFactory", {
23
23
  }
24
24
  });
25
25
 
26
+ var loop_1 = require("./loop");
27
+
28
+ Object.defineProperty(exports, "loop", {
29
+ enumerable: true,
30
+ get: function get() {
31
+ return loop_1.loop;
32
+ }
33
+ });
34
+
26
35
  var queue_1 = require("./queue");
27
36
 
28
37
  Object.defineProperty(exports, "SyncQueue", {
@@ -0,0 +1 @@
1
+ export declare const loop: (cb: (stop: () => void) => Promise<void>, time: number) => () => void;
@@ -0,0 +1,193 @@
1
+ "use strict";
2
+
3
+ var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
4
+ function adopt(value) {
5
+ return value instanceof P ? value : new P(function (resolve) {
6
+ resolve(value);
7
+ });
8
+ }
9
+
10
+ return new (P || (P = Promise))(function (resolve, reject) {
11
+ function fulfilled(value) {
12
+ try {
13
+ step(generator.next(value));
14
+ } catch (e) {
15
+ reject(e);
16
+ }
17
+ }
18
+
19
+ function rejected(value) {
20
+ try {
21
+ step(generator["throw"](value));
22
+ } catch (e) {
23
+ reject(e);
24
+ }
25
+ }
26
+
27
+ function step(result) {
28
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
29
+ }
30
+
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+
35
+ var __generator = this && this.__generator || function (thisArg, body) {
36
+ var _ = {
37
+ label: 0,
38
+ sent: function sent() {
39
+ if (t[0] & 1) throw t[1];
40
+ return t[1];
41
+ },
42
+ trys: [],
43
+ ops: []
44
+ },
45
+ f,
46
+ y,
47
+ t,
48
+ g;
49
+ return g = {
50
+ next: verb(0),
51
+ "throw": verb(1),
52
+ "return": verb(2)
53
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
54
+ return this;
55
+ }), g;
56
+
57
+ function verb(n) {
58
+ return function (v) {
59
+ return step([n, v]);
60
+ };
61
+ }
62
+
63
+ function step(op) {
64
+ if (f) throw new TypeError("Generator is already executing.");
65
+
66
+ while (_) {
67
+ try {
68
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
69
+ if (y = 0, t) op = [op[0] & 2, t.value];
70
+
71
+ switch (op[0]) {
72
+ case 0:
73
+ case 1:
74
+ t = op;
75
+ break;
76
+
77
+ case 4:
78
+ _.label++;
79
+ return {
80
+ value: op[1],
81
+ done: false
82
+ };
83
+
84
+ case 5:
85
+ _.label++;
86
+ y = op[1];
87
+ op = [0];
88
+ continue;
89
+
90
+ case 7:
91
+ op = _.ops.pop();
92
+
93
+ _.trys.pop();
94
+
95
+ continue;
96
+
97
+ default:
98
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
99
+ _ = 0;
100
+ continue;
101
+ }
102
+
103
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
104
+ _.label = op[1];
105
+ break;
106
+ }
107
+
108
+ if (op[0] === 6 && _.label < t[1]) {
109
+ _.label = t[1];
110
+ t = op;
111
+ break;
112
+ }
113
+
114
+ if (t && _.label < t[2]) {
115
+ _.label = t[2];
116
+
117
+ _.ops.push(op);
118
+
119
+ break;
120
+ }
121
+
122
+ if (t[2]) _.ops.pop();
123
+
124
+ _.trys.pop();
125
+
126
+ continue;
127
+ }
128
+
129
+ op = body.call(thisArg, _);
130
+ } catch (e) {
131
+ op = [6, e];
132
+ y = 0;
133
+ } finally {
134
+ f = t = 0;
135
+ }
136
+ }
137
+
138
+ if (op[0] & 5) throw op[1];
139
+ return {
140
+ value: op[0] ? op[1] : void 0,
141
+ done: true
142
+ };
143
+ }
144
+ };
145
+
146
+ Object.defineProperty(exports, "__esModule", {
147
+ value: true
148
+ });
149
+ exports.loop = void 0;
150
+
151
+ var sleep_1 = require("../sleep");
152
+
153
+ exports.loop = function (cb, time) {
154
+ var onOff = true;
155
+
156
+ var stop = function stop() {
157
+ onOff = false;
158
+ };
159
+
160
+ var fn = function fn() {
161
+ return __awaiter(void 0, void 0, void 0, function () {
162
+ return __generator(this, function (_a) {
163
+ switch (_a.label) {
164
+ case 0:
165
+ return [4
166
+ /*yield*/
167
+ , cb(stop)];
168
+
169
+ case 1:
170
+ _a.sent();
171
+
172
+ return [4
173
+ /*yield*/
174
+ , sleep_1.sleep(time)];
175
+
176
+ case 2:
177
+ _a.sent();
178
+
179
+ if (onOff) {
180
+ fn();
181
+ }
182
+
183
+ return [2
184
+ /*return*/
185
+ ];
186
+ }
187
+ });
188
+ });
189
+ };
190
+
191
+ fn();
192
+ return stop;
193
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pubo-utils",
3
- "version": "1.0.3-alpha.0",
3
+ "version": "1.0.5-alpha.0",
4
4
  "main": "./lib/index.js",
5
5
  "module": "./es/index.js",
6
6
  "types": "./lib/index.d.ts",
@@ -18,5 +18,5 @@
18
18
  "engines": {
19
19
  "node": ">=8.0.0"
20
20
  },
21
- "gitHead": "8d9cb8b2d13b048ab12185e63d286e2a03c09544"
21
+ "gitHead": "0adede4ec5ab594ec9df7b5415e29690d138052b"
22
22
  }