Skip to content
This repository was archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #357 from KsavinN/toggleLife-bug
Browse files Browse the repository at this point in the history
Toggle life bug
  • Loading branch information
jtpio authored Mar 13, 2020
2 parents 154a29d + 5d1c2d4 commit e442a89
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 25 deletions.
105 changes: 88 additions & 17 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@

import { JupyterFrontEnd } from '@jupyterlab/application';

import { ToolbarButton } from '@jupyterlab/apputils';
import {
ISessionContext,
SessionContext,
ToolbarButton
} from '@jupyterlab/apputils';

import { ConsolePanel } from '@jupyterlab/console';

import { IChangedArgs } from '@jupyterlab/coreutils';

import { DocumentWidget } from '@jupyterlab/docregistry';

import { FileEditor } from '@jupyterlab/fileeditor';
Expand Down Expand Up @@ -87,36 +93,67 @@ export class DebuggerHandler {
widget: DebuggerHandler.SessionWidget[DebuggerHandler.SessionType],
connection: Session.ISessionConnection
): Promise<void> {
const updateHandler = async () => {
if (!connection) {
delete this._kernelChangedHandlers[widget.id];
delete this._statusChangedHandlers[widget.id];
return this._update(widget, connection);
}

const kernelChanged = () => {
void this._update(widget, connection);
};
const kernelChangedHandler = this._kernelChangedHandlers[widget.id];

// setup handler when the kernel changes
const kernelChangedHandler = this._kernelChangedHandlers[connection.path];
if (kernelChangedHandler) {
connection.kernelChanged.disconnect(kernelChangedHandler);
}
connection.kernelChanged.connect(updateHandler);
this._kernelChangedHandlers[connection.path] = updateHandler;
this._kernelChangedHandlers[widget.id] = kernelChanged;
connection.kernelChanged.connect(kernelChanged);

// setup handler when the status of the kernel changes (restart)
const statusChanged = async (
sender: Session.ISessionConnection,
const statusChanged = (
_: Session.ISessionConnection,
status: Kernel.Status
) => {
if (status.endsWith('restarting')) {
return updateHandler();
void this._update(widget, connection);
}
};

const statusChangedHandler = this._statusChangedHandlers[connection.path];
const statusChangedHandler = this._statusChangedHandlers[widget.id];
if (statusChangedHandler) {
connection.statusChanged.disconnect(statusChangedHandler);
}
connection.statusChanged.connect(statusChanged);
this._statusChangedHandlers[connection.path] = statusChanged;
this._statusChangedHandlers[widget.id] = statusChanged;

return this._update(widget, connection);
}

/**
* Update a debug handler for the given widget, and
* handle connection kernel changed events.
* @param widget The widget to update.
* @param sessionContext The session context.
*/
async updateContext(
widget: DebuggerHandler.SessionWidget[DebuggerHandler.SessionType],
sessionContext: ISessionContext
): Promise<void> {
const connectionChanged = () => {
const { session: connection } = sessionContext;
void this.update(widget, connection);
};

const contextKernelChangedHandlers = this._contextKernelChangedHandlers[
widget.id
];

return updateHandler();
if (contextKernelChangedHandlers) {
sessionContext.kernelChanged.disconnect(contextKernelChangedHandlers);
}
this._contextKernelChangedHandlers[widget.id] = connectionChanged;
sessionContext.kernelChanged.connect(connectionChanged);

return this.update(widget, sessionContext.session);
}

/**
Expand Down Expand Up @@ -183,10 +220,11 @@ export class DebuggerHandler {
delete this._handlers[widget.id];
delete this._kernelChangedHandlers[widget.id];
delete this._statusChangedHandlers[widget.id];
delete this._contextKernelChangedHandlers[widget.id];

// clear the model if the handler being removed corresponds
// to the current active debug session
if (this._service.session?.connection?.path === connection.path) {
if (this._service.session?.connection?.path === connection?.path) {
const model = this._service.model as DebuggerModel;
model.clear();
}
Expand Down Expand Up @@ -219,10 +257,16 @@ export class DebuggerHandler {
return;
}

if (this._service.isStarted) {
if (
this._service.isStarted &&
this._previousConnection.id === connection.id
) {
this._service.session.connection = connection;
await this._service.stop();
removeHandlers();
} else {
this._service.session.connection = connection;
this._previousConnection = connection;
await this._service.restoreState(true);
await createHandler();
}
Expand All @@ -239,6 +283,9 @@ export class DebuggerHandler {
if (!this._service.session) {
this._service.session = new DebugSession({ connection });
} else {
this._previousConnection = this._service.session.connection.kernel
? this._service.session.connection
: null;
this._service.session.connection = connection;
}

Expand All @@ -248,11 +295,14 @@ export class DebuggerHandler {
// check the state of the debug session
if (!this._service.isStarted) {
removeHandlers();
this._service.session.connection = this._previousConnection ?? connection;
await this._service.restoreState(false);
return;
}

// if the debugger is started but there is no handler, create a new one
await createHandler();
this._previousConnection = connection;

// listen to the disposed signals
widget.disposed.connect(removeHandlers);
Expand All @@ -262,10 +312,31 @@ export class DebuggerHandler {
private _type: DebuggerHandler.SessionType;
private _shell: JupyterFrontEnd.IShell;
private _service: IDebugger;
private _previousConnection: Session.ISessionConnection;
private _handlers: {
[id: string]: DebuggerHandler.SessionHandler[DebuggerHandler.SessionType];
} = {};
private _kernelChangedHandlers: { [id: string]: () => void } = {};

private _contextKernelChangedHandlers: {
[id: string]: (
sender: SessionContext,
args: IChangedArgs<
Kernel.IKernelConnection,
Kernel.IKernelConnection,
'kernel'
>
) => void;
} = {};
private _kernelChangedHandlers: {
[id: string]: (
sender: Session.ISessionConnection,
args: IChangedArgs<
Kernel.IKernelConnection,
Kernel.IKernelConnection,
'kernel'
>
) => void;
} = {};
private _statusChangedHandlers: {
[id: string]: (
sender: Session.ISessionConnection,
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ const consoles: JupyterFrontEndPlugin<void> = {
});

const updateHandlerAndCommands = async (widget: ConsolePanel) => {
const sessionContext = widget.sessionContext;
const { sessionContext } = widget;
await sessionContext.ready;
await handler.update(widget, sessionContext.session);
await handler.updateContext(widget, sessionContext);
app.commands.notifyCommandChanged();
};

Expand Down Expand Up @@ -202,9 +202,9 @@ const notebooks: JupyterFrontEndPlugin<void> = {
});

const updateHandlerAndCommands = async (widget: NotebookPanel) => {
const sessionContext = widget.sessionContext;
const { sessionContext } = widget;
await sessionContext.ready;
await handler.update(widget, sessionContext.session);
await handler.updateContext(widget, sessionContext);
app.commands.notifyCommandChanged();
};

Expand Down
5 changes: 2 additions & 3 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class DebuggerService implements IDebugger, IDisposable {
* @param connection The session connection.
*/
async isAvailable(connection: Session.ISessionConnection): Promise<boolean> {
const kernel = connection.kernel;
const kernel = connection?.kernel;
if (!kernel) {
return false;
}
Expand Down Expand Up @@ -249,7 +249,7 @@ export class DebuggerService implements IDebugger, IDisposable {
this._model.breakpoints.restoreBreakpoints(bpMap);
if (stoppedThreads.size !== 0) {
await this._getAllFrames();
} else {
} else if (this.isStarted) {
this._clearModel();
this._clearSignals();
}
Expand Down Expand Up @@ -535,7 +535,6 @@ export class DebuggerService implements IDebugger, IDisposable {
* Clear the current model.
*/
private _clearModel() {
this._model.title = this.isStarted ? this.session?.connection?.name : '-';
this._model.callstack.frames = [];
this._model.variables.scopes = [];
}
Expand Down
1 change: 0 additions & 1 deletion src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export class DebugSession implements IDebugger.ISession {
if (this._connection) {
this._connection.iopubMessage.disconnect(this._handleEvent, this);
}

this._connection = connection;

if (!this._connection) {
Expand Down

0 comments on commit e442a89

Please sign in to comment.