1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | class ProgressTimeout {
|
8 | constructor (timeout, timeoutHandler) {
|
9 | this._timeout = timeout
|
10 | this._onTimedOut = timeoutHandler
|
11 | this._isDone = false
|
12 | this._aliveTimer = null
|
13 | this._onTimedOut = this._onTimedOut.bind(this)
|
14 | }
|
15 |
|
16 | progress () {
|
17 |
|
18 |
|
19 |
|
20 | if (this._isDone) return
|
21 |
|
22 | if (this._timeout > 0) {
|
23 | if (this._aliveTimer) clearTimeout(this._aliveTimer)
|
24 | this._aliveTimer = setTimeout(this._onTimedOut, this._timeout)
|
25 | }
|
26 | }
|
27 |
|
28 | done () {
|
29 | if (this._aliveTimer) {
|
30 | clearTimeout(this._aliveTimer)
|
31 | this._aliveTimer = null
|
32 | }
|
33 | this._isDone = true
|
34 | }
|
35 | }
|
36 |
|
37 | module.exports = ProgressTimeout
|