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

Refactor the DebugHandler #393

Closed
jtpio opened this issue Mar 13, 2020 · 1 comment
Closed

Refactor the DebugHandler #393

jtpio opened this issue Mar 13, 2020 · 1 comment
Milestone

Comments

@jtpio
Copy link
Member

jtpio commented Mar 13, 2020

As mentioned in #357, the DebugHandler is getting more complicated as it now handles session context changes (for notebooks and consoles), as well as session connection changes for files:

debugger/src/handler.ts

Lines 164 to 310 in e442a89

private async _update(
widget: DebuggerHandler.SessionWidget[DebuggerHandler.SessionType],
connection: Session.ISessionConnection
): Promise<void> {
if (!this._service.model) {
return;
}
const hasFocus = () => {
return this._shell.currentWidget && this._shell.currentWidget === widget;
};
const updateAttribute = () => {
if (!this._handlers[widget.id]) {
widget.node.removeAttribute('data-jp-debugger');
return;
}
widget.node.setAttribute('data-jp-debugger', 'true');
};
const createHandler = async () => {
if (this._handlers[widget.id]) {
return;
}
switch (this._type) {
case 'notebook':
this._handlers[widget.id] = new NotebookHandler({
debuggerService: this._service,
widget: widget as NotebookPanel
});
break;
case 'console':
this._handlers[widget.id] = new ConsoleHandler({
debuggerService: this._service,
widget: widget as ConsolePanel
});
break;
case 'file':
this._handlers[widget.id] = new FileHandler({
debuggerService: this._service,
widget: widget as DocumentWidget<FileEditor>
});
break;
default:
throw Error(`No handler for the type ${this._type}`);
}
updateAttribute();
};
const removeHandlers = () => {
const handler = this._handlers[widget.id];
if (!handler) {
return;
}
handler.dispose();
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) {
const model = this._service.model as DebuggerModel;
model.clear();
}
updateAttribute();
};
const addToolbarButton = () => {
const button = this._buttons[widget.id];
if (button) {
return;
}
const newButton = updateToolbar(widget, toggleDebugging);
this._buttons[widget.id] = newButton;
};
const removeToolbarButton = () => {
const button = this._buttons[widget.id];
if (!button) {
return;
}
button.parent = null;
button.dispose();
delete this._buttons[widget.id];
};
const toggleDebugging = async () => {
// bail if the widget doesn't have focus
if (!hasFocus()) {
return;
}
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();
}
};
const debuggingEnabled = await this._service.isAvailable(connection);
if (!debuggingEnabled) {
removeHandlers();
removeToolbarButton();
return;
}
// update the active debug session
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;
}
await this._service.restoreState(false);
addToolbarButton();
// 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);
this._service.model.disposed.connect(removeHandlers);
}

@jtpio
Copy link
Member Author

jtpio commented Jan 12, 2021

Closing as this should eventually be addressed in https://github.com/jupyterlab/jupyterlab directly.

@jtpio jtpio closed this as completed Jan 12, 2021
@jtpio jtpio modified the milestones: 0.3.0, Future Jan 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants