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

Commit

Permalink
fix: try harder to get the very latest data when saving explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl committed Sep 10, 2018
1 parent ee50c31 commit e9c994e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 39 deletions.
12 changes: 6 additions & 6 deletions src/electron/create-main-menu/create-file-menu.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 @@ -64,16 +65,15 @@ export function createFileMenu(
enabled: typeof ctx.project !== 'undefined',
accelerator: 'CmdOrCtrl+S',
role: 'save',
click: () => {
if (!project) {
return;
}
click: async () => {
const freshProject = await requestProject(injection.sender);

injection.sender.send({
type: MessageType.Save,
id: uuid.v4(),
payload: {
path: project.getPath(),
project: project.toJSON()
path: freshProject.getPath(),
project: freshProject.toJSON()
}
});
}
Expand Down
34 changes: 1 addition & 33 deletions src/electron/create-server-message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as Model from '../model';
import * as Path from 'path';
import { Persistence } from '../persistence';
import { readProjectOrError } from './read-project-or-error';
import { requestProject, requestProjectSafely } from './request-project';
import { Sender } from '../sender/server';
import { showContextMenu } from './show-context-menu';
import { showError } from './show-error';
Expand Down Expand Up @@ -524,39 +525,6 @@ async function performAnalysis(
});
}

async function requestProject(sender: Sender): Promise<Model.Project> {
const projectResponse = await sender.request<Message.ProjectRequestResponsePair>(
{
id: uuid.v4(),
type: Message.MessageType.ProjectRequest,
payload: undefined
},
Message.MessageType.ProjectResponse
);

if (projectResponse.payload.status === Types.ProjectStatus.None) {
throw new Error('There must be an opened project before oppening a library');
}

if (
projectResponse.payload.status === Types.ProjectStatus.Error ||
projectResponse.payload.status !== Types.ProjectStatus.Ok ||
typeof projectResponse.payload.data === 'undefined'
) {
throw new Error('Error while importing library');
}

return Model.Project.from(projectResponse.payload.data);
}

async function requestProjectSafely(sender: Sender): Promise<Model.Project | undefined> {
try {
return await requestProject(sender);
} catch (err) {
return;
}
}

function showAnalysisError(error: Error): void {
console.error(error);

Expand Down
38 changes: 38 additions & 0 deletions src/electron/request-project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as Message from '../message';
import * as Model from '../model';
import { Sender } from '../sender/server';
import * as Types from '../types';
import * as uuid from 'uuid';

export async function requestProject(sender: Sender): Promise<Model.Project> {
const projectResponse = await sender.request<Message.ProjectRequestResponsePair>(
{
id: uuid.v4(),
type: Message.MessageType.ProjectRequest,
payload: undefined
},
Message.MessageType.ProjectResponse
);

if (projectResponse.payload.status === Types.ProjectStatus.None) {
throw new Error('There must be an opened project before oppening a library');
}

if (
projectResponse.payload.status === Types.ProjectStatus.Error ||
projectResponse.payload.status !== Types.ProjectStatus.Ok ||
typeof projectResponse.payload.data === 'undefined'
) {
throw new Error('Error while importing library');
}

return Model.Project.from(projectResponse.payload.data);
}

export async function requestProjectSafely(sender: Sender): Promise<Model.Project | undefined> {
try {
return await requestProject(sender);
} catch (err) {
return;
}
}

0 comments on commit e9c994e

Please sign in to comment.