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

Commit

Permalink
feat: add force reload
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl committed Sep 10, 2018
1 parent edfb369 commit aa48020
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 26 deletions.
13 changes: 13 additions & 0 deletions src/electron/create-main-menu/create-view-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ export function createViewMenu(
});
}
},
{
label: '&Force Reload',
accelerator: 'CmdOrCtrl+Shift+R',
click: (item: Electron.MenuItem, focusedWindow: Electron.BrowserWindow) => {
injection.sender.send({
id: uuid.v4(),
type: Message.ServerMessageType.Reload,
payload: {
forced: true
}
});
}
},
{
label: 'Toggle &Full Screen',
accelerator: process.platform === 'darwin' ? 'Ctrl+Command+F' : 'F11',
Expand Down
13 changes: 1 addition & 12 deletions src/electron/create-server-message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,7 @@ export async function createServerMessageHandler(
break;
}
case Message.ServerMessageType.Reload: {
/* const lastMainMenuRequest = injection.sender.last(
Message.ServerMessageType.MainMenuRequest
);
if (lastMainMenuRequest) {
showMainMenu(lastMainMenuRequest.payload, { sender: injection.sender });
} */

const focusedWindow = Electron.BrowserWindow.getFocusedWindow();
focusedWindow.reload();

injection.emitter.emit('reload');
injection.emitter.emit('reload', message.payload || {});
break;
}
case Message.ServerMessageType.CreateNewFileRequest: {
Expand Down
4 changes: 4 additions & 0 deletions src/electron/ephemeral-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export class EphemeralStore {
this.store.set('connections', [...connections]);
}

public async clear(): Promise<void> {
this.store.clear();
}

public async getConnections(): Promise<EphemeralConnection[]> {
const raw = this.store.get('connections');

Expand Down
10 changes: 1 addition & 9 deletions src/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,14 @@ const CONTEXT: AppContext = {
win: undefined
};

let restarting: Promise<void> = Promise.resolve();

async function main(): Promise<void> {
const StartApp = importFresh('./start-app');
const startApp = StartApp.startApp as typeof start;
const app = await startApp(CONTEXT);

const restart = async () => {
app.emitter.once('reload', async () => {
clearModule.match(new RegExp(`^${Path.join(__dirname, '..')}`));
await main();
restarting = Promise.resolve();
};

app.emitter.on('reload', async () => {
await restarting;
restarting = restart();
});
}

Expand Down
20 changes: 16 additions & 4 deletions src/electron/start-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface AppContext {

export async function startApp(ctx: AppContext): Promise<{ emitter: Events.EventEmitter }> {
log.info('App starting...');
ctx.win = ctx.win ? ctx.win : (await createWindow()).window;

const emitter = new Events.EventEmitter();

// Cast getPort return type from PromiseLike<number> to Promise<number>
Expand Down Expand Up @@ -69,12 +69,23 @@ export async function startApp(ctx: AppContext): Promise<{ emitter: Events.Event
});

await server.start();
log.info(`App started on port ${ctx.port}.`);
log.info(`Server started on port ${ctx.port}.`);

if (ctx.win) {
ctx.win.reload();
} else {
ctx.win = (await createWindow()).window;
}

startUpdater();

emitter.on('reload', async () => {
log.info('App reloading ...');
emitter.once('reload', async payload => {
log.info(`App reloading ${payload.forced ? 'forcefully' : ''}...`);

if (payload && payload !== null && payload.forced) {
ephemeralStore.clear();
}

await server.stop();
sender.stop();
server.removeAllListeners();
Expand All @@ -83,5 +94,6 @@ export async function startApp(ctx: AppContext): Promise<{ emitter: Events.Event
stopUpdater();
});

log.info('App started.');
return { emitter };
}
2 changes: 1 addition & 1 deletion src/message/server-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export type ProjectResponse = Envelope<
{ data: Types.SerializedProject | undefined; status: Types.ProjectStatus }
>;
export type Redo = EmptyEnvelope<ServerMessageType.Redo>;
export type Reload = EmptyEnvelope<ServerMessageType.Reload>;
export type Reload = Envelope<ServerMessageType.Reload, { forced: boolean } | undefined>;
export type Save = Envelope<ServerMessageType.Save, Types.SavePayload>;
export type SelectElement = Envelope<ServerMessageType.SelectElement, Types.PatternIdPayload>;
export type SetPane = Envelope<
Expand Down

0 comments on commit aa48020

Please sign in to comment.