import { BaseModel } from '../models'; import { isWorkspace, Workspace } from '../models/workspace'; // Key for VCS to delete before computing changes const DELETE_KEY: keyof BaseModel = 'modified'; type ResetModelKeys = { [K in keyof T]?: T[K] | null; } // Keys for VCS to reset before computing changes // We can't always delete keys // If we delete a key that previously existed (even as null), we then have a different hash because of the deletion // Therefore, we have to set it back to a default value const RESET_WORKSPACE_KEYS: ResetModelKeys = { parentId: null, }; export const shouldIgnoreKey = (key: keyof T, doc: T) => { if (key === DELETE_KEY) { return true; } if (isWorkspace(doc)) { return key in RESET_WORKSPACE_KEYS; } return false; }; export const deleteKeys = (doc: T) => { // @ts-expect-error force delete the key even if it is required delete doc[DELETE_KEY]; }; export const resetKeys = (doc: T) => { if (isWorkspace(doc)) { Object.keys(RESET_WORKSPACE_KEYS) .forEach(key => { doc[key] = RESET_WORKSPACE_KEYS[key]; }); } };