mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-21 23:19:09 +00:00
refactor: make message service stateless
This commit is contained in:
committed by
Carlos Valente
parent
a4aa513d1f
commit
77e87e1d0c
@@ -202,9 +202,6 @@ export const startServer = async (
|
|||||||
// TODO: pass event store to rundownservice
|
// TODO: pass event store to rundownservice
|
||||||
runtimeService.init(maybeRestorePoint);
|
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', () => {
|
expressServer.listen(serverPort, '0.0.0.0', () => {
|
||||||
const nif = getNetworkInterfaces();
|
const nif = getNetworkInterfaces();
|
||||||
consoleSuccess(`Local: http://localhost:${serverPort}/editor`);
|
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 { DeepPartial } from 'ts-essentials';
|
||||||
|
|
||||||
import { throttle } from '../../utils/throttle.js';
|
import { throttle } from '../../utils/throttle.js';
|
||||||
import type { PublishFn } from '../../stores/EventStore.js';
|
import { eventStore, 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;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialises the message service with a publish function
|
* Create a throttled version of the set function
|
||||||
* @param publishFn
|
|
||||||
*/
|
*/
|
||||||
export function init(publishFn: PublishFn) {
|
const throttledSet: PublishFn = throttle(eventStore.set, 100);
|
||||||
throttledSet = throttle(publishFn, 100);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exposes function to reset the internal state
|
* Exposes function to reset the internal state
|
||||||
*/
|
*/
|
||||||
export function clear() {
|
export function clear() {
|
||||||
timer = { ...defaultTimer };
|
throttledSet('message', {
|
||||||
external = '';
|
...runtimeStorePlaceholder.message,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exposes the internal state of the message service
|
* Exposes the internal state of the message service
|
||||||
*/
|
*/
|
||||||
export function getState(): MessageState {
|
export function getState(): MessageState {
|
||||||
return {
|
return eventStore.get('message');
|
||||||
external,
|
|
||||||
timer,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility function allows patching internal object
|
* Utility function allows patching internal object
|
||||||
*/
|
*/
|
||||||
export function patch(patch: DeepPartial<MessageState>): MessageState {
|
export function patch(patch: DeepPartial<MessageState>): MessageState {
|
||||||
// we cannot call patch before init
|
// make a copy of the state in store
|
||||||
// eslint-disable-next-line no-unused-labels -- dev code path
|
const newState = { ...getState() };
|
||||||
DEV: {
|
|
||||||
if (throttledSet === null) {
|
|
||||||
throw new Error('MessageService.patch() called before init()');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ('timer' in patch) timer = { ...timer, ...patch.timer };
|
if ('timer' in patch) newState.timer = { ...newState.timer, ...patch.timer };
|
||||||
if ('external' in patch && patch.external !== undefined) external = patch.external;
|
if ('external' in patch && patch.external !== undefined) newState.external = patch.external;
|
||||||
const newState = getState();
|
|
||||||
throttledSet?.('message', newState);
|
throttledSet('message', newState);
|
||||||
return newState;
|
return newState;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,6 @@
|
|||||||
import * as messageService from '../MessageService.js';
|
import * as messageService from '../MessageService.js';
|
||||||
|
|
||||||
describe('MessageService', () => {
|
describe('MessageService', () => {
|
||||||
const publishFunction = () => {};
|
|
||||||
|
|
||||||
beforeAll(() => {
|
|
||||||
messageService.init(publishFunction);
|
|
||||||
});
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
messageService.clear();
|
messageService.clear();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user