-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
35 lines (28 loc) · 727 Bytes
/
Copy pathindex.js
File metadata and controls
35 lines (28 loc) · 727 Bytes
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
var pushable = require('pull-pushable')
module.exports = function () {
var listeners = []
var closed = false
function notify (message) {
// notify by pushing to all listeners
for (var i = 0; i < listeners.length; i++) {
if (closed) return message
listeners[i].push(message)
}
return message
}
notify.listen = function () {
// create listener with `onClose` handler
var listener = pushable()
listeners.push(listener)
return listener
}
notify.abort = function (err) {
// abort by ending all listeners
closed = true
while (listeners.length) listeners.shift().end(err)
}
notify.end = function () {
return notify.abort(true)
}
return notify
}