Skip to content

Commit

Permalink
fix: cleaner unsaved dialog logic (#539)
Browse files Browse the repository at this point in the history
  • Loading branch information
erickzhao authored Nov 27, 2020
1 parent 445e173 commit df22a5f
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 38 deletions.
11 changes: 1 addition & 10 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,7 @@ export async function onReady() {
*/
export function onBeforeQuit() {
ipcMainManager.send(IpcEvents.BEFORE_QUIT);
ipcMainManager.on(IpcEvents.CONFIRM_QUIT, quitAppIfConfirmed);
}

export function quitAppIfConfirmed(
_: Electron.IpcMainEvent,
quitConfirmed: boolean,
) {
if (quitConfirmed) {
app.quit();
}
ipcMainManager.on(IpcEvents.CONFIRM_QUIT, app.quit);
}

export function setupMenuHandler() {
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/components/commands-action-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export class GistActionButton extends React.Component<

if (description) {
await this.publishGist(description);
appState.isUnsaved = false;
}

appState.genericDialogLastInput = null;
Expand All @@ -199,6 +200,7 @@ export class GistActionButton extends React.Component<
files: this.gistFilesList(values) as any,
});

appState.isUnsaved = false;
console.log('Updating: Updating done', { gist });
this.renderToast({ message: 'Successfully updated gist!' });
} catch (error) {
Expand Down Expand Up @@ -231,6 +233,7 @@ export class GistActionButton extends React.Component<
gist_id: appState.gistId!,
});

appState.isUnsaved = true;
console.log('Deleting: Deleting done', { gist });
this.renderToast({ message: 'Successfully deleted gist!' });
} catch (error) {
Expand Down
20 changes: 9 additions & 11 deletions src/renderer/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,32 +250,30 @@ export class AppState {
autorun(() => {
if (typeof this.isUnsaved === 'undefined') return;

if (!!this.isUnsaved) {
if (this.isUnsaved) {
window.onbeforeunload = () => {
ipcRendererManager.send(IpcEvents.SHOW_INACTIVE);
this.setGenericDialogOptions({
type: GenericDialogType.warning,
label: `The current Fiddle is unsaved. Do you want to exit anyway?`,
ok: 'Quit',
ok: 'Exit',
});

this.isGenericDialogShowing = true;

// We'll wait until the warning dialog was closed
when(() => !this.isGenericDialogShowing).then(() => {
const quitConfirmed = this.genericDialogLastResult;
const closeConfirmed = this.genericDialogLastResult;
// The user confirmed, let's close for real.
if (quitConfirmed) {
if (closeConfirmed) {
// isQuitting checks if we're trying to quit the app
// or just close the window
if (this.isQuitting) {
ipcRendererManager.send(IpcEvents.CONFIRM_QUIT);
}
window.onbeforeunload = null;
window.close();
}

// isQuitting checks if we're trying to quit the app
// or just close the window
if (this.isQuitting) {
ipcRendererManager.send(IpcEvents.CONFIRM_QUIT, quitConfirmed);
this.isQuitting = false;
}
});

// return value doesn't matter, we just want to cancel the event
Expand Down
15 changes: 1 addition & 14 deletions tests/main/main-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
onBeforeQuit,
onReady,
onWindowsAllClosed,
quitAppIfConfirmed,
setupMenuHandler,
} from '../../src/main/main';
import { shouldQuit } from '../../src/main/squirrel';
Expand Down Expand Up @@ -78,23 +77,11 @@ describe('main', () => {
expect(ipcMainManager.send).toHaveBeenCalledWith(IpcEvents.BEFORE_QUIT);
expect(ipcMainManager.on).toHaveBeenCalledWith(
IpcEvents.CONFIRM_QUIT,
quitAppIfConfirmed,
app.quit,
);
});
});

describe('quitAppIfConfirmed()', () => {
it('quits app if passed a truthy value', () => {
quitAppIfConfirmed({} as any, true);
expect(app.quit).toHaveBeenCalledTimes(1);
});

it('does nothing if passed a value', () => {
quitAppIfConfirmed({} as any, false);
expect(app.quit).toHaveBeenCalledTimes(0);
});
});

describe('onReady()', () => {
it('opens a BrowserWindow, sets up updates', async () => {
await onReady();
Expand Down
4 changes: 1 addition & 3 deletions tests/renderer/state-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ describe('AppState', () => {
expect(window.close).toHaveBeenCalledTimes(1);
expect(ipcRendererManager.send).toHaveBeenCalledWith(
IpcEvents.CONFIRM_QUIT,
true,
);
done();
});
Expand All @@ -135,9 +134,8 @@ describe('AppState', () => {
appState.isQuitting = true;
process.nextTick(() => {
expect(window.close).toHaveBeenCalledTimes(0);
expect(ipcRendererManager.send).toHaveBeenCalledWith(
expect(ipcRendererManager.send).not.toHaveBeenCalledWith(
IpcEvents.CONFIRM_QUIT,
false,
);
done();
});
Expand Down

0 comments on commit df22a5f

Please sign in to comment.