Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
perf: avoid saving unchanged project data (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl authored and tilmx committed May 30, 2018
1 parent e451e43 commit 55ba21d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/electron/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
10 changes: 10 additions & 0 deletions src/model/project.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
}
Expand Down
10 changes: 10 additions & 0 deletions src/store/view-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 55ba21d

Please sign in to comment.