UNPKG

@casual-simulation/aux-vm-browser

Version:

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

104 lines 3.73 kB
import { BehaviorSubject, merge } from 'rxjs'; import { mergeMap, bufferTime } from 'rxjs/operators'; import { IDE_PORTAL, isPortalScript, isScript, isFormula, KNOWN_TAG_PREFIXES, getScriptPrefix, hasValue, } from '@casual-simulation/aux-common'; import { sortBy } from 'es-toolkit/compat'; /** * Defines a class that manages the bot panel. */ export class IdePortalManager { /** * Gets an observable that resolves whenever the list of selected bots is updated. */ get itemsUpdated() { return this._itemsUpdated; } get items() { return this._itemsUpdated.value; } /** * 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._itemsUpdated = new BehaviorSubject({ hasPortal: false, items: [], }); this._subs.push(this._calculateItemsUpdated().subscribe(this._itemsUpdated)); } unsubscribe() { if (!this.closed) { this.closed = true; this._subs.forEach((s) => s.unsubscribe()); this._subs = null; } } _findMatchingItems() { if (!this._helper.userBot) { return { hasPortal: false, items: [], }; } const portalValue = this._helper.userBot.tags[IDE_PORTAL]; if (portalValue) { let items = []; for (let bot of this._helper.objects) { if (bot.id === this._helper.userId) { continue; } for (let tag in bot.values) { const val = bot.tags[tag]; if (portalValue === true || portalValue === 'true' || isPortalScript(portalValue, bot.tags[tag])) { let item = { type: 'tag', botId: bot.id, tag: tag, name: tag, key: `${tag}.${bot.id}`, }; let prefix = getScriptPrefix(KNOWN_TAG_PREFIXES, val); if (hasValue(prefix)) { item.prefix = prefix; } if (isScript(val)) { item.isScript = true; } else if (isFormula(val)) { item.isFormula = true; } items.push(item); } } } return { hasPortal: true, items: sortBy(items, (item) => item.key), }; } return { hasPortal: false, items: [], }; } _calculateItemsUpdated() { 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 () => { const items = this._findMatchingItems(); return items; })); } } //# sourceMappingURL=IdePortalManager.js.map