From b06ab151904250461f08a657e4cf877cf96e05ef Mon Sep 17 00:00:00 2001 From: Carlos Valente <34649812+cpvalente@users.noreply.github.com> Date: Thu, 21 Mar 2024 22:10:54 +0100 Subject: [PATCH] refactor: save object on restore (#842) --- apps/server/src/services/RestoreService.ts | 24 ++++++++++++---------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/apps/server/src/services/RestoreService.ts b/apps/server/src/services/RestoreService.ts index cf08dfcb2..46bfad9cd 100644 --- a/apps/server/src/services/RestoreService.ts +++ b/apps/server/src/services/RestoreService.ts @@ -2,6 +2,7 @@ import { MaybeNumber, MaybeString, Playback } from 'ontime-types'; import { JSONFile } from 'lowdb/node'; import { resolveRestoreFile } from '../setup/index.js'; +import { deepEqual } from 'fast-equals'; export type RestorePoint = { playback: Playback; @@ -62,13 +63,13 @@ export function isRestorePoint(obj: unknown): obj is RestorePoint { export class RestoreService { private readonly filePath: MaybeString; private readonly file: JSONFile; - private lastStore: MaybeString; private failedCreateAttempts: number; + private savedState: RestorePoint; constructor(filePath: string) { this.filePath = filePath; - this.lastStore = null; + this.savedState = null; this.file = new JSONFile(this.filePath); this.failedCreateAttempts = 0; } @@ -100,15 +101,16 @@ export class RestoreService { return; } - const stringifiedStore = JSON.stringify(newState); - if (stringifiedStore !== this.lastStore) { - try { - await this.write(newState); - this.lastStore = stringifiedStore; - this.failedCreateAttempts = 0; - } catch (_error) { - this.failedCreateAttempts += 1; - } + if (deepEqual(newState, this.savedState)) { + return; + } + + try { + await this.write(newState); + this.savedState = { ...newState }; + this.failedCreateAttempts = 0; + } catch (_error) { + this.failedCreateAttempts += 1; } }