UNPKG

@casual-simulation/aux-runtime

Version:
143 lines 4.51 kB
import { TAG_MASK_SPACE_PRIORITIES, hasValue, } from '@casual-simulation/aux-common/bots'; import { createRuntimeBot, RealtimeEditMode } from '../RuntimeBot'; import { createCompiledBot } from '../CompiledBot'; import { pickBy } from 'es-toolkit/compat'; import { applyTagEdit, isTagEdit } from '@casual-simulation/aux-common/bots'; export class TestScriptBotFactory { createRuntimeBot(bot) { return createDummyRuntimeBot(bot.id, bot.tags, bot.space); } destroyScriptBot() { return RealtimeEditMode.Immediate; } } /** * Creates a dummy script bot. * That is, a bot which uses the given values directly and does not marshall changes back to a runtime. * @param id The ID of the bot. * @param tags The tags the bot should have. * @param space The space of the bot. */ export function createDummyRuntimeBot(id, tags = {}, space, signatures) { let functions = pickBy(tags, (t) => typeof t === 'function'); const precalc = createCompiledBot(id, tags, undefined, space, functions, signatures); return createRuntimeBot(precalc, testScriptBotInterface); } export const testScriptBotInterface = { updateTag(bot, tag, newValue) { if (isTagEdit(newValue)) { bot.values[tag] = bot.tags[tag] = applyTagEdit(bot.tags[tag], newValue); } else { if (hasValue(newValue)) { bot.tags[tag] = newValue; bot.values[tag] = newValue; } else { delete bot.tags[tag]; delete bot.values[tag]; } } return { mode: RealtimeEditMode.Immediate, changedValue: newValue, }; }, getValue(bot, tag) { return bot.values[tag]; }, getRawValue(bot, tag) { return bot.tags[tag]; }, getListener(bot, tag) { var _a, _b; return (_b = (_a = bot.listenerOverrides[tag]) !== null && _a !== void 0 ? _a : bot.listeners[tag]) !== null && _b !== void 0 ? _b : null; }, setListener(bot, tag, value) { if (!hasValue(value)) { delete bot.listenerOverrides[tag]; } else { bot.listenerOverrides[tag] = value; } }, getSignature(bot, signature) { if (bot.signatures) { return bot.signatures[signature]; } else { return undefined; } }, notifyChange() { }, notifyActionEnqueued(action) { }, getTagMask(bot, tag) { if (!bot.masks) { return undefined; } for (let space of TAG_MASK_SPACE_PRIORITIES) { if (!bot.masks[space]) { continue; } if (tag in bot.masks[space]) { return bot.masks[space][tag]; } } return undefined; }, updateTagMask(bot, tag, spaces, value) { if (!bot.masks) { bot.masks = {}; } for (let space of spaces) { if (!bot.masks[space]) { bot.masks[space] = {}; } if (isTagEdit(value)) { bot.masks[space][tag] = applyTagEdit(bot.masks[space][tag], value); } else { bot.masks[space][tag] = value; } } return { mode: RealtimeEditMode.Immediate, changedValue: value, }; }, getTagLink(bot, tag) { return null; }, addDynamicListener(bot, tag, listener) { if (!bot.dynamicListeners) { bot.dynamicListeners = {}; } if (!bot.dynamicListeners[tag]) { bot.dynamicListeners[tag] = []; } bot.dynamicListeners[tag].push(listener); }, removeDynamicListener(bot, tag, listener) { if (bot.dynamicListeners && bot.dynamicListeners[tag]) { const listeners = bot.dynamicListeners[tag]; const index = listeners.indexOf(listener); if (index >= 0) { listeners.splice(index, 1); if (listeners.length <= 0) { delete bot.dynamicListeners[tag]; } } } }, getDynamicListeners(bot, tag) { if (bot.dynamicListeners && bot.dynamicListeners[tag]) { return bot.dynamicListeners[tag]; } return null; }, currentVersion: { localSites: {}, vector: {}, }, }; //# sourceMappingURL=TestScriptBotFactory.js.map