import { RuntimeStore } from 'ontime-types'; import { socket } from '../adapters/WebsocketAdapter.js'; import { isEmptyObject } from '../utils/parserUtils.js'; export type PublishFn = (key: T, value: RuntimeStore[T]) => void; export type StoreGetter = (key: T) => Partial[T]; let store: Partial = {}; /** * A runtime store that broadcasts its payload * - init: allows for adding an initial payload to the store * - poll: utility to return state * - broadcast: send its payload as json object */ export const eventStore = { init(payload: RuntimeStore) { store = payload; }, get(key: T) { return store[key]; }, set(key: T, value: RuntimeStore[T]) { store[key] = value; socket.sendAsJson({ type: 'ontime-patch', payload: { [key]: value } }); }, createBatch() { const patch: Partial = {}; return { add(key: T, value: RuntimeStore[T]) { patch[key] = value; }, send() { if (isEmptyObject(patch)) return; store = { ...store, ...patch }; socket.sendAsJson({ type: 'ontime-patch', payload: patch }); }, }; }, poll() { return store as RuntimeStore; }, broadcast() { socket.sendAsJson({ type: 'ontime', payload: store, }); }, };