refactor: make message service stateless

This commit is contained in:
Carlos Valente
2024-10-13 22:30:37 +02:00
committed by Carlos Valente
parent a4aa513d1f
commit 77e87e1d0c
3 changed files with 14 additions and 46 deletions
-3
View File
@@ -202,9 +202,6 @@ export const startServer = async (
// TODO: pass event store to rundownservice
runtimeService.init(maybeRestorePoint);
// eventStore set is a dependency of the services that publish to it
messageService.init((key, value) => eventStore.set(key, value));
expressServer.listen(serverPort, '0.0.0.0', () => {
const nif = getNetworkInterfaces();
consoleSuccess(`Local: http://localhost:${serverPort}/editor`);
@@ -1,63 +1,40 @@
import { TimerMessage, MessageState } from 'ontime-types';
import { MessageState, runtimeStorePlaceholder } from 'ontime-types';
import { DeepPartial } from 'ts-essentials';
import { throttle } from '../../utils/throttle.js';
import type { PublishFn } from '../../stores/EventStore.js';
const defaultTimer: TimerMessage = {
text: '',
visible: false,
blink: false,
blackout: false,
secondarySource: null,
};
let timer = { ...defaultTimer };
let external = '';
let throttledSet: PublishFn | null = null;
import { eventStore, type PublishFn } from '../../stores/EventStore.js';
/**
* Initialises the message service with a publish function
* @param publishFn
* Create a throttled version of the set function
*/
export function init(publishFn: PublishFn) {
throttledSet = throttle(publishFn, 100);
}
const throttledSet: PublishFn = throttle(eventStore.set, 100);
/**
* Exposes function to reset the internal state
*/
export function clear() {
timer = { ...defaultTimer };
external = '';
throttledSet('message', {
...runtimeStorePlaceholder.message,
});
}
/**
* Exposes the internal state of the message service
*/
export function getState(): MessageState {
return {
external,
timer,
};
return eventStore.get('message');
}
/**
* Utility function allows patching internal object
*/
export function patch(patch: DeepPartial<MessageState>): MessageState {
// we cannot call patch before init
// eslint-disable-next-line no-unused-labels -- dev code path
DEV: {
if (throttledSet === null) {
throw new Error('MessageService.patch() called before init()');
}
}
// make a copy of the state in store
const newState = { ...getState() };
if ('timer' in patch) timer = { ...timer, ...patch.timer };
if ('external' in patch && patch.external !== undefined) external = patch.external;
const newState = getState();
throttledSet?.('message', newState);
if ('timer' in patch) newState.timer = { ...newState.timer, ...patch.timer };
if ('external' in patch && patch.external !== undefined) newState.external = patch.external;
throttledSet('message', newState);
return newState;
}
@@ -1,12 +1,6 @@
import * as messageService from '../MessageService.js';
describe('MessageService', () => {
const publishFunction = () => {};
beforeAll(() => {
messageService.init(publishFunction);
});
beforeEach(() => {
messageService.clear();
});