UNPKG

@casual-simulation/aux-vm-browser

Version:

A set of utilities required to securely run an AUX in a web browser.

84 lines 3.04 kB
import { BehaviorSubject, merge } from 'rxjs'; import { mergeMap, bufferTime } from 'rxjs/operators'; import { filterBotsBySelection, SHEET_PORTAL, } from '@casual-simulation/aux-common'; /** * Defines a class that manages the bot panel. */ export class BotPanelManager { get state() { return this._botsUpdated.value; } /** * Gets an observable that resolves whenever the list of selected bots is updated. */ get botsUpdated() { return this._botsUpdated; } /** * Creates a new bot panel manager. * @param watcher The bot watcher to use. * @param helper The bot helper to use. * @param bufferEvents Whether to buffer the update events. */ constructor(watcher, helper, bufferEvents = true) { this._subs = []; this.closed = false; this._watcher = watcher; this._helper = helper; this._buffer = bufferEvents; this._botsUpdated = new BehaviorSubject({ bots: [], isDiff: false, hasPortal: false, dimension: null, isSingleBot: false, }); this._subs.push(this._calculateBotsUpdated().subscribe(this._botsUpdated)); } unsubscribe() { if (!this.closed) { this.closed = true; this._subs.forEach((s) => s.unsubscribe()); this._subs = null; } } _calculateBotsUpdated() { const allBotsSelectedUpdatedAddedAndRemoved = merge(this._watcher.botsDiscovered, this._watcher.botsUpdated, this._watcher.botsRemoved); const bufferedEvents = this._buffer ? allBotsSelectedUpdatedAddedAndRemoved.pipe(bufferTime(10)) : allBotsSelectedUpdatedAddedAndRemoved; return bufferedEvents.pipe(mergeMap(async () => { if (this._helper.userBot) { const dimension = this._helper.userBot.values[SHEET_PORTAL]; if (!!dimension && dimension !== true) { const bots = filterBotsBySelection(this._helper.objects, dimension); const singleBot = bots.length === 1 && bots[0].id === dimension; return { bots: bots, hasPortal: true, dimension: dimension, isDiff: false, isSingleBot: singleBot, }; } else if (dimension === true) { return { bots: this._helper.objects, hasPortal: true, dimension: null, isDiff: false, isSingleBot: false, }; } } return { bots: [], hasPortal: false, dimension: null, isDiff: false, isSingleBot: false, }; })); } } //# sourceMappingURL=BotPanelManager.js.map