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

Commit

Permalink
perf: save when changes are commited
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl committed Jun 1, 2018
1 parent 5d3433f commit 512fb16
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 36 deletions.
10 changes: 1 addition & 9 deletions src/electron/create-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,7 @@ export function createMenu(ctx: MenuContext): void {
if (!project) {
return;
}

Sender.send({
type: ServerMessageType.Save,
id: uuid.v4(),
payload: {
path: project.getPath(),
project: project.toJSON()
}
});
ctx.store.save();
}
},
{
Expand Down
26 changes: 0 additions & 26 deletions src/electron/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -335,32 +335,6 @@ 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(),
project: serializedProject
};

Sender.send({
id: uuid.v4(),
payload,
type: ServerMessageType.Save
});
}
});

ReactDom.render(
<MobxReact.Provider app={app} store={store}>
<App />
Expand Down
33 changes: 32 additions & 1 deletion src/store/view-store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Sender from '../message/client';
import { isEqual } from 'lodash';
import { debounce, isEqual } from 'lodash';
import { ServerMessageType } from '../message';
import * as Mobx from 'mobx';
import * as Model from '../model';
Expand Down Expand Up @@ -54,6 +54,8 @@ export class ViewStore {
public constructor(init: { app: Model.AlvaApp; history: Model.EditHistory }) {
this.app = init.app;
this.editHistory = init.history;

this.save = debounce(this.save, 1000);
}

@Mobx.action
Expand Down Expand Up @@ -82,6 +84,9 @@ export class ViewStore {
app: this.app.toJSON(),
project: this.project.toJSON()
});

// tslint:disable-next-line:no-any
(window as any).requestIdleCallback(() => this.save());
}

public connectPatternLibrary(): void {
Expand Down Expand Up @@ -817,6 +822,32 @@ export class ViewStore {
this.project.removePage(page);
}

@Mobx.action
public save(): void {
const savedProjects = this.getSavedProjects();
const savedProject = savedProjects[savedProjects.length - 1];

if (savedProject && Model.Project.isEqual(savedProject, this.project.toDisk())) {
return;
}

if (this.project) {
const serializedProject = this.project.toJSON();
this.addSavedProject(this.project);

const payload = {
path: this.project.getPath(),
project: serializedProject
};

Sender.send({
id: uuid.v4(),
payload,
type: ServerMessageType.Save
});
}
}

@Mobx.action
public setActiveAppView(appView: Types.AlvaView): void {
this.app.setActiveView(appView);
Expand Down

0 comments on commit 512fb16

Please sign in to comment.