From 55ba21dcbc4749c0e8884a0be10eae8cf31772f6 Mon Sep 17 00:00:00 2001 From: Mario Nebl Date: Wed, 30 May 2018 18:44:08 +0200 Subject: [PATCH] perf: avoid saving unchanged project data (#492) --- src/electron/renderer.ts | 7 +++++++ src/model/project.ts | 10 ++++++++++ src/store/view-store.ts | 10 ++++++++++ 3 files changed, 27 insertions(+) diff --git a/src/electron/renderer.ts b/src/electron/renderer.ts index 4191641df..4c1fcedc0 100644 --- a/src/electron/renderer.ts +++ b/src/electron/renderer.ts @@ -278,9 +278,16 @@ Mobx.autorunAsync(() => { Mobx.autorunAsync(() => { const project = store.getProject(); + const savedProjects = store.getSavedProjects(); + const savedProject = savedProjects[savedProjects.length - 1]; + + if (savedProject && Project.isEqual(savedProject, project.toDisk())) { + return; + } if (project) { const serializedProject = project.toJSON(); + store.addSavedProject(project); const payload = { path: project.getPath(), diff --git a/src/model/project.ts b/src/model/project.ts index 4cab8f6b0..13c019f29 100644 --- a/src/model/project.ts +++ b/src/model/project.ts @@ -1,4 +1,5 @@ import { Element, ElementContent } from './element'; +import { isEqual } from 'lodash'; import * as Mobx from 'mobx'; import { Page } from './page'; import { PatternLibrary } from './pattern-library'; @@ -120,6 +121,15 @@ export class Project { return project; } + public static isEqual(a: Types.SavedProject, b: Types.SavedProject): boolean; + public static isEqual( + a: Types.SavedProject | Project, + b: Types.SavedProject | Project + ): boolean { + const toData = input => (input instanceof Project ? input.toDisk() : input); + return isEqual(toData(a), toData(b)); + } + public addElement(element: Element): void { this.elements.push(element); } diff --git a/src/store/view-store.ts b/src/store/view-store.ts index d68bdf1e7..fd3fe0d87 100644 --- a/src/store/view-store.ts +++ b/src/store/view-store.ts @@ -84,6 +84,8 @@ export class ViewStore { */ @Mobx.observable private redoBuffer: Command.Command[] = []; + private savedProjects: Types.SavedProject[] = []; + /** * http port the preview server is listening on */ @@ -148,6 +150,10 @@ export class ViewStore { return page; } + public addSavedProject(project: Model.Project): void { + this.savedProjects.push(project.toDisk()); + } + /** * Clears the undo and redo buffers (e.g. if a page is loaded or the page state get * incompatible with the buffers). @@ -513,6 +519,10 @@ export class ViewStore { return this.project; } + public getSavedProjects(): Types.SavedProject[] { + return this.savedProjects; + } + public getSelectedElement(): Model.Element | undefined { if (!this.project) { return;