pubo-utils 1.0.0 → 1.0.3-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.
- package/dist/pubo-utils.js +1 -1
- package/es/emitter/index.d.ts +13 -0
- package/es/emitter/index.js +326 -0
- package/es/factory/index.d.ts +11 -0
- package/es/factory/index.js +43 -0
- package/es/index.d.ts +8 -2
- package/es/index.js +8 -2
- package/es/queue/index.d.ts +8 -0
- package/es/queue/index.js +255 -0
- package/es/random/index.d.ts +1 -1
- package/es/random/index.js +24 -2
- package/es/sleep/index.d.ts +1 -0
- package/es/sleep/index.js +166 -0
- package/es/throttle/index.d.ts +1 -0
- package/es/throttle/index.js +17 -0
- package/es/trigger/index.d.ts +15 -0
- package/es/trigger/index.js +38 -0
- package/es/watch-dog/index.d.ts +10 -8
- package/es/watch-dog/index.js +21 -12
- package/lib/emitter/index.d.ts +13 -0
- package/lib/emitter/index.js +333 -0
- package/lib/factory/index.d.ts +11 -0
- package/lib/factory/index.js +50 -0
- package/lib/index.d.ts +8 -2
- package/lib/index.js +62 -9
- package/lib/queue/index.d.ts +8 -0
- package/lib/queue/index.js +262 -0
- package/lib/random/index.d.ts +1 -1
- package/lib/random/index.js +24 -2
- package/lib/sleep/index.d.ts +1 -0
- package/lib/sleep/index.js +173 -0
- package/lib/throttle/index.d.ts +1 -0
- package/lib/throttle/index.js +26 -0
- package/lib/trigger/index.d.ts +15 -0
- package/lib/trigger/index.js +45 -0
- package/lib/watch-dog/index.d.ts +10 -8
- package/lib/watch-dog/index.js +22 -12
- package/package.json +3 -2
package/dist/pubo-utils.js
CHANGED
|
@@ -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(o){if(n[o])return n[o].exports;var r=n[o]={i:o,l:!1,exports:{}};return t[o].call(r.exports,r,r.exports,e),r.l=!0,r.exports}return e.m=t,e.c=n,e.d=function(t,n,o){e.o(t,n)||Object.defineProperty(t,n,{enumerable:!0,get:o})},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 o=Object.create(null);if(e.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:t}),2&n&&"string"!=typeof t)for(var r in t)e.d(o,r,function(n){return t[n]}.bind(null,r));return o},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 o})),e.d(n,"WatchDog",(function(){return r}));var o=function(){return Math.random().toString(32).slice(2,10)},r=function(){function t(t){var n=t.limit,e=void 0===n?1:n,o=t.onError;this.count=0,this.limit=1,this.limit=e,this.onError=o}return t.prototype.init=function(){var t=this;setInterval((function(){return t.onTimer()}),1e3)},t.prototype.feed=function(){this.count=0},t.prototype.onError=function(){},t.prototype.onTimer=function(){this.count>this.limit&&"function"==typeof this.onError?(this.onError(),this.count=0):this.count++},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 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}()}])}));
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
interface EmitterType {
|
|
2
|
+
on: (event: string, func: any) => string;
|
|
3
|
+
cancel: (id: string) => void;
|
|
4
|
+
emit: (event: string, ...args: any) => any;
|
|
5
|
+
}
|
|
6
|
+
export declare class Emitter implements EmitterType {
|
|
7
|
+
private readonly state;
|
|
8
|
+
private readonly ids;
|
|
9
|
+
on(event: string, func: any): string;
|
|
10
|
+
cancel(id: string): void;
|
|
11
|
+
emit(event: string, ...args: any): Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,326 @@
|
|
|
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
|
+
var __values = this && this.__values || function (o) {
|
|
145
|
+
var s = typeof Symbol === "function" && Symbol.iterator,
|
|
146
|
+
m = s && o[s],
|
|
147
|
+
i = 0;
|
|
148
|
+
if (m) return m.call(o);
|
|
149
|
+
if (o && typeof o.length === "number") return {
|
|
150
|
+
next: function next() {
|
|
151
|
+
if (o && i >= o.length) o = void 0;
|
|
152
|
+
return {
|
|
153
|
+
value: o && o[i++],
|
|
154
|
+
done: !o
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
var __read = this && this.__read || function (o, n) {
|
|
162
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
163
|
+
if (!m) return o;
|
|
164
|
+
var i = m.call(o),
|
|
165
|
+
r,
|
|
166
|
+
ar = [],
|
|
167
|
+
e;
|
|
168
|
+
|
|
169
|
+
try {
|
|
170
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) {
|
|
171
|
+
ar.push(r.value);
|
|
172
|
+
}
|
|
173
|
+
} catch (error) {
|
|
174
|
+
e = {
|
|
175
|
+
error: error
|
|
176
|
+
};
|
|
177
|
+
} finally {
|
|
178
|
+
try {
|
|
179
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
180
|
+
} finally {
|
|
181
|
+
if (e) throw e.error;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return ar;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
var __spread = this && this.__spread || function () {
|
|
189
|
+
for (var ar = [], i = 0; i < arguments.length; i++) {
|
|
190
|
+
ar = ar.concat(__read(arguments[i]));
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return ar;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
import { random } from '../random';
|
|
197
|
+
|
|
198
|
+
var Emitter =
|
|
199
|
+
/** @class */
|
|
200
|
+
function () {
|
|
201
|
+
function Emitter() {
|
|
202
|
+
this.state = {};
|
|
203
|
+
this.ids = {};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
Emitter.prototype.on = function (event, func) {
|
|
207
|
+
if (!this.state[event]) {
|
|
208
|
+
this.state[event] = [];
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (typeof func !== 'function') {
|
|
212
|
+
throw new Error('第二个参数必须为function!');
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
var index = this.state[event].push(func) - 1;
|
|
216
|
+
var key = random(40);
|
|
217
|
+
this.ids[key] = {
|
|
218
|
+
event: event,
|
|
219
|
+
index: index
|
|
220
|
+
};
|
|
221
|
+
return key;
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
Emitter.prototype.cancel = function (id) {
|
|
225
|
+
var _a = this.ids[id] || {},
|
|
226
|
+
event = _a.event,
|
|
227
|
+
index = _a.index;
|
|
228
|
+
|
|
229
|
+
if (!event) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (!Array.isArray(this.state[event])) {
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
this.state[event].splice(index, 1);
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
Emitter.prototype.emit = function (event) {
|
|
241
|
+
var args = [];
|
|
242
|
+
|
|
243
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
244
|
+
args[_i - 1] = arguments[_i];
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
248
|
+
var _a, _b, func, e_1_1;
|
|
249
|
+
|
|
250
|
+
var e_1, _c;
|
|
251
|
+
|
|
252
|
+
return __generator(this, function (_d) {
|
|
253
|
+
switch (_d.label) {
|
|
254
|
+
case 0:
|
|
255
|
+
if (!Array.isArray(this.state[event])) return [3
|
|
256
|
+
/*break*/
|
|
257
|
+
, 8];
|
|
258
|
+
_d.label = 1;
|
|
259
|
+
|
|
260
|
+
case 1:
|
|
261
|
+
_d.trys.push([1, 6, 7, 8]);
|
|
262
|
+
|
|
263
|
+
_a = __values(this.state[event]), _b = _a.next();
|
|
264
|
+
_d.label = 2;
|
|
265
|
+
|
|
266
|
+
case 2:
|
|
267
|
+
if (!!_b.done) return [3
|
|
268
|
+
/*break*/
|
|
269
|
+
, 5];
|
|
270
|
+
func = _b.value;
|
|
271
|
+
if (!(typeof func === 'function')) return [3
|
|
272
|
+
/*break*/
|
|
273
|
+
, 4];
|
|
274
|
+
return [4
|
|
275
|
+
/*yield*/
|
|
276
|
+
, func.apply(void 0, __spread(args))];
|
|
277
|
+
|
|
278
|
+
case 3:
|
|
279
|
+
_d.sent();
|
|
280
|
+
|
|
281
|
+
_d.label = 4;
|
|
282
|
+
|
|
283
|
+
case 4:
|
|
284
|
+
_b = _a.next();
|
|
285
|
+
return [3
|
|
286
|
+
/*break*/
|
|
287
|
+
, 2];
|
|
288
|
+
|
|
289
|
+
case 5:
|
|
290
|
+
return [3
|
|
291
|
+
/*break*/
|
|
292
|
+
, 8];
|
|
293
|
+
|
|
294
|
+
case 6:
|
|
295
|
+
e_1_1 = _d.sent();
|
|
296
|
+
e_1 = {
|
|
297
|
+
error: e_1_1
|
|
298
|
+
};
|
|
299
|
+
return [3
|
|
300
|
+
/*break*/
|
|
301
|
+
, 8];
|
|
302
|
+
|
|
303
|
+
case 7:
|
|
304
|
+
try {
|
|
305
|
+
if (_b && !_b.done && (_c = _a["return"])) _c.call(_a);
|
|
306
|
+
} finally {
|
|
307
|
+
if (e_1) throw e_1.error;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return [7
|
|
311
|
+
/*endfinally*/
|
|
312
|
+
];
|
|
313
|
+
|
|
314
|
+
case 8:
|
|
315
|
+
return [2
|
|
316
|
+
/*return*/
|
|
317
|
+
];
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
return Emitter;
|
|
324
|
+
}();
|
|
325
|
+
|
|
326
|
+
export { Emitter };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
declare type OptionsObject<T, C> = {
|
|
2
|
+
[key in keyof T]: C;
|
|
3
|
+
};
|
|
4
|
+
declare type ProductsObject<T, F> = {
|
|
5
|
+
[key in keyof T]: F;
|
|
6
|
+
};
|
|
7
|
+
declare type Factory<C> = (config: C) => any;
|
|
8
|
+
export declare type CreateFactory<C, F> = <T>(apis: OptionsObject<T, C>) => ProductsObject<T, F>;
|
|
9
|
+
export declare type SuperFactory = <C, F>(factory: Factory<C>) => CreateFactory<C, F>;
|
|
10
|
+
export declare const superFactory: SuperFactory;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
var __values = this && this.__values || function (o) {
|
|
2
|
+
var s = typeof Symbol === "function" && Symbol.iterator,
|
|
3
|
+
m = s && o[s],
|
|
4
|
+
i = 0;
|
|
5
|
+
if (m) return m.call(o);
|
|
6
|
+
if (o && typeof o.length === "number") return {
|
|
7
|
+
next: function next() {
|
|
8
|
+
if (o && i >= o.length) o = void 0;
|
|
9
|
+
return {
|
|
10
|
+
value: o && o[i++],
|
|
11
|
+
done: !o
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export var superFactory = function superFactory(factory) {
|
|
19
|
+
return function (options) {
|
|
20
|
+
var e_1, _a;
|
|
21
|
+
|
|
22
|
+
var product = {};
|
|
23
|
+
|
|
24
|
+
try {
|
|
25
|
+
for (var _b = __values(Object.keys(options)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
26
|
+
var key = _c.value;
|
|
27
|
+
product[key] = factory(options[key]);
|
|
28
|
+
}
|
|
29
|
+
} catch (e_1_1) {
|
|
30
|
+
e_1 = {
|
|
31
|
+
error: e_1_1
|
|
32
|
+
};
|
|
33
|
+
} finally {
|
|
34
|
+
try {
|
|
35
|
+
if (_c && !_c.done && (_a = _b["return"])) _a.call(_b);
|
|
36
|
+
} finally {
|
|
37
|
+
if (e_1) throw e_1.error;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return product;
|
|
42
|
+
};
|
|
43
|
+
};
|
package/es/index.d.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
import { Emitter } from './emitter';
|
|
2
|
+
import { superFactory } from './factory';
|
|
3
|
+
import { SyncQueue } from './queue';
|
|
1
4
|
import { random } from './random';
|
|
2
|
-
import
|
|
3
|
-
|
|
5
|
+
import { sleep } from './sleep';
|
|
6
|
+
import { throttle } from './throttle';
|
|
7
|
+
import { ContinuousTrigger } from './trigger';
|
|
8
|
+
import { WatchDog } from './watch-dog';
|
|
9
|
+
export { random, WatchDog, superFactory, Emitter, SyncQueue, sleep, ContinuousTrigger, throttle };
|
package/es/index.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
import { Emitter } from './emitter';
|
|
2
|
+
import { superFactory } from './factory';
|
|
3
|
+
import { SyncQueue } from './queue';
|
|
1
4
|
import { random } from './random';
|
|
2
|
-
import
|
|
3
|
-
|
|
5
|
+
import { sleep } from './sleep';
|
|
6
|
+
import { throttle } from './throttle';
|
|
7
|
+
import { ContinuousTrigger } from './trigger';
|
|
8
|
+
import { WatchDog } from './watch-dog';
|
|
9
|
+
export { random, WatchDog, superFactory, Emitter, SyncQueue, sleep, ContinuousTrigger, throttle };
|