UNPKG

@tf2pickup-org/mumble-client

Version:
87 lines 2.88 kB
import { ChannelRemove, ChannelState } from '@tf2pickup-org/mumble-protocol'; import { tap } from 'rxjs'; import { Channel } from './channel.js'; import { filterPacket } from './rxjs-operators/filter-packet.js'; export class ChannelManager { client; _channels = new Map(); constructor(client) { this.client = client; this.client.on('socketConnect', (socket) => { this._channels.clear(); socket.packet .pipe(filterPacket(ChannelState), tap(channelState => { this.syncChannelState(channelState); })) .subscribe(); socket.packet .pipe(filterPacket(ChannelRemove), tap(channelRemove => { this.removeChannel(channelRemove); })) .subscribe(); }); } get root() { return this.byId(0); } byId(channelId) { return this._channels.get(channelId); } byName(channelName) { for (const [, channel] of this._channels) { if (channel.name === channelName) { return channel; } } return undefined; } byPath(...channelPath) { const byNameAndParent = (channelName, parent) => { for (const [, channel] of this._channels) { if (channel.name === channelName && channel.parent === parent) { return channel; } } return undefined; }; let lastParent = this.byId(0); for (const element of channelPath) { if (!lastParent) { return undefined; } lastParent = byNameAndParent(element, lastParent.id); } return lastParent; } find(predicate) { return Array.from(this._channels.values()).find(predicate); } findAll(predicate) { return Array.from(this._channels.values()).filter(predicate); } syncChannelState(channelState) { if (channelState.channelId === undefined) { return; } let channel = this.byId(channelState.channelId); if (!channel) { channel = new Channel(this.client, channelState); this._channels.set(channel.id, channel); this.client.emit('channelCreate', channel); } else { const changes = channel.syncState(channelState); if (Object.keys(changes).length > 0) { this.client.emit('channelUpdate', channel, changes); } } } removeChannel(channelRemove) { const channel = this.byId(channelRemove.channelId); if (channel) { this._channels.delete(channelRemove.channelId); this.client.emit('channelRemove', channel); } } } //# sourceMappingURL=channel-manager.js.map