Add speaker class, completing the black hand

This commit is contained in:
Talia 2021-09-13 18:54:32 +02:00
parent 95c6540ba2
commit 7a061b10d5
No known key found for this signature in database
GPG Key ID: AD727AD22802D0D6
1 changed files with 37 additions and 0 deletions

37
speaker.js Normal file
View File

@ -0,0 +1,37 @@
class Speaker {
#callbacks = new Set()
#immediate
#scheduled = []
constructor(immediate) {
this.#immediate = immediate
}
listen(callback) {
this.#callbacks.add(callback)
}
speak(...args) {
if (this.#immediate) {
for (let callback of this.#callbacks) {
callback(...args)
}
} else {
if (!this.#scheduled.length) {
queueMicrotask(() => {
for (let callback of this.#callbacks) {
callback(...args)
}
this.#scheduled = []
})
}
this.#scheduled.push(args)
}
}
silence(callback) {
this.#callbacks.delete(callbacks)
}
}
export default Speaker