diff --git a/src/client/voice/VoiceBroadcast.js b/src/client/voice/VoiceBroadcast.js index c709eb64c..421988a65 100644 --- a/src/client/voice/VoiceBroadcast.js +++ b/src/client/voice/VoiceBroadcast.js @@ -1,5 +1,6 @@ const EventEmitter = require('events'); const BroadcastAudioPlayer = require('./player/BroadcastAudioPlayer'); +const DispatcherSet = require('./util/DispatcherSet'); /** * A voice broadcast can be played across multiple voice connections for improved shared-stream efficiency. @@ -23,7 +24,7 @@ class VoiceBroadcast extends EventEmitter { * @type {Client} */ this.client = client; - this.dispatchers = new Set(); + this.dispatchers = new DispatcherSet(this); this.player = new BroadcastAudioPlayer(this); } diff --git a/src/client/voice/util/DispatcherSet.js b/src/client/voice/util/DispatcherSet.js new file mode 100644 index 000000000..a1ab7e943 --- /dev/null +++ b/src/client/voice/util/DispatcherSet.js @@ -0,0 +1,40 @@ +const { Events } = require('../../../util/Constants'); + +/** + * A "store" for handling broadcast dispatcher (un)subscription + * @private + */ +class DispatcherSet extends Set { + constructor(broadcast) { + super(); + /** + * The broadcast that this set belongs to + * @type {VoiceBroadcast} + */ + this.broadcast = broadcast; + } + + add(dispatcher) { + super.add(dispatcher); + /** + * Emitted whenever a stream dispatcher subscribes to the broadcast. + * @event VoiceBroadcast#subscribe + * @param {StreamDispatcher} dispatcher The subscribed dispatcher + */ + this.broadcast.emit(Events.VOICE_BROADCAST_SUBSCRIBE, dispatcher); + return this; + } + + delete(dispatcher) { + const ret = super.delete(dispatcher); + /** + * Emitted whenever a stream dispatcher unsubscribes to the broadcast. + * @event VoiceBroadcast#unsubscribe + * @param {StreamDispatcher} dispatcher The unsubscribed dispatcher + */ + if (ret) this.broadcast.emit(Events.VOICE_BROADCAST_UNSUBSCRIBE, dispatcher); + return ret; + } +} + +module.exports = DispatcherSet; diff --git a/src/util/Constants.js b/src/util/Constants.js index 69e4f4995..bf6106cc1 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -234,6 +234,8 @@ exports.Events = { USER_GUILD_SETTINGS_UPDATE: 'clientUserGuildSettingsUpdate', PRESENCE_UPDATE: 'presenceUpdate', VOICE_STATE_UPDATE: 'voiceStateUpdate', + VOICE_BROADCAST_SUBSCRIBE: 'subscribe', + VOICE_BROADCAST_UNSUBSCRIBE: 'unsubscribe', TYPING_START: 'typingStart', TYPING_STOP: 'typingStop', DISCONNECT: 'disconnect',