refactor: simplify handling of notification

This commit is contained in:
cv
2023-09-27 09:55:18 +02:00
parent 88a853c158
commit cb871a8c26
@@ -35,7 +35,6 @@ import { clock } from '../Clock.js';
*/ */
export function forceReset() { export function forceReset() {
eventLoader.reset(); eventLoader.reset();
sendRefetch();
runtimeCacheStore.invalidate(delayedRundownCacheKey); runtimeCacheStore.invalidate(delayedRundownCacheKey);
} }
@@ -192,15 +191,11 @@ export async function addEvent(eventData: Partial<OntimeEvent> | Partial<OntimeD
// modify rundown // modify rundown
await cachedAdd(insertIndex, newEvent as OntimeEvent | OntimeDelay | OntimeBlock); await cachedAdd(insertIndex, newEvent as OntimeEvent | OntimeDelay | OntimeBlock);
// notify timer service of changed events notifyChanges({ timer: [id], external: true });
updateTimer([id]);
// notify event loader that rundown size has changed // notify event loader that rundown size has changed
updateChangeNumEvents(); updateChangeNumEvents();
// advice socket subscribers of change
sendRefetch();
return newEvent; return newEvent;
} }
@@ -211,11 +206,7 @@ export async function editEvent(eventData: Partial<OntimeEvent> | Partial<Ontime
const newEvent = await cachedEdit(eventData.id, eventData); const newEvent = await cachedEdit(eventData.id, eventData);
// notify timer service of changed events notifyChanges({ timer: [newEvent.id], external: true });
updateTimer([newEvent.id]);
// advice socket subscribers of change
sendRefetch();
return newEvent; return newEvent;
} }
@@ -228,14 +219,9 @@ export async function editEvent(eventData: Partial<OntimeEvent> | Partial<Ontime
export async function deleteEvent(eventId) { export async function deleteEvent(eventId) {
await cachedDelete(eventId); await cachedDelete(eventId);
// notify timer service of changed events notifyChanges({ timer: [eventId], external: true });
updateTimer([eventId]);
// notify event loader that rundown size has changed // notify event loader that rundown size has changed
updateChangeNumEvents(); updateChangeNumEvents();
// advice socket subscribers of change
sendRefetch();
} }
/** /**
@@ -245,9 +231,7 @@ export async function deleteEvent(eventId) {
export async function deleteAllEvents() { export async function deleteAllEvents() {
await cachedClear(); await cachedClear();
// notify timer service of changed events notifyChanges({ timer: true, external: true, reset: true });
updateTimer();
forceReset();
} }
/** /**
@@ -260,22 +244,15 @@ export async function deleteAllEvents() {
export async function reorderEvent(eventId: string, from: number, to: number) { export async function reorderEvent(eventId: string, from: number, to: number) {
const reorderedItem = await cachedReorder(eventId, from, to); const reorderedItem = await cachedReorder(eventId, from, to);
// notify timer service of changed events notifyChanges({ timer: true, external: true });
updateTimer();
// advice socket subscribers of change
sendRefetch();
return reorderedItem; return reorderedItem;
} }
export async function applyDelay(eventId: string) { export async function applyDelay(eventId: string) {
await cachedApplyDelay(eventId); await cachedApplyDelay(eventId);
// notify timer service of changed events notifyChanges({ timer: true, external: true });
updateTimer();
// advice socket subscribers of change
sendRefetch();
} }
/** /**
@@ -287,11 +264,7 @@ export async function applyDelay(eventId: string) {
export async function swapEvents(from: string, to: string) { export async function swapEvents(from: string, to: string) {
await cachedSwap(from, to); await cachedSwap(from, to);
// notify timer service of changed events notifyChanges({ timer: true, external: true });
updateTimer();
// advice socket subscribers of change
sendRefetch();
} }
/** /**
@@ -301,3 +274,26 @@ export async function swapEvents(from: string, to: string) {
function updateChangeNumEvents() { function updateChangeNumEvents() {
eventLoader.updateNumEvents(); eventLoader.updateNumEvents();
} }
/**
* Notify services of changes in the rundown
*/
export function notifyChanges(options: { timer?: boolean | string[]; external?: boolean; reset?: boolean }) {
if (options.timer) {
// notify timer service of changed events
if (Array.isArray(options.timer)) {
updateTimer(options.timer);
}
updateTimer();
}
if (options.external) {
// advice socket subscribers of change
sendRefetch();
}
if (options.reset) {
// force rundown to be recalculated
forceReset();
}
}