UNPKG

@casual-simulation/aux-common

Version:
84 lines 2.2 kB
import { apply } from '../bots/AuxStateHelpers'; import { hasValue } from './BotCalculations'; /** * Applies the given update to the given state and returns a new object representing the final state. * @param currentState The current state. * @param update The update to apply. */ export function applyUpdates(currentState, update) { if (currentState) { return apply(currentState, update.state); } else { return update.state; } } /** * Calculates the StateUpdatedEvent from the given partial bots state. * @param state The state update. * @param version The version of the state. */ export function stateUpdatedEvent(state, version = null) { let update = { addedBots: [], removedBots: [], updatedBots: [], state: state, version, }; for (let id in state) { const bot = state[id]; if (bot === null) { update.removedBots.push(id); } else if (!hasValue(bot)) { // Do nothing for this bot. // Incorrectly formatted state. } else if (bot.id === id) { update.addedBots.push(bot.id); } else { update.updatedBots.push(id); } } return update; } export function updatedBot(partialBot, currentBot) { let tags = new Set(); let signatures = []; if (partialBot.tags) { for (let tag in partialBot.tags) { tags.add(tag); } } if (partialBot.signatures) { for (let sig in partialBot.signatures) { if (!signatures) { signatures = []; } signatures.push(sig); } } if (partialBot.masks) { for (let space in partialBot.masks) { for (let tag in partialBot.masks[space]) { tags.add(tag); } } } if (signatures.length > 0) { return { bot: currentBot, tags: [...tags.values()], signatures, }; } else { return { bot: currentBot, tags: [...tags.values()], }; } } //# sourceMappingURL=StateUpdatedEvent.js.map