From ad0da6def4c75c8018f1e6e90119bff0c54c7a52 Mon Sep 17 00:00:00 2001 From: Carlos Valente <34649812+cpvalente@users.noreply.github.com> Date: Sat, 15 Jun 2024 15:07:20 +0200 Subject: [PATCH] fix: persistence of custom fields (#1078) --- .../rundown-service/__tests__/rundownCache.test.ts | 14 ++++++++++---- .../__tests__/rundownCacheUtils.test.ts | 10 +++------- .../src/services/rundown-service/rundownCache.ts | 8 +++++--- .../services/rundown-service/rundownCacheUtils.ts | 6 +++--- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/apps/server/src/services/rundown-service/__tests__/rundownCache.test.ts b/apps/server/src/services/rundown-service/__tests__/rundownCache.test.ts index 1219a19fc..a8205b314 100644 --- a/apps/server/src/services/rundown-service/__tests__/rundownCache.test.ts +++ b/apps/server/src/services/rundown-service/__tests__/rundownCache.test.ts @@ -885,7 +885,7 @@ describe('custom fields', () => { }; const customField = await editCustomField('sound', { label: 'Sound', type: 'string', colour: 'green' }); - expect(customFieldChangelog).toStrictEqual({}); + expect(customFieldChangelog).toStrictEqual(new Map()); expect(customField).toStrictEqual(expected); }); @@ -931,9 +931,15 @@ describe('custom fields', () => { }, }; + // We need to flush all scheduled tasks for the generate function to settle + vi.useFakeTimers(); const customField = await editCustomField('video', { label: 'AV', type: 'string', colour: 'red' }); expect(customField).toStrictEqual(expectedAfter); - expect(customFieldChangelog).toStrictEqual({ video: 'av' }); + expect(customFieldChangelog).toStrictEqual(new Map([['video', 'av']])); + await editCustomField('av', { label: 'video' }); + vi.runAllTimers(); + expect(customFieldChangelog).toStrictEqual(new Map()); + vi.useRealTimers(); }); }); @@ -945,8 +951,8 @@ describe('custom fields', () => { type: 'string', colour: 'blue', }, - av: { - label: 'AV', + video: { + label: 'video', type: 'string', colour: 'red', }, diff --git a/apps/server/src/services/rundown-service/__tests__/rundownCacheUtils.test.ts b/apps/server/src/services/rundown-service/__tests__/rundownCacheUtils.test.ts index 80035c712..b927c6c4b 100644 --- a/apps/server/src/services/rundown-service/__tests__/rundownCacheUtils.test.ts +++ b/apps/server/src/services/rundown-service/__tests__/rundownCacheUtils.test.ts @@ -96,7 +96,7 @@ describe('handleCustomField()', () => { label: 'sound', }, } as CustomFields; - const customFieldChangelog = {}; + const customFieldChangelog = new Map(); // @ts-expect-error -- partial event for testing const event: OntimeEvent = { @@ -132,9 +132,7 @@ describe('handleCustomField()', () => { }, } as CustomFields; - const customFieldChangelog = { - sound: 'video', - }; + const customFieldChangelog = new Map([['sound', 'video']]); // @ts-expect-error -- partial event for testing const event: OntimeEvent = { @@ -170,9 +168,7 @@ describe('handleCustomField()', () => { }, } as CustomFields; - const customFieldChangelog = { - field1: 'newField1', - }; + const customFieldChangelog = new Map([['field1', 'newField1']]); // @ts-expect-error -- partial event for testing const mutableEvent: OntimeEvent = { diff --git a/apps/server/src/services/rundown-service/rundownCache.ts b/apps/server/src/services/rundown-service/rundownCache.ts index 4218139b7..03e4cd4c8 100644 --- a/apps/server/src/services/rundown-service/rundownCache.ts +++ b/apps/server/src/services/rundown-service/rundownCache.ts @@ -47,7 +47,7 @@ let links: Record = {}; * lighting: lx * } */ -export const customFieldChangelog = {}; +export const customFieldChangelog = new Map(); /** * Keep track of which custom fields are used. @@ -141,6 +141,7 @@ export function generate( } isStale = false; + customFieldChangelog.clear(); totalDelay = accumulatedDelay; if (lastEnd !== null && firstStart !== null) { totalDuration = getTotalDuration(firstStart, lastEnd, daySpan); @@ -411,6 +412,7 @@ function invalidateIfUsed(label: CustomFieldLabel) { // schedule a non priority cache update setImmediate(() => { generate(); + DataProvider.setRundown(persistedRundown); }); } @@ -459,7 +461,7 @@ export const editCustomField = async (key: string, newField: Partial, + customFieldChangelog: Map, mutableEvent: OntimeEvent, assignedCustomFields: Record, ) { for (const field in mutableEvent.custom) { // rename the property if it is in the changelog - if (field in customFieldChangelog) { + if (customFieldChangelog.has(field)) { const oldData = mutableEvent.custom[field]; - const newLabel = customFieldChangelog[field]; + const newLabel = customFieldChangelog.get(field); mutableEvent.custom[newLabel] = oldData; delete mutableEvent.custom[field];