Browse Source

Merge pull request #53 from JohanMabille/start_stop

Start and stop always visible, enabled/disabled
Jeremy Tuloup 5 years ago
parent
commit
3290cdea2a
4 changed files with 32 additions and 18 deletions
  1. 6 3
      src/debugger.ts
  2. 11 15
      src/index.ts
  3. 10 0
      src/session.ts
  4. 5 0
      src/tokens.ts

+ 6 - 3
src/debugger.ts

@@ -15,8 +15,6 @@ import { BoxPanel } from '@phosphor/widgets';
 
 import { IDebugger } from './tokens';
 
-import { DebugSession } from './session';
-
 import { DebuggerSidebar } from './sidebar';
 
 export class Debugger extends BoxPanel {
@@ -57,7 +55,12 @@ export namespace Debugger {
   export class Model implements IDisposable {
     constructor(options: Debugger.Model.IOptions) {
       this.connector = options.connector || null;
-      this.session = new DebugSession({ client: options.session });
+      // Avoids setting session with invalid client
+      // session should be set only when a notebook or
+      // a console get the focus.
+      // TODO: also checks that the notebook or console
+      // runs a kernel with debugging ability
+      this.session = null;
       this.id = options.id;
       void this._populate();
     }

+ 11 - 15
src/index.ts

@@ -32,8 +32,6 @@ import { DebuggerNotebookHandler } from './handlers/notebook';
 
 import { DebuggerConsoleHandler } from './handlers/console';
 
-import { IDisposable } from '@phosphor/disposable';
-
 import { Kernel } from '@jupyterlab/services';
 
 /**
@@ -224,7 +222,6 @@ const main: JupyterFrontEndPlugin<IDebugger> = {
     });
     const { commands, shell } = app;
     let widget: MainAreaWidget<Debugger>;
-    let commandStop: IDisposable;
 
     const getModel = () => {
       return tracker.currentWidget ? tracker.currentWidget.content.model : null;
@@ -273,12 +270,15 @@ const main: JupyterFrontEndPlugin<IDebugger> = {
 
     commands.addCommand(CommandIDs.stop, {
       label: 'Stop',
+      isEnabled: () => {
+        const debuggerModel = getModel();
+        return (debuggerModel &&
+          debuggerModel.session !== null &&
+          debuggerModel.session.isStarted) as boolean;
+      },
       execute: async () => {
         const debuggerModel = getModel();
-        if (debuggerModel) {
-          await debuggerModel.session.stop();
-          commandStop.dispose();
-        }
+        await debuggerModel.session.stop();
       }
     });
 
@@ -287,17 +287,12 @@ const main: JupyterFrontEndPlugin<IDebugger> = {
       isEnabled: () => {
         const debuggerModel = getModel();
         return (debuggerModel &&
-          debuggerModel.session !== undefined) as boolean;
+          debuggerModel.session !== null &&
+          !debuggerModel.session.isStarted) as boolean;
       },
       execute: async () => {
         const debuggerModel = getModel();
-        if (debuggerModel && debuggerModel.session) {
-          await debuggerModel.session.start();
-          commandStop = palette.addItem({
-            command: CommandIDs.stop,
-            category: 'Debugger'
-          });
-        }
+        await debuggerModel.session.start();
       }
     });
 
@@ -360,6 +355,7 @@ const main: JupyterFrontEndPlugin<IDebugger> = {
       palette.addItem({ command: CommandIDs.changeMode, category: 'Debugger' });
       palette.addItem({ command: CommandIDs.create, category: 'Debugger' });
       palette.addItem({ command: CommandIDs.start, category: 'Debugger' });
+      palette.addItem({ command: CommandIDs.stop, category: 'Debugger' });
     }
 
     if (restorer) {

+ 10 - 0
src/session.ts

@@ -87,10 +87,18 @@ export class DebugSession implements IDebugger.ISession {
     return this._isDisposed;
   }
 
+  /**
+   * Whether the debug session is started
+   */
+  get isStarted(): boolean {
+    return this._isStarted;
+  }
+
   /**
    * Start a new debug session
    */
   async start(): Promise<void> {
+    this._isStarted = true;
     await this.sendRequest('initialize', {
       clientID: 'jupyterlab',
       clientName: 'JupyterLab',
@@ -111,6 +119,7 @@ export class DebugSession implements IDebugger.ISession {
    * Stop the running debug session.
    */
   async stop(): Promise<void> {
+    this._isStarted = false;
     await this.sendRequest('disconnect', {
       restart: false,
       terminateDebuggee: true
@@ -176,6 +185,7 @@ export class DebugSession implements IDebugger.ISession {
 
   private _disposed = new Signal<this, void>(this);
   private _isDisposed: boolean = false;
+  private _isStarted: boolean = false;
   private _eventMessage = new Signal<DebugSession, IDebugger.ISession.Event>(
     this
   );

+ 5 - 0
src/tokens.ts

@@ -65,6 +65,11 @@ export namespace IDebugger {
      */
     editors: CodeEditor.IEditor[];
 
+    /**
+     * Whether the debug session is started
+     */
+    isStarted: boolean;
+
     /**
      * Start a new debug session.
      */