mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-08 00:43:54 +00:00
1849b4d39f
* chore: migrate eslint to oxlint * chore: migrate prettier to oxfmt * chore: migrate typescript * chore: toThrow should have a expected value * chore: cast test value as Day * chore: small title fix * chore: mocks should be hoisted * chore: incorrect async useage * chore: test should be inside description * chore: test sohuld include an expeced * chore: oxfmt --------- Co-authored-by: alex-Arc <omnivox@LAPTOP-RC5SNBVV.localdomain>
51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
import { MessageTag, RuntimeStore } from 'ontime-types';
|
|
|
|
import { socket } from '../adapters/WebsocketAdapter.js';
|
|
import { isEmptyObject } from '../utils/parserUtils.js';
|
|
|
|
export type PublishFn = <T extends keyof RuntimeStore>(key: T, value: RuntimeStore[T]) => void;
|
|
export type StoreGetter = <T extends keyof RuntimeStore>(key: T) => Partial<RuntimeStore>[T];
|
|
|
|
let store: Partial<RuntimeStore> = {};
|
|
|
|
/**
|
|
* 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<T extends keyof RuntimeStore>(key: T) {
|
|
return store[key];
|
|
},
|
|
set<T extends keyof RuntimeStore>(key: T, value: RuntimeStore[T]) {
|
|
store[key] = value;
|
|
socket.sendAsJson(MessageTag.RuntimeData, { [key]: value });
|
|
},
|
|
createBatch() {
|
|
const patch: Partial<RuntimeStore> = {};
|
|
return {
|
|
add<T extends keyof RuntimeStore>(key: T, value: RuntimeStore[T]) {
|
|
patch[key] = value;
|
|
},
|
|
send() {
|
|
if (isEmptyObject(patch)) return;
|
|
store = { ...store, ...patch };
|
|
socket.sendAsJson(MessageTag.RuntimeData, patch);
|
|
},
|
|
};
|
|
},
|
|
poll() {
|
|
return store as RuntimeStore;
|
|
},
|
|
broadcast() {
|
|
socket.sendAsJson(
|
|
MessageTag.RuntimeData,
|
|
store as RuntimeStore, // We assume that it has been initialized at this point
|
|
);
|
|
},
|
|
};
|