UNPKG

@casual-simulation/aux-vm-browser

Version:

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

161 lines 5.26 kB
import { RemoteAuxVM } from '@casual-simulation/aux-vm-client/vm'; import { proxy, releaseProxy, wrap } from 'comlink'; import { Subject, Subscription } from 'rxjs'; /** * Gets an AUX VM that is able to communicate with a proxied aux channel. */ export class ConnectableAuxVM { get id() { return this._id; } get origin() { return this._origin; } get configBotId() { return this._configBotId; } constructor(id, origin, configBotId, port) { this._id = id; this._origin = origin; this._configBotId = configBotId; this._proxy = wrap(port); this._localEvents = new Subject(); this._deviceEvents = new Subject(); this._stateUpdated = new Subject(); this._versionUpdated = new Subject(); this._connectionStateChanged = new Subject(); this._onError = new Subject(); this._subVMAdded = new Subject(); this._subVMRemoved = new Subject(); this._subVMMap = new Map(); this._onAuthMessage = new Subject(); this._sub = new Subscription(() => { this._proxy[releaseProxy](); }); } get subVMAdded() { return this._subVMAdded; } get subVMRemoved() { return this._subVMRemoved; } createEndpoint() { throw new Error('Method not implemented.'); } get connectionStateChanged() { return this._connectionStateChanged; } get onError() { return this._onError; } get onAuthMessage() { return this._onAuthMessage; } async init() { await this._proxy.registerListeners(proxy((events) => this._localEvents.next(events)), proxy((events) => this._deviceEvents.next(events)), proxy((state) => this._stateUpdated.next(state)), proxy((version) => this._versionUpdated.next(version)), proxy((state) => this._connectionStateChanged.next(state)), proxy((err) => this._onError.next(err)), proxy((channel) => this._handleAddedSubChannel(channel)), proxy((id) => this._handleRemovedSubChannel(id)), proxy((message) => this._onAuthMessage.next(message))); } unsubscribe() { this._sub.unsubscribe(); } get closed() { return this._sub.closed; } /** * The observable list of events that should be produced locally. */ get localEvents() { return this._localEvents; } get deviceEvents() { return this._deviceEvents; } /** * The observable list of bot state updates from this simulation. */ get stateUpdated() { return this._stateUpdated; } get versionUpdated() { return this._versionUpdated; } /** * Sends the given list of events to the simulation. * @param events The events to send to the simulation. */ async sendEvents(events) { if (!this._proxy) return null; return await this._proxy.sendEvents(events); } /** * Executes a shout with the given event name on the given bot IDs with the given argument. * Also dispatches any actions and errors that occur. * Returns the results from the event. * @param eventName The name of the event. * @param botIds The IDs of the bots that the shout is being sent to. * @param arg The argument to include in the shout. */ async shout(eventName, botIds, arg) { if (!this._proxy) return null; return await this._proxy.shout(eventName, botIds, arg); } async formulaBatch(formulas) { if (!this._proxy) return null; return await this._proxy.formulaBatch(formulas); } async forkAux(newId) { if (!this._proxy) return null; return await this._proxy.forkAux(newId); } async exportBots(botIds) { if (!this._proxy) return null; return await this._proxy.exportBots(botIds); } /** * Exports the causal tree for the simulation. */ async export() { if (!this._proxy) return null; return await this._proxy.export(); } async getTags() { if (!this._proxy) return null; return await this._proxy.getTags(); } async updateDevice(device) { if (!this._proxy) return null; return await this._proxy.updateDevice(device); } _createSubVM(id, origin, configBotId, channel) { return new RemoteAuxVM(id, origin, configBotId, channel); } sendAuthMessage(message) { return this._proxy.sendAuthMessage(message); } async _handleAddedSubChannel(subChannel) { const { id, configBotId } = await subChannel.getInfo(); const channel = (await subChannel.getChannel()); const subVM = { id, vm: this._createSubVM(id, this.origin, configBotId, channel), channel, }; this._subVMMap.set(id, subVM); this._subVMAdded.next(subVM); } async _handleRemovedSubChannel(channelId) { const vm = this._subVMMap.get(channelId); if (vm) { this._subVMMap.delete(channelId); this._subVMRemoved.next(vm); } } } //# sourceMappingURL=ConnectableAuxVM.js.map