Skip to content

Commit

Permalink
fix: persistence of custom fields (#1078)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpvalente authored Jun 15, 2024
1 parent a15eb0d commit ad0da6d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -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();
});
});

Expand All @@ -945,8 +951,8 @@ describe('custom fields', () => {
type: 'string',
colour: 'blue',
},
av: {
label: 'AV',
video: {
label: 'video',
type: 'string',
colour: 'red',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('handleCustomField()', () => {
label: 'sound',
},
} as CustomFields;
const customFieldChangelog = {};
const customFieldChangelog = new Map<string, string>();

// @ts-expect-error -- partial event for testing
const event: OntimeEvent = {
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 = {
Expand Down
8 changes: 5 additions & 3 deletions apps/server/src/services/rundown-service/rundownCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ let links: Record<EventID, EventID> = {};
* lighting: lx
* }
*/
export const customFieldChangelog = {};
export const customFieldChangelog = new Map<string, string>();

/**
* Keep track of which custom fields are used.
Expand Down Expand Up @@ -141,6 +141,7 @@ export function generate(
}

isStale = false;
customFieldChangelog.clear();
totalDelay = accumulatedDelay;
if (lastEnd !== null && firstStart !== null) {
totalDuration = getTotalDuration(firstStart, lastEnd, daySpan);
Expand Down Expand Up @@ -411,6 +412,7 @@ function invalidateIfUsed(label: CustomFieldLabel) {
// schedule a non priority cache update
setImmediate(() => {
generate();
DataProvider.setRundown(persistedRundown);
});
}

Expand Down Expand Up @@ -459,7 +461,7 @@ export const editCustomField = async (key: string, newField: Partial<CustomField
}

const existingField = persistedCustomFields[key];
if (existingField.type !== newField.type) {
if (newField.type !== undefined && existingField.type !== newField.type) {
throw new Error('Change of field type is not allowed');
}

Expand All @@ -468,7 +470,7 @@ export const editCustomField = async (key: string, newField: Partial<CustomField

if (key !== newKey) {
delete persistedCustomFields[key];
customFieldChangelog[key] = newKey;
customFieldChangelog.set(key, newKey);
}

scheduleCustomFieldPersist(persistedCustomFields);
Expand Down
6 changes: 3 additions & 3 deletions apps/server/src/services/rundown-service/rundownCacheUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ export function addToCustomAssignment(
*/
export function handleCustomField(
customFields: CustomFields,
customFieldChangelog: Record<string, string>,
customFieldChangelog: Map<string, string>,
mutableEvent: OntimeEvent,
assignedCustomFields: Record<string, string[]>,
) {
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];
Expand Down

0 comments on commit ad0da6d

Please sign in to comment.