Skip to content

Commit f1ac0d8

Browse files
Reset state upon manual terminal closing (#70)
* Reset state upon manual terminal closing * Explain more behavior in docstring * Fix order of command invocations * Avoid spawning preview notification when disposing terminal on startup * Clarify docstring for reset active shell * Remove unnecessary docstring part * fix error - couldn't start the extension * Use boolean datatype (instead of any) --------- Co-authored-by: Vladimir Fokow <[email protected]>
1 parent c9d35c6 commit f1ac0d8

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

β€Žsrc/manimShell.tsβ€Ž

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,13 @@ export interface CommandExecutionEventHandler {
7575
* Callback that is invoked when the command is issued, i.e. sent to the
7676
* terminal. At this point, the command is probably not yet finished
7777
* executing.
78+
*
79+
* @param shellStillExists Whether the shell still exists after the command
80+
* was issued. In certain scenarios (e.g. user manually exits the shell
81+
* during Manim startup), the shell might not exist anymore after the
82+
* command was issued.
7883
*/
79-
onCommandIssued?: () => void;
84+
onCommandIssued?: (shellStillExists: boolean) => void;
8085

8186
/**
8287
* Callback that is invoked when data is received from the active Manim
@@ -292,7 +297,7 @@ export class ManimShell {
292297
let currentExecutionCount = this.iPythonCellCount;
293298

294299
this.exec(shell, command);
295-
handler?.onCommandIssued?.();
300+
handler?.onCommandIssued?.(this.activeShell !== null);
296301

297302
this.waitUntilCommandFinished(currentExecutionCount, () => {
298303
Logger.debug("πŸ”“ Command execution unlocked");
@@ -370,6 +375,10 @@ export class ManimShell {
370375
/**
371376
* Resets the active shell such that a new terminal is created on the next
372377
* command execution.
378+
*
379+
* This will also remove all event listeners! Having called this method,
380+
* you should NOT emit any events anymore before a new Manim shell is
381+
* detected, as those events would not be caught by any listeners.
373382
*/
374383
public resetActiveShell() {
375384
Logger.debug("πŸ’« Reset active shell");
@@ -631,6 +640,29 @@ export class ManimShell {
631640
this.resetActiveShell();
632641
}
633642
});
643+
644+
/**
645+
* This event is fired when a terminal is closed manually by the user.
646+
* In this case, we can only do our best to clean up since the terminal
647+
* is probably not able to receive commands anymore. It's mostly for us
648+
* to reset all states such that we're ready for the next command.
649+
*
650+
* When closing while previewing a scene, the terminal is closed, however
651+
* we are not able to send a keyboard interrupt anymore. Therefore,
652+
* ManimGL will take a few seconds to actually close its window. When
653+
* the user employs our "Quit preview" command instead, the preview will
654+
* be closed immediately.
655+
*/
656+
window.onDidCloseTerminal(async (terminal: Terminal) => {
657+
if (terminal !== this.activeShell) {
658+
return;
659+
}
660+
Logger.debug("πŸ”š Active shell closed");
661+
this.eventEmitter.emit(ManimShellEvent.MANIM_NOT_STARTED);
662+
this.eventEmitter.emit(ManimShellEvent.KEYBOARD_INTERRUPT);
663+
this.forceQuitActiveShell(); // don't await here on purpose
664+
this.resetActiveShell();
665+
});
634666
}
635667
}
636668

β€Žsrc/previewCode.tsβ€Ž

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,15 @@ export async function previewCode(code: string, startLine: number): Promise<void
3434

3535
await ManimShell.instance.executeCommand(
3636
PREVIEW_COMMAND, startLine, true, {
37-
onCommandIssued: () => {
37+
onCommandIssued: (shellStillExists) => {
3838
Logger.debug(`πŸ“Š Command issued: ${PREVIEW_COMMAND}. Will restore clipboard`);
3939
restoreClipboard(clipboardBuffer);
40-
progress = new PreviewProgress();
40+
if (shellStillExists) {
41+
Logger.debug("πŸ“Š Initializing preview progress");
42+
progress = new PreviewProgress();
43+
} else {
44+
Logger.debug("πŸ“Š Shell was closed in the meantime, not showing progress");
45+
}
4146
},
4247
onData: (data) => {
4348
progress?.reportOnData(data);

0 commit comments

Comments
Β (0)