-
Notifications
You must be signed in to change notification settings - Fork 3
/
timeoututils.js
120 lines (104 loc) · 3.64 KB
/
timeoututils.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
/*
Copyright 2015 Norut Northern Research Institute
Author : Ingar Mæhlum Arntzen
This file is part of the Timingsrc module.
Timingsrc is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Timingsrc is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Timingsrc. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict'
/*
TIMEOUT
Wraps setTimeout() to implement improved version
- guarantee that timeout does not wake up too early
- offers precise timeout by "busy"-looping just before timeout
- wraps a single timeout
- clock operates in seconds
- parameters expected in seconds - breaking conformance with setTimeout
- wakes up 3 seconds before on long timeouts to readjust
*/
var Timeout = function (clock, callback, delay, options) {
// clock
this._clock = clock // seconds
var now = this._clock.now() // seconds
// timeout
this._tid = null
this._callback = callback
this._delay_counter = 0
this._options = options || {}
// options
this._options.anchor = this._options.anchor || now // seconds
this._options.early = Math.abs(this._options.early) || 0 // seconds
this._target = this._options.anchor + delay // seconds
// Initialise
var self = this
window.addEventListener('message', this, true) // this.handleEvent
var time_left = this._target - this._clock.now() // seconds
if (time_left > 10) {
// long timeout > 10s - wakeup 3 seconds earlier to readdjust
this._tid = setTimeout(function () { self._ontimeout() }, time_left - 3000)
} else {
// wake up just before
this._tid = setTimeout(function () { self._ontimeout() }, (time_left - self._options.early) * 1000)
}
}
Object.defineProperty(Timeout.prototype, 'target', {
get: function () {
return this._target
}
})
// Internal function
Timeout.prototype._ontimeout = function () {
if (this._tid !== null) {
var time_left = this._target - this._clock.now() // seconds
if (time_left <= 0) {
// callback due
this.cancel()
this._callback()
} else if (time_left > this._options.early) {
// wakeup before target - options early sleep more
var self = this
this._tid = setTimeout(function () { self._ontimeout() }, (time_left - this._options.early) * 1000)
} else {
// wake up just before (options early) - event loop
this._smalldelay()
}
}
}
// Internal function - handler for small delays
Timeout.prototype.handleEvent = function (event) {
if (event.source === window && event.data.indexOf('smalldelaymsg_') === 0) {
event.stopPropagation()
// ignore if timeout has been canceled
var the_tid = parseInt(event.data.split('_')[1])
if (this._tid !== null && this._tid === the_tid) {
this._ontimeout()
}
}
}
Timeout.prototype._smalldelay = function () {
this._delay_counter++
var self = this
window.postMessage('smalldelaymsg_' + self._tid, '*')
}
Timeout.prototype.cancel = function () {
if (this._tid !== null) {
clearTimeout(this._tid)
this._tid = null
var self = this
window.removeEventListener('message', this, true)
}
}
// return module object
module.exports = {
setTimeout: function setTimeout (clock, callback, delay, options) {
return new Timeout(clock, callback, delay, options)
}
}