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

Commit

Permalink
fix: use atomic change messages for file saving
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl committed Sep 10, 2018
1 parent 9a7a9c3 commit 59af741
Show file tree
Hide file tree
Showing 13 changed files with 208 additions and 204 deletions.
19 changes: 10 additions & 9 deletions src/electron/create-edit-message-handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as Clipboard from './clipboard';
import * as Message from '../message';
import { requestApp } from './request-app';
import { requestProject } from './request-project';
import * as Types from '../types';
import * as uuid from 'uuid';

Expand All @@ -20,13 +19,12 @@ export async function createEditMessageHandler(
case Message.MessageType.Copy: {
const app = await requestApp(injection.sender);

if (app.getHasFocusedInput()) {
if (app.getHasFocusedInput() || !ctx.project) {
return;
}

const project = await requestProject(injection.sender);
const focusedItemType = project.getFocusedItemType();
const focusedItem = project.getFocusedItem();
const focusedItemType = ctx.project.getFocusedItemType();
const focusedItem = ctx.project.getFocusedItem();

if (!focusedItem) {
return;
Expand All @@ -35,15 +33,18 @@ export async function createEditMessageHandler(
Clipboard.setClipboard({
type: serializeItemType(focusedItemType),
item: focusedItem.toJSON(),
project: project.toJSON()
project: ctx.project.toJSON()
});

break;
}
case Message.MessageType.CutElement:
case Message.MessageType.CopyElement: {
const project = await requestProject(injection.sender);
const element = project.getElementById(message.payload);
if (!ctx.project) {
return;
}

const element = ctx.project.getElementById(message.payload);

if (!element) {
return;
Expand All @@ -52,7 +53,7 @@ export async function createEditMessageHandler(
Clipboard.setClipboard({
type: serializeItemType(Types.ItemType.Element),
item: element.toJSON(),
project: project.toJSON()
project: ctx.project.toJSON()
});

break;
Expand Down
42 changes: 26 additions & 16 deletions src/electron/create-export-message-handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as Export from '../export';
import * as Fs from 'fs';
import * as Message from '../message';
import { requestProject } from './request-project';
import { showError } from './show-error';
import { showSaveDialog } from './show-save-dialog';
import * as Types from '../types';
Expand All @@ -18,14 +17,16 @@ export async function createExportMessageHandler(
return async function exportMessageHandler(message: Message.Message): Promise<void> {
switch (message.type) {
case Message.MessageType.ExportHtmlProject: {
const project = await requestProject(injection.sender);
if (!ctx.project) {
return;
}

const path = await showSaveDialog({
defaultPath: `/${project.getName()}.html`,
title: `Export ${project.getName()} as HTML file`,
defaultPath: `/${ctx.project.getName()}.html`,
title: `Export ${ctx.project.getName()} as HTML file`,
filters: [
{
name: project.getName(),
name: ctx.project.getName(),
extensions: ['html', 'htm']
}
]
Expand All @@ -35,7 +36,10 @@ export async function createExportMessageHandler(
return;
}

const htmlExport = await Export.exportHtmlProject({ project, port: ctx.port });
const htmlExport = await Export.exportHtmlProject({
project: ctx.project,
port: ctx.port
});

if (htmlExport.type === Types.ExportResultType.ExportError) {
showError(htmlExport.error);
Expand All @@ -51,18 +55,21 @@ export async function createExportMessageHandler(
break;
}
case Message.MessageType.ExportPngPage: {
const project = await requestProject(injection.sender);
const activePage = project.getPages().find(p => p.getActive());
if (!ctx.project) {
return;
}

const activePage = ctx.project.getPages().find(p => p.getActive());

if (!activePage) {
return;
}

const index = project.getPages().indexOf(activePage);
const index = ctx.project.getPages().indexOf(activePage);

const path = await showSaveDialog({
defaultPath: `${project.getName()} - ${index}.png`,
title: `Export Page ${index} of ${project.getName()} as PNG`
defaultPath: `${ctx.project.getName()} - ${index}.png`,
title: `Export Page ${index} of ${ctx.project.getName()} as PNG`
});

if (!path) {
Expand All @@ -87,18 +94,21 @@ export async function createExportMessageHandler(
break;
}
case Message.MessageType.ExportSketchPage: {
const project = await requestProject(injection.sender);
const activePage = project.getPages().find(p => p.getActive());
if (!ctx.project) {
return;
}

const activePage = ctx.project.getPages().find(p => p.getActive());

if (!activePage) {
return;
}

const index = project.getPages().indexOf(activePage);
const index = ctx.project.getPages().indexOf(activePage);

const path = await showSaveDialog({
defaultPath: `${project.getName()} - Page ${index}.asketch.json`,
title: `Export Page ${index} of ${project.getName()} as .asketch.json`
defaultPath: `${ctx.project.getName()} - Page ${index}.asketch.json`,
title: `Export Page ${index} of ${ctx.project.getName()} as .asketch.json`
});

if (!path) {
Expand Down
10 changes: 5 additions & 5 deletions src/electron/create-file-message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ export async function createFileMessageHandler(
break;
}
case Message.MessageType.Save: {
const project = Model.Project.from(message.payload.project);

project.setPath(message.payload.path);
injection.ephemeralStore.setProjectPath(project.getPath());
if (!ctx.project) {
return;
}

await Persistence.persist(project.getPath(), project);
injection.ephemeralStore.setProjectPath(ctx.project.getPath());
await Persistence.persist(ctx.project.getPath(), ctx.project);
}
}
};
Expand Down
23 changes: 15 additions & 8 deletions src/electron/create-library-message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import * as Fs from 'fs';
import * as Message from '../message';
import * as Model from '../model';
import * as Path from 'path';
import { requestProject } from './request-project';
import { showOpenDialog } from './show-open-dialog';
import * as Types from '../types';
import * as uuid from 'uuid';
Expand All @@ -19,6 +18,7 @@ export async function createLibraryMessageHandler(
ctx: ServerMessageHandlerContext,
injection: ServerMessageHandlerInjection
): Promise<(message: Message.Message) => Promise<void>> {
// tslint:disable-next-line:cyclomatic-complexity
return async function libraryMessageHandler(message: Message.Message): Promise<void> {
switch (message.type) {
case Message.MessageType.CreateScriptBundleRequest: {
Expand Down Expand Up @@ -53,18 +53,22 @@ export async function createLibraryMessageHandler(
break;
}
case Message.MessageType.ConnectedPatternLibraryNotification: {
const project = await requestProject(injection.sender);
if (!ctx.project) {
return;
}

await injection.ephemeralStore.addConnection({
projectId: project.getId(),
projectId: ctx.project.getId(),
libraryId: message.payload.id,
libraryPath: message.payload.path
});

break;
}
case Message.MessageType.ConnectPatternLibraryRequest: {
const project = await requestProject(injection.sender);
if (!ctx.project) {
return;
}

const paths = await showOpenDialog({
title: 'Connnect Pattern Library',
Expand All @@ -82,8 +86,8 @@ export async function createLibraryMessageHandler(
);

const previousLibrary = message.payload.library
? project.getPatternLibraryById(message.payload.library)
: project
? ctx.project.getPatternLibraryById(message.payload.library)
: ctx.project
.getPatternLibraries()
.find(p => connections.some(c => c.libraryId === p.getId()));

Expand Down Expand Up @@ -127,8 +131,11 @@ export async function createLibraryMessageHandler(
break;
}
case Message.MessageType.UpdatePatternLibraryRequest: {
const project = await requestProject(injection.sender);
const library = project.getPatternLibraryById(message.payload.id);
if (!ctx.project) {
return;
}

const library = ctx.project.getPatternLibraryById(message.payload.id);

if (!library || !library.getCapabilites().includes(Types.LibraryCapability.Update)) {
return;
Expand Down
8 changes: 1 addition & 7 deletions src/electron/create-main-menu/create-file-menu.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as Electron from 'electron';
import { MessageType } from '../../message';
import * as Model from '../../model';
import { requestProject } from '../request-project';
import { Sender } from '../../sender/server';
import * as Types from '../../types';
import * as uuid from 'uuid';
Expand Down Expand Up @@ -66,15 +65,10 @@ export function createFileMenu(
accelerator: 'CmdOrCtrl+S',
role: 'save',
click: async () => {
const freshProject = await requestProject(injection.sender);

injection.sender.send({
type: MessageType.Save,
id: uuid.v4(),
payload: {
path: freshProject.getPath(),
project: freshProject.toJSON()
}
payload: undefined
});
}
},
Expand Down
38 changes: 0 additions & 38 deletions src/electron/request-project.ts

This file was deleted.

10 changes: 10 additions & 0 deletions src/electron/start-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as Ephemeral from './ephemeral-store';
import * as Events from 'events';
import * as getPort from 'get-port';
import * as Message from '../message';
import * as Mobx from 'mobx';
import * as Model from '../model';
import { Sender } from '../sender/server';
import { createServer } from '../server';
Expand Down Expand Up @@ -33,6 +34,14 @@ export async function startApp(ctx: AppContext): Promise<{ emitter: Events.Event
const server = createServer({ port: ctx.port, sender, context: ctx });
const ephemeralStore = new Ephemeral.EphemeralStore();

const dispose = Mobx.autorun(() => {
if (!ctx.project) {
return;
}

ctx.project.sync(sender);
});

const serverMessageHandler = await createServerMessageHandler(ctx, {
emitter,
ephemeralStore,
Expand Down Expand Up @@ -84,6 +93,7 @@ export async function startApp(ctx: AppContext): Promise<{ emitter: Events.Event
ephemeralStore.clear();
}

dispose();
await server.stop();
sender.stop();
server.removeAllListeners();
Expand Down
2 changes: 1 addition & 1 deletion src/message/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export type ProjectResponse = Envelope<
>;
export type Redo = EmptyEnvelope<MessageType.Redo>;
export type Reload = Envelope<MessageType.Reload, { forced: boolean } | undefined>;
export type Save = Envelope<MessageType.Save, Types.SavePayload>;
export type Save = Envelope<MessageType.Save, undefined>;
export type SetPane = Envelope<MessageType.SetPane, { pane: Types.AppPane; visible: boolean }>;
export type ShowError = Envelope<MessageType.ShowError, { message: string; stack: string }>;
export type SketchExportRequest = Envelope<
Expand Down
Loading

0 comments on commit 59af741

Please sign in to comment.