-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjchecks.js
345 lines (345 loc) · 8.26 KB
/
jchecks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
(function (exportName) {
/*<function name="Emitter">*/
/**
* @file h5emitter
* @url https://github.com/zswang/h5emitter.git
* event emitter function
* @author
* zswang (http://weibo.com/zswang)
* @version 1.0.0
* @date 2017-05-20
* @license MIT
*/
/**
* 创建事件对象
'''<example>'''
* @example base
```js
var emitter = new h5emitter.Emitter();
emitter.on('click', function (data) {
console.log('on', data);
});
emitter.once('click', function (data) {
console.log('once', data);
});
function bee(data) {
console.log('bee', data);
}
emitter.on('click', bee);
emitter.on('click2', function (data) {
console.log('on', data);
});
emitter.emit('click2', 'hello 1');
// > on hello 1
emitter.emit('click', 'hello 1');
// > on hello 1
// > once hello 1
// > bee hello 1
emitter.emit('click', 'hello 2');
// > on hello 2
// > bee hello 2
emitter.off('click', bee);
emitter.emit('click', 'hello 3');
// > on hello 3
```
'''</example>'''
*/
var Emitter = (function () {
function Emitter() {
/**
* 事件列表
*/
this.callbacks = [];
}
/**
* 事件绑定
*
* @param event 事件名
* @param fn 回调函数
* @return 返回事件实例
*/
Emitter.prototype.on = function (event, fn) {
this.callbacks.push({
event: event,
fn: fn,
});
return this;
};
/**
* 取消事件绑定
*
* @param event 事件名
* @param fn 回调函数
* @return返回事件实例
*/
Emitter.prototype.off = function (event, fn) {
this.callbacks = this.callbacks.filter(function (item) {
return !(item.event === event && item.fn === fn);
});
return this;
};
/**
* 事件绑定,只触发一次
*
* @param event 事件名
* @param fn 回调函数
* @return 返回事件实例
*/
Emitter.prototype.once = function (event, fn) {
function handler() {
this.off(event, handler);
fn.apply(this, arguments);
}
this.on(event, handler);
return this;
};
/**
* 触发事件
*
* @param event 事件名
* @param fn 回调函数
* @return 返回事件实例
*/
Emitter.prototype.emit = function (event) {
var _this = this;
var argv = [];
for (var _i = 1; _i < arguments.length; _i++) {
argv[_i - 1] = arguments[_i];
}
this.callbacks.filter(function (item) {
return item.event === event;
}).forEach(function (item) {
item.fn.apply(_this, argv);
});
return this;
};
return Emitter;
}()); /*</function>*/
/* istanbul ignore next */
var __extends = (function () {
/* jshint proto: true */
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
/*<function name="Checklist">*/
/**
* @example Checklist:base
```js
var flag = 0;
var flagTimer = setInterval(function () {
flag++
}, 1000)
var checklist = new jchecks.Checklist({
items: [{
checker: function () {
console.log('checker 1')
return flag > 0
},
processor: function () {
console.log('processor 1')
return null
},
timeout: 5000,
}, {
}, {
checker: function () {
console.log('checker 2')
return flag > 3
},
processor: function () {
console.log('processor 2')
return null
},
}, {
checker: function () {
console.log('checker 3')
return flag > 4
},
processor: function () {
console.log('processor 3')
return 'error 3'
},
timeout: 0,
}]
})
checklist.once('error', function (error) {
console.log('once error: %j', error)
})
checklist.once('stop', function () {
clearInterval(flagTimer)
// > checker 1
// > checker 1
// > processor 1
// > checker 2
// > checker 2
// > processor 2
// > checker 3
// > processor 3
// > once error: "error 3"
// * done
})
checklist.start()
```
* @example Checklist:timeout
```js
var flag = 0;
var checklist = new jchecks.Checklist({
items: [{
checker: function () {
return flag > 0
},
processor: function () {
return null
},
timeout: 500,
}]
})
checklist.once('error', function (error) {
console.log('once error: %j', error)
})
checklist.once('stop', function () {
// > once error: "timeout"
// * done
})
checklist.start()
```
* @example Checklist:coverage
```js
var checklist = new jchecks.Checklist();
checklist.run();
checklist.next();
checklist.start();
var checklist = new jchecks.Checklist({
timeout: 2000,
items: [{
checker: function() {
},
processor: function () {
}
}]
});
checklist.start();
checklist.start();
console.log(checklist.items.length);
// > 1
```
*/
var Checklist = (function (_super) {
__extends(Checklist, _super);
function Checklist(options) {
var _this = _super.call(this) || this;
options = options || {};
_this._items = options.items || [];
_this._interval = options.interval || 1000;
_this._timeout = options.timeout || 5000;
return _this;
}
Object.defineProperty(Checklist.prototype, "items", {
get: function () {
return this._items;
},
enumerable: true,
configurable: true
});
/**
* 主动运行状态机
*/
Checklist.prototype.run = function () {
if (!this._timer) {
return;
}
var item = this._items[this._stepIndex];
if (!item) {
this.stop();
return;
}
var timeout = item.timeout === undefined ? this._timeout : item.timeout;
var delay = null;
if (timeout > 0) {
delay = Date.now() - this._stepTime;
if (delay > timeout) {
this.error('timeout');
return;
}
}
this.emit('check', item, timeout, delay);
if (item.checker === undefined || item.checker()) {
if (item.processor !== undefined) {
this.emit('process', item);
var error = item.processor();
if (error) {
this.error(error);
return;
}
}
this.next();
}
};
Checklist.prototype.start = function () {
var _this = this;
if (this._timer) {
this.stop();
}
this._timer = setInterval(function () {
_this.run();
}, this._interval);
this._stepIndex = 0;
this._stepTime = Date.now();
this.run();
this.emit('start');
};
/**
* To the next step
*/
Checklist.prototype.next = function () {
this._stepIndex++;
this._stepTime = Date.now();
var item = this._items[this._stepIndex];
if (item) {
this.emit('next');
}
else {
this.stop();
}
};
/**
* 停止检测
*/
Checklist.prototype.stop = function () {
clearInterval(this._timer);
this._timer = 0;
this.emit('stop');
};
/**
* 检测出现异常
*
* @param error 错误信息
*/
Checklist.prototype.error = function (error) {
this.emit('error', error);
this.stop();
};
return Checklist;
}(Emitter)); /*</function>*/
var exports = {
Checklist: Checklist
};
/* istanbul ignore next */
if (typeof define === 'function') {
if (define.amd || define.cmd) {
define(function () {
return exports;
});
}
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = exports;
} else {
window[exportName] = exports;
}
})('jchecks');