-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeaker.js
More file actions
48 lines (41 loc) · 794 Bytes
/
speaker.js
File metadata and controls
48 lines (41 loc) · 794 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
36
37
38
39
40
41
42
43
44
45
46
47
48
export class Speaker {
_callbacks = new Set()
_scheduled = []
_retain
constructor(...initial) {
this._retain = initial
}
listen(callback) {
this._callbacks.add(callback)
return this._retain
}
speak(...args) {
if (!this._scheduled.length) {
queueMicrotask(() => {
for (const args of this._scheduled) {
for (const callback of this._callbacks) {
callback(...args)
}
this._retain = args
}
this._scheduled = []
})
}
this._scheduled.push(args)
}
forget(callback) {
this._callbacks.delete(callback)
}
silence() {
this._callbacks.clear()
}
}
export class ImmediateSpeaker extends Speaker {
speak(...args) {
for (const callback of this._callbacks) {
callback(...args)
}
this._retain = args
}
}
export default Speaker