Skip to content

Commit

Permalink
Handle promises
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Rieseberg committed Oct 8, 2022
1 parent c483871 commit c3537ae
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions src/renderer/emulator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ export class Emulator extends React.Component<{}, EmulatorState> {

await this.saveState();
this.unlockMouse();
emulator.stop();
await emulator.stop();
this.setState({ isRunning: false });

document.body.classList.add("paused");
Expand All @@ -385,25 +385,17 @@ export class Emulator extends React.Component<{}, EmulatorState> {
const { emulator } = this.state;
const statePath = await getStatePath();

return new Promise((resolve) => {
if (!emulator || !emulator.save_state) {
console.log(`restoreState: No emulator present`);
return resolve();
}

emulator.save_state(async (error: Error, newState: ArrayBuffer) => {
if (error) {
console.warn(`saveState: Could not save state`, error);
return resolve();
}

await fs.outputFile(statePath, Buffer.from(newState));

console.log(`saveState: Saved state to ${statePath}`);
if (!emulator || !emulator.save_state) {
console.log(`restoreState: No emulator present`);
return;
}

resolve();
});
});
try {
const newState = await emulator.save_state();
await fs.outputFile(statePath, Buffer.from(newState));
} catch (error) {
console.warn(`saveState: Could not save state`, error);
}
}

/**
Expand All @@ -423,7 +415,7 @@ export class Emulator extends React.Component<{}, EmulatorState> {
}

try {
this.state.emulator.restore_state(state);
await this.state.emulator.restore_state(state);
} catch (error) {
console.log(
`State: Could not read state file. Maybe none exists?`,
Expand Down

0 comments on commit c3537ae

Please sign in to comment.