Skip to content

Commit 861ae8c

Browse files
authored
prevent simulator from running in background when switching view (microsoft#11547)
1 parent 9080e02 commit 861ae8c

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎localtypings/pxteditor.d.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,7 @@ declare namespace pxt.editor {
831831
feedback?: FeedbackState;
832832
themePickerOpen?: boolean;
833833
errorListNote?: string;
834+
timeMachine?: boolean;
834835
}
835836

836837
export interface EditorState {

‎webapp/src/app.tsx‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,17 @@ export class ProjectView
11701170
restartSimulator: () => this.restartSimulator(),
11711171
singleSimulator: () => this.singleSimulator(),
11721172
onStateChanged: (state) => {
1173-
const simStateChanged = () => this.allEditors.forEach((editor) => editor.simStateChanged());
1173+
const simStateChanged = () => {
1174+
// If the simulator was pending a run when the user switched to
1175+
// a different view, stop the simulator to avoid running in the background
1176+
if (
1177+
state === pxsim.SimulatorState.Running &&
1178+
this.isSimulatorInaccessible()
1179+
) {
1180+
this.stopSimulator();
1181+
}
1182+
this.allEditors.forEach((editor) => editor.simStateChanged())
1183+
};
11741184
switch (state) {
11751185
case pxsim.SimulatorState.Paused:
11761186
case pxsim.SimulatorState.Unloaded:
@@ -4174,6 +4184,14 @@ export class ProjectView
41744184
})();
41754185
}
41764186

4187+
protected isSimulatorInaccessible() {
4188+
return this.state.home ||
4189+
this.state.extensionsVisible ||
4190+
this.profileDialog?.state?.visible ||
4191+
this.scriptSearch?.state.visible ||
4192+
this.state.timeMachine
4193+
}
4194+
41774195
openNewTab(hd: pxt.workspace.Header, dependent: boolean) {
41784196
if (!hd
41794197
|| pxt.BrowserUtils.isElectron()
@@ -4710,11 +4728,15 @@ export class ProjectView
47104728
this.stopSimulator();
47114729
}
47124730

4731+
this.setState({ timeMachine: true });
4732+
47134733
await dialogs.showTurnBackTimeDialogAsync(this.state.header, () => {
47144734
this.reloadHeaderAsync();
47154735
simWasRunning = false;
47164736
});
47174737

4738+
this.setState({ timeMachine: false });
4739+
47184740
if (simWasRunning) {
47194741
this.startSimulator();
47204742
}

0 commit comments

Comments
 (0)