-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
275 lines (234 loc) · 5.7 KB
/
index.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
'use strict';
var has = Object.prototype.hasOwnProperty
, ms = require('millisecond');
/**
* Timer instance.
*
* @constructor
* @param {Object} timer New timer instance.
* @param {Function} clear Clears the timer instance.
* @param {Function} duration Duration of the timer.
* @param {Function} fn The functions that need to be executed.
* @api private
*/
function Timer(timer, clear, duration, fn) {
this.start = +(new Date());
this.duration = duration;
this.clear = clear;
this.timer = timer;
this.fns = [fn];
}
/**
* Calculate the time left for a given timer.
*
* @returns {Number} Time in milliseconds.
* @api public
*/
Timer.prototype.remaining = function remaining() {
return this.duration - this.taken();
};
/**
* Calculate the amount of time it has taken since we've set the timer.
*
* @returns {Number}
* @api public
*/
Timer.prototype.taken = function taken() {
return +(new Date()) - this.start;
};
/**
* Custom wrappers for the various of clear{whatever} functions. We cannot
* invoke them directly as this will cause thrown errors in Google Chrome with
* an Illegal Invocation Error
*
* @see #2
* @type {Function}
* @api private
*/
function unsetTimeout(id) { clearTimeout(id); }
function unsetInterval(id) { clearInterval(id); }
function unsetImmediate(id) { clearImmediate(id); }
/**
* Simple timer management.
*
* @constructor
* @param {Mixed} context Context of the callbacks that we execute.
* @api public
*/
function Tick(context) {
if (!(this instanceof Tick)) return new Tick(context);
this.timers = {};
this.context = context || this;
}
/**
* Return a function which will just iterate over all assigned callbacks and
* optionally clear the timers from memory if needed.
*
* @param {String} name Name of the timer we need to execute.
* @param {Boolean} clear Also clear from memory.
* @returns {Function}
* @api private
*/
Tick.prototype.tock = function ticktock(name, clear) {
var tock = this;
return function tickedtock() {
if (!(name in tock.timers)) return;
var timer = tock.timers[name]
, fns = timer.fns.slice()
, l = fns.length
, i = 0;
if (clear) tock.clear(name);
else timer.start = +new Date();
for (; i < l; i++) {
fns[i].call(tock.context);
}
};
};
/**
* Add a new timeout.
*
* @param {String} name Name of the timer.
* @param {Function} fn Completion callback.
* @param {Mixed} time Duration of the timer.
* @returns {Tick}
* @api public
*/
Tick.prototype.setTimeout = function timeout(name, fn, time) {
var tick = this
, tock;
if (tick.timers[name]) {
tick.timers[name].fns.push(fn);
return tick;
}
tock = ms(time);
tick.timers[name] = new Timer(
setTimeout(tick.tock(name, true), tock),
unsetTimeout,
tock,
fn
);
return tick;
};
/**
* Add a new interval.
*
* @param {String} name Name of the timer.
* @param {Function} fn Completion callback.
* @param {Mixed} time Interval of the timer.
* @returns {Tick}
* @api public
*/
Tick.prototype.setInterval = function interval(name, fn, time) {
var tick = this
, tock;
if (tick.timers[name]) {
tick.timers[name].fns.push(fn);
return tick;
}
tock = ms(time);
tick.timers[name] = new Timer(
setInterval(tick.tock(name), tock),
unsetInterval,
tock,
fn
);
return tick;
};
/**
* Add a new setImmediate.
*
* @param {String} name Name of the timer.
* @param {Function} fn Completion callback.
* @returns {Tick}
* @api public
*/
Tick.prototype.setImmediate = function immediate(name, fn) {
var tick = this;
if ('function' !== typeof setImmediate) return tick.setTimeout(name, fn, 0);
if (tick.timers[name]) {
tick.timers[name].fns.push(fn);
return tick;
}
tick.timers[name] = new Timer(
setImmediate(tick.tock(name, true)),
unsetImmediate,
0,
fn
);
return tick;
};
/**
* Check if we have a timer set.
*
* @param {String} name
* @returns {Boolean}
* @api public
*/
Tick.prototype.active = function active(name) {
return name in this.timers;
};
/**
* Properly clean up all timeout references. If no arguments are supplied we
* will attempt to clear every single timer that is present.
*
* @param {Arguments} ..args.. The names of the timeouts we need to clear
* @returns {Tick}
* @api public
*/
Tick.prototype.clear = function clear() {
var args = arguments.length ? arguments : []
, tick = this
, timer, i, l;
if (args.length === 1 && 'string' === typeof args[0]) {
args = args[0].split(/[, ]+/);
}
if (!args.length) {
for (timer in tick.timers) {
if (has.call(tick.timers, timer)) args.push(timer);
}
}
for (i = 0, l = args.length; i < l; i++) {
timer = tick.timers[args[i]];
if (!timer) continue;
timer.clear(timer.timer);
timer.fns = timer.timer = timer.clear = null;
delete tick.timers[args[i]];
}
return tick;
};
/**
* Adjust a timeout or interval to a new duration.
*
* @returns {Tick}
* @api public
*/
Tick.prototype.adjust = function adjust(name, time) {
var interval
, tick = this
, tock = ms(time)
, timer = tick.timers[name];
if (!timer) return tick;
interval = timer.clear === unsetInterval;
timer.clear(timer.timer);
timer.start = +(new Date());
timer.duration = tock;
timer.timer = (interval ? setInterval : setTimeout)(tick.tock(name, !interval), tock);
return tick;
};
/**
* We will no longer use this module, prepare your self for global cleanups.
*
* @returns {Boolean}
* @api public
*/
Tick.prototype.end = Tick.prototype.destroy = function end() {
if (!this.context) return false;
this.clear();
this.context = this.timers = null;
return true;
};
//
// Expose the timer factory.
//
Tick.Timer = Timer;
module.exports = Tick;