forked from nicksz/jTime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjTime.js
354 lines (265 loc) · 9.17 KB
/
jTime.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
346
347
348
349
350
351
352
353
354
/*
jTime: run your code when you want it to run.
cb = callback
tCb = trueCallback
fCb = falseCallback
startCb = startCallback
endCb = endCallback
actionObj = cb | { tcb: function, fCb: function }
Plan to add { errCb, tEvent, fEvent, errEvent } and perhaps others in future.
Caller can include any or all or none of these. e.g. can have a tCb or tEvent or both for the true case.
context = the "this" object to be used in the callback. The default context
object in browsers is window for callbacks invoked in setTimeout().
*/
var jTime = (function() {
function JTime() {
function Poller() {
this.poller;
this.killMe = false;
this.clear = function() {
this.killMe = true;
}
}
this.mySetInterval = function(period, callback, context) {
var start = new Date().getTime();
var time = 0;
var elapsed = '0.0';
// this sets p.killMe to false
var p = new Poller();
function next()
{
if (p.killMe == false) {
callback.call(context);
time += period;
elapsed = Math.floor(time / period) / 10;
if (Math.round(elapsed) == elapsed) { elapsed += '.0'; };
var diff = (new Date().getTime() - start) - time;
if (p.killMe == false) { p.poller = setTimeout(next, period - diff) };
}
}
p.poller = setTimeout(next, period);
return p;
}
} // JTime()
var jTime = new JTime();
// aliases
var T_ = JTime;
JTime.prototype.clear = function(p) {
p.clear();
}
JTime.prototype.becomes = function(pollInterval, condition, actionObj, context) {
var p;
if (typeof(actionObj.startCb) == "function") {
startCb.call(context);
};
p = t_.mySetInterval(pollInterval, function() {
if (condition()) {
if (typeof(actionObj) === "function") { actionObj.call(context); };
if (typeof(actionObj.tCb) === "function") { actionObj.tCb.call(context); };
t_.clear(p);
} else {
if (typeof(actionObj.fCb) === "function") { actionObj.fCb.call(context); };
}
}, context);
return p;
}
JTime.prototype.until = function(pollInterval, condition, callback, context) {
var p;
if (typeof(callback) == "function") {
p = t_.becomes(pollInterval, condition,
{ fCb: callback }, context);
};
return p;
}
JTime.prototype.when = function(pollInterval, timeout, condition, actionObj, context) {
var p;
var curTime = (new Date()).getTime();
if (typeof(actionObj.startCb) == "function") {
startCb();
};
p = t_.mySetInterval(pollInterval, function() {
if (condition()) {
if (typeof(actionObj) === "function") { actionObj.call(context); };
if (typeof(actionObj.tCb) === "function") { actionObj.tCb.call(context); };
} else {
if (typeof(actionObj.fCb) === "function") { actionObj.fCb.call(context); };
}
}, context);
setTimeout(function() {
t_.clear(p);
if (typeof(actionObj.endCb) === "function") {
actionObj.endCb.call(context);
}
},
timeout-curTime);
return p;
}
// TODO: actionObj when whenCondition, run until untilCondition
JTime.prototype.whenUntil = function(pollInterval, untilCondition,
whenCondition, actionObj) {}
JTime.prototype.every = function(period, start, timeout, actionObj, context) {
var p;
var curTime = (new Date()).getTime();
if (timeout <= period + start) {
console.log("jTime.every() ERROR: timeout must be greater than period + start");
return;
};
if (typeof(actionObj.startCb) == "function") {
startCb.call(context);
};
setTimeout(function() {
p = t_.mySetInterval(period, function() {
if (typeof(actionObj) === "function") {
actionObj.call(context);
};
if (typeof(actionObj.cb) === "function") { actionObj.cb.call(context); };
}, context);
}, start-curTime);
setTimeout(function() {
t_.clear(p);
if (typeof(actionObj.endCb) === "function") {
actionObj.endCb.call(context);
}
},
timeout-curTime);
return p;
}
/* TODO:
performs actionObjs.tCb when changes from false to true
performs actionObjs.fCb when changes from true to false
performs actionObjs.ncCB when remains true or false from last poll
*/
JTime.prototype.whenChanges = function(pollInterval, condition, actionObj, context) {};
/*
TODO:
Checks whether a condition occurs or event fires before a deadline.
Catches/binds the event, handler checks the time.
expected is { event: event, condition: function }
actionObj is { firedCb: ..., missedCb: ..., errCb:...,
firedE: event, missedE: event, errE: event }
*/
JTime.prototype.byDeadline = function(pI, deadline, expected, actionObj, context) {}
/* TODO: fires based on whichever conditions and/or events happen first
{ conditions: actionObjs }
{ events: actionObjs }
*/
JTime.prototype.happensFirst = function(pollInterval, conditionsActionsMap, eventsActionsMaps, context) {};
/* at(), before(), and after() should trigger from system clock, not setTimeout(),
so need to poll but this kind of polling for this purpose is
very inefficient
TODO: exponential or adaptive polling
//####### TODO: MAKE ORTHOGONAL W/REST OF LIBRARY
function(pollInterval, time, callback, context)
*/
JTime.prototype.at = function(time, pollInterval, callback, context) {
var p = this.becomes(pollInterval,
function() {
var curDate = new Date();
return (curDate.getTime() >= time);
},
callback, context);
return p;
}
JTime.prototype.after = function(time, pollInterval, callback, context) {
var p = this.when(pollInterval,
time + 29999, //### isn't supposed to time out at all...
function() {
return ((new Date()).getTime() > time)
},
callback, context);
return p;
}
JTime.prototype.before = function(time, pollInterval, callback, context) {
var p = this.when(pollInterval,
time + 2999, // timeout after big 3 second fudge factor
function() {
return ((new Date()).getTime() < time)
},
callback, context);
return p;
}
JTime.prototype.Schedule = function() {
this.sched = new Array();
this.timeouts = new Array();
this.addTime = function(time) {
this.sched.push(time);
};
// doesn't matter if the array is sorted
this.setTimes = function(timeArray) {
this.sched = timeArray;
};
this.run = function(callback, context) {
for (i = 0; i<this.sched.length; i++) {
t_.at(this.sched[i], 100, callback, context);
};
};
}
JTime.prototype.timeIsWithin = function(min, max) {
var curTime = (new Date()).getTime();
return ((curTime > min) && (curTime < max));
}
JTime.prototype.longEach = function(pollInterval, array, step, callback, context) {
var p = t_.until(pollInterval,
function() { return (array.length === 0) },
function() {
for (var i=0; (i < step && array.length>0); i++) {
var item = array.shift();
callback.call(context, item);
}
});
return p;
}
// should work both in browswer and in node.js
function bind(emittingObj, event, eventHandler){
if (emittingObj.addEventListener) {
emittingObj.addEventListener(event, eventHandler, false);
} else if (emittingObj.attachEvent) {
event = "on" + event;
emittingObj.attachEvent(event, eventHandler);
}
}
function unbind(emittingObj, event, eventHandler) {};
/* TODO: test bind list of events to emittingObj
unbind events
custom events
*/
JTime.prototype.on = function(emitters, event, eventHandler) {
/*### TODO: check to see if it's a new kind of event, and if so create it */
/*### create using event or the optional object event.condition, event.event */
if (typeof(emitters) == "Array") {
emitters.forEach(function(emittingObj) {
bind(emittingObj, event, eventHandler);
} );
} else {
bind(emitters, event, eventHandler);
}
}
JTime.prototype.off = function(emitters, event, eventHandler) { }
// TODO:
// event handler that triggers only if condition is true when event fires
JTime.prototype.onEventAndContition = function() {}
JTime.prototype.showCurrentTime = function() {
return (new Date()).getTime() % 100000;
}
return jTime;
}());
var t_ = jTime; // alias
// exports for node.js
if(typeof exports != 'undefined'){
this.becomes = t_.becomes;
this.until = t_.until;
this.every = t_.every;
this.when = t_.when;
this.at = t_.at;
this.after = t_.after;
this.before = t_.before;
this.Schedule = t_.Schedule;
this.on = t_.on;
this.longEach = t_.longEach;
}
/* doesn't work -- can be made to work?
if(typeof exports != 'undefined'){
exports.jTime = jTime;
exports.t_ = t_;
}
*/