@casual-simulation/aux-vm-browser
Version:
A set of utilities required to securely run an AUX in a web browser.
68 lines • 2.74 kB
JavaScript
import { registerBuiltinPortal } from '@casual-simulation/aux-common';
import { NEVER, of } from 'rxjs';
import { switchMap, first, map, distinctUntilChanged, filter, mergeMap, } from 'rxjs/operators';
/**
* Gets an observable that resolves whenever the user bot for the given simulation changes.
* @param simulation The simulation.
*/
export function userBotChanged(simulation) {
return userBotChangedCore(simulation.helper.userId, simulation.watcher).pipe(map((u) => u.bot));
}
/**
* Gets an observable that resolves whenever the user bot for the given simulation changes.
* @param simulation The simulation.
*/
export function userBotTagsChanged(simulation) {
return userBotChangedCore(simulation.helper.userId, simulation.watcher);
}
export function userBotChangedCore(botId, watcher) {
if (botId) {
return watcher.botTagsChanged(botId);
}
else {
return NEVER;
}
}
/**
* Gets the user bot for the given simulation asynchronously.
* @param simulation The simulation.
*/
export function getUserBotAsync(simulation) {
return userBotChanged(simulation).pipe(first((bot) => !!bot));
}
/**
* Gets the config bot for the given portal.
* @param simulation The simulation.
* @param portal The portal.
*/
export function getPortalConfigBot(simulation, portal) {
var _a;
const data = simulation.portals.portalBots.get(portal);
if (data && data.botId && simulation.helper.botsState) {
return (_a = simulation.helper.botsState[data.botId]) !== null && _a !== void 0 ? _a : null;
}
return null;
}
/**
* Watches the config bot for the given portal for changes.
* @param simulation The simulation.
* @param portal The portal.
* @param shouldRegisterBuiltinPortal Whether an event should be sent to register the given portal as a builtin portal.
*/
export function watchPortalConfigBot(simulation, portal, shouldRegisterBuiltinPortal = true) {
return watchPortalConfigBotCore(simulation.watcher, simulation.portals, simulation.helper, portal, shouldRegisterBuiltinPortal);
}
/**
* Watches the config bot for the given portal for changes.
* @param watcher The bot watcher.
* @param portals The portal manager.
* @param helper The bot helper.
* @param portal The portal.
*/
export function watchPortalConfigBotCore(watcher, portals, helper, portal, shouldRegisterBuiltinPortal = true) {
if (shouldRegisterBuiltinPortal) {
helper.transaction(registerBuiltinPortal(portal));
}
return portals.portalBotIdUpdated.pipe(mergeMap((p) => p), filter((p) => p.portalId === portal), map((p) => p.botId), distinctUntilChanged(), switchMap((id) => (id ? watcher.botChanged(id) : of(null))));
}
//# sourceMappingURL=BrowserSimulationCalculations.js.map