-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPauseWrapper.js
102 lines (95 loc) · 2.49 KB
/
PauseWrapper.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
'use strict'
var EventProxy = require('./EventProxy.js')
var EventEmitter = require('events').EventEmitter
var inherits = require('inherits')
module.exports = PauseWrapper
function PauseWrapper (proxied) {
if (!(this instanceof PauseWrapper)) {
return new PauseWrapper(proxied)
}
EventProxy.call(this, proxied)
this._ownEvents['pause'] = true
this._ownEvents['paused'] = true
this._ownEvents['resume'] = true
this.pauseable = true
this._paused = false
this._closed = false
}
inherits(PauseWrapper, EventProxy)
Object.assign(PauseWrapper.prototype, {
_onResume: function (handler) {
if (this._closed) return
if (this._paused || this.resuming) {
return this._pauseStack.push(handler)
}
handler.call(this)
},
_onResumeCb: function (cb, handler) {
var scope = this
this._onResume(function () {
handler.call(scope, function (err, data) {
scope._onResume(function () {
if (!cb) return
cb.call(scope, err, data)
})
})
})
},
emit: function (eventName, payload) {
if (eventName === 'close' || eventName === 'destroy') {
this._closed = true
return EventEmitter.prototype.emit.call(this, eventName, payload)
}
if (this._closed) {
return
}
if (eventName === 'pause' || eventName === 'resume' || eventName === 'paused') {
return EventEmitter.prototype.emit.call(this, eventName, payload)
}
var scope = this
this._onResume(function () { EventEmitter.prototype.emit.call(scope, eventName, payload) })
},
setPaused: function (paused) {
if (paused === this._paused) return false
if (this._resuming) {
var scope = this
this._pauseStack.push(function () { scope.setPaused(paused) })
return false
}
this._paused = paused
this.emit('paused', paused)
if (paused) {
this.emit('pause')
} else {
this.emit('resume')
}
if (paused) {
if (this._pauseStack === undefined) {
this._pauseStack = []
}
} else {
while (this._pauseStack.length > 0 && !this._paused) {
var handler = this._pauseStack.shift()
handler()
}
}
return true
},
pause: function () {
return this.setPaused(true)
},
resume: function () {
return this.setPaused(false)
}
})
Object.defineProperties(PauseWrapper.prototype, {
paused: {
get: function () {
return this._paused
},
set: function (paused) {
this.setPaused(paused)
return this._paused
}
}
})