import { BotAction , LocalActions , RuntimeStateVersion , StateUpdatedEvent , } from '@casual-simulation/aux-common' ; import { AuxVM , AuxChannel , AuxChannelErrorType , AuxUser , StoredAux , ChannelActionResult , } from '@casual-simulation/aux-vm' ; import { DeviceAction , StatusUpdate } from '@casual-simulation/causal-trees' ; import { proxy , releaseProxy , Remote , wrap } from 'comlink' ; import { Observable , Subject , Subscription } from 'rxjs' ; /**
 * Gets an AUX VM that is able to communicate with a proxied aux channel.
 */ export class ConnectableAuxVM implements AuxVM { private _localEvents : Subject < LocalActions [ ] > ; private _deviceEvents : Subject < DeviceAction [ ] > ; private _connectionStateChanged : Subject < StatusUpdate > ; private _stateUpdated : Subject < StateUpdatedEvent > ; private _versionUpdated : Subject < RuntimeStateVersion > ; private _onError : Subject < AuxChannelErrorType > ; private _proxy : Remote < AuxChannel > ; private _port : MessagePort ; private _sub : Subscription ; constructor ( id , port ) { this . id = id ; this . _proxy = wrap ( port ) ; this . _localEvents = new Subject < LocalActions [ ] > ( ) ; this . _deviceEvents = new Subject < DeviceAction [ ] > ( ) ; this . _stateUpdated = new Subject < StateUpdatedEvent > ( ) ; this . _versionUpdated = new Subject < RuntimeStateVersion > ( ) ; this . _connectionStateChanged = new Subject < StatusUpdate > ( ) ; this . _onError = new Subject < AuxChannelErrorType > ( ) ; this . _sub = new Subscription ( ( ) => { this . _proxy [ releaseProxy ] ( ) ; } ) ; } id : string ; get connectionStateChanged ( ) : Observable < StatusUpdate > { return this . _connectionStateChanged ; } get onError ( ) : Observable < AuxChannelErrorType > { return this . _onError ; } 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 ) ) ) ; } unsubscribe ( ) { this . _sub . unsubscribe ( ) ; } get closed ( ) : boolean { return this . _sub . closed ; } /**
     * The observable list of events that should be produced locally.
     */ get localEvents ( ) : Observable < LocalActions [ ] > { return this . _localEvents ; } get deviceEvents ( ) : Observable < DeviceAction [ ] > { return this . _deviceEvents ; } /**
     * The observable list of bot state updates from this simulation.
     */ get stateUpdated ( ) : Observable < StateUpdatedEvent > { return this . _stateUpdated ; } get versionUpdated ( ) : Observable < RuntimeStateVersion > { return this . _versionUpdated ; } async setUser ( user ) { if ( ! this . _proxy ) return null ; return await this . _proxy . setUser ( user ) ; } async setGrant ( grant ) { if ( ! this . _proxy ) return null ; return await this . _proxy . setGrant ( grant ) ; } param events async sendEvents ( events ) { if ( ! this . _proxy ) return null ; return await this . _proxy . sendEvents ( events ) ; } param eventName param botIds param arg 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 ( ) ; } }