-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
279 lines (232 loc) · 7.66 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
276
277
278
279
/*! Copyright (c) 2017-2018 Simon Dawson */
/** N.B. Public API in use:
*
* - constructor, with element and following options: timeout, storage_key
* - 'destroy' method
* - 'reset' method
* - 'idle' event
*/
import * as Utils from './utils'
/** Idle timer */
export default class IdleTimer {
/** Constructor */
constructor(
{
element = document, /* The element to be monitored for activity */
initially_idle = false, /* Is the timer initially idle? */
timeout = 30000, /* Duration (ms) before user considered idle */
events = [
'mousemove',
'keydown',
'wheel',
'DOMMouseScroll',
'mousewheel',
'mousedown'
], /* Array of monitored events */
storage_key = null /* localStorage key to use for synchronising this timer between browser tabs/windows */
} = {}
) {
console.log('idle-timer: constructor')
/* Initialise member variables */
Object.assign(this, {
element,
initially_idle,
timeout,
events,
storage_key
})
this.timeout_id = null /* setTimeout handle */
this.pageX = null /* Cached mouse event coordinate */
this.pageY = null /* Cached mouse event coordinate */
this.__install_handlers()
this.reset()
}
/** Stop the timer: remove event handlers and cancel pending timeout */
destroy() {
console.log('idle-timer: destroy')
/* Clear any pending timeout */
this.__clear()
this.__uninstall_handlers()
return this
}
/** Install event handlers */
__install_handlers() {
// console.log('idle-timer: install handlers')
const handler = (e) => { this.__handle_event(e) }
this.events.forEach((item) => {
this.element.addEventListener(
item,
handler,
Utils.passive_supported() ? { passive: true } : false
)
})
if (this.storage_key) {
window.addEventListener('storage', handler)
}
return this
}
/* Uninstall event handlers */
__uninstall_handlers() {
// console.log('idle-timer: uninstall handlers')
const handler = (e) => { this.__handle_event(e) }
this.events.forEach((item) => {
this.element.removeEventListener(item, handler)
})
if (this.storage_key) {
window.removeEventListener('storage', handler)
}
return this
}
/** Toggles the idle state and fires an appropriate event */
__toggle_idle(event = null) {
// console.log('idle-timer: toggle idle state')
/* Toggle state */
this.idle = !this.is_idle()
/* Store toggle state timestamp */
this.last_toggled_at = Utils.now()
/* Dispatch a custom event, with state */
const event_name =
'idle-timer:' + (this.is_idle() ? 'idle' : 'active')
const custom_event =
new CustomEvent(event_name,
{
detail: {
timer: Object.assign({}, this),
event: event
}
})
this.element.dispatchEvent(custom_event)
}
/** Handle an event indicating that the user isn't idle */
__handle_event(event) {
// console.log('idle-timer: handle event')
if (this.__is_paused()) {
/* Ignore events for now */
return
}
if ('storage' === event.type && event.key !== this.storage_key) {
return
}
/*
mousemove is buggy: it can be triggered when it should not
This typically happens 115-150ms after idle triggered
*/
if ('mousemove' === event.type) {
if (event.pageX === this.pageX && event.pageY === this.pageY) {
/* Coordinates unchanged: false alarm */
return
}
if ('undefined' === typeof event.pageX &&
'undefined' === typeof event.pageY) {
/* Coordinates invalid: false alarm */
return
}
if (this.get_elapsed_time() < 200) {
/* Sub-200ms start-stop motion: false alarm */
return
}
}
/* Clear any pending timeout */
this.__clear()
/* If the idle timer is enabled, toggle state */
if (this.is_idle()) {
this.__toggle_idle(event)
}
/* Store when user was last active */
this.last_activity_at = Utils.now()
/* Update mouse coordinates */
this.pageX = event.pageX
this.pageY = event.pageY
/* Synchronise last activity timestamp between browser tabs/windows */
if ('storage' !== event.type && this.storage_key) {
if ('undefined' !== typeof localStorage) {
localStorage.setItem(this.storage_key,
this.get_last_active_time())
}
}
/* Set a new timeout */
this.__start()
}
__start(duration = null) {
// console.log('idle-timer: start')
if (null === duration) {
duration = this.timeout
}
this.timeout_id = setTimeout(
() => { this.__toggle_idle(null) },
duration
)
return this
}
__clear() {
// console.log('idle-timer: clear')
if (this.timeout_id) {
clearTimeout(this.timeout_id)
this.timeout_id = null
}
return this
}
/** Restore initial settings and restart timer */
reset() {
console.log('idle-timer: reset')
/* Reset settings */
this.idle = this.initially_idle
this.last_activity_at = this.last_toggled_at = Utils.now()
this.remaining = null
/* Reset timers */
this.__clear()
if (!this.is_idle()) {
this.__start()
}
return this
}
/** Pause timer, and cache remaining time until idle */
pause() {
console.log('idle-timer: pause')
if (!this.__is_paused()) {
/* Calculate and cache remaining time */
this.remaining = this.timeout - this.get_elapsed_time()
/* Clear any pending timeout */
this.__clear()
}
return this
}
/** Resume timer with cached remaining time until idle */
resume() {
console.log('idle-timer: resume')
if (this.__is_paused()) {
/* Start timer */
if (!this.is_idle()) {
this.__start(this.remaining)
}
/* Clear cached remaining time */
this.remaining = null
}
return this
}
__is_paused() {
return null != this.remaining
}
/** Get the time remaining until becoming idle */
get_remaining_time() {
if (this.is_idle()) {
/* Already idle: no time remaining */
return 0
}
if (this.__is_paused()) {
/* Use the cached remaining time */
return this.remaining
}
/* Calculate remaining; if negative, state didn't finish toggling */
return Math.max(0, this.timeout - (Utils.now() - this.get_last_active_time()))
}
get_elapsed_time() {
return Utils.now() - this.last_toggled_at
}
get_last_active_time() {
return this.last_activity_at
}
is_idle() {
return this.idle
}
}