Skip to content

Commit

Permalink
Handle delete with optionals
Browse files Browse the repository at this point in the history
  • Loading branch information
jtpio committed Mar 26, 2021
1 parent 3e9c51a commit d5b900c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 33 deletions.
40 changes: 23 additions & 17 deletions packages/base/src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,16 @@ export class WidgetModel extends Backbone.Model {
delete this.comm;
}
// Delete all views of this model
const views = Object.keys(this.views).map((id: string) => {
return this.views[id].then(view => view.remove());
});
delete this.views;
return Promise.all(views).then(() => {
return;
});
if (this.views) {
const views = Object.keys(this.views).map((id: string) => {
return this.views![id].then(view => view.remove());
});
delete this.views;
return Promise.all(views).then(() => {
return;
});
}
return Promise.resolve();
}

/**
Expand Down Expand Up @@ -466,6 +469,9 @@ export class WidgetModel extends Backbone.Model {
* Send a sync message to the kernel.
*/
send_sync_message(state: JSONObject, callbacks: any = {}): void {
if (!this.comm) {
return;
}
try {
callbacks.iopub = callbacks.iopub || {};
const statuscb = callbacks.iopub.status;
Expand Down Expand Up @@ -575,9 +581,9 @@ export class WidgetModel extends Backbone.Model {
// values subclasses may set in their initialization functions.
widget_manager: IWidgetManager;
model_id: string;
views: { [key: string]: Promise<WidgetView> };
views?: { [key: string]: Promise<WidgetView> };
state_change: Promise<any>;
comm: IClassicComm;
comm?: IClassicComm;
name: string;
module: string;

Expand Down Expand Up @@ -750,7 +756,7 @@ export namespace WidgetView {

export namespace JupyterLuminoWidget {
export interface IOptions {
view: DOMWidgetView;
view?: DOMWidgetView;
}
}

Expand All @@ -759,7 +765,7 @@ export class JupyterLuminoWidget extends Widget {
const view = options.view;
delete options.view;
super(options);
this._view = view;
this._view = view ?? null;
}

/**
Expand All @@ -775,7 +781,7 @@ export class JupyterLuminoWidget extends Widget {
if (this._view) {
this._view.remove();
}
this._view = null!;
this._view = null;
}

/**
Expand All @@ -786,18 +792,18 @@ export class JupyterLuminoWidget extends Widget {
*/
processMessage(msg: Message): void {
super.processMessage(msg);
this._view.processLuminoMessage(msg);
this._view?.processLuminoMessage(msg);
}

private _view: DOMWidgetView;
private _view: DOMWidgetView | null;
}

export class JupyterLuminoPanelWidget extends Panel {
constructor(options: JupyterLuminoWidget.IOptions & Panel.IOptions) {
const view = options.view;
delete options.view;
super(options);
this._view = view;
this._view = view ?? null;
}

/**
Expand All @@ -808,7 +814,7 @@ export class JupyterLuminoPanelWidget extends Panel {
*/
processMessage(msg: Message): void {
super.processMessage(msg);
this._view.processLuminoMessage(msg);
this._view?.processLuminoMessage(msg);
}

/**
Expand All @@ -827,7 +833,7 @@ export class JupyterLuminoPanelWidget extends Panel {
this._view = null!;
}

private _view: DOMWidgetView;
private _view: DOMWidgetView | null;
}

export class DOMWidgetView extends WidgetView {
Expand Down
32 changes: 16 additions & 16 deletions packages/controls/src/widget_selectioncontainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
JupyterLuminoWidget,
WidgetModel,
reject,
WidgetView
WidgetView,
} from '@jupyter-widgets/base';

import { BoxModel, BoxView } from './widget_box';
Expand All @@ -32,7 +32,7 @@ export class SelectionContainerModel extends BoxModel {
...super.defaults(),
_model_name: 'SelectionContainerModel',
selected_index: null,
_titles: []
_titles: [],
};
}
}
Expand All @@ -42,7 +42,7 @@ export class AccordionModel extends SelectionContainerModel {
return {
...super.defaults(),
_model_name: 'AccordionModel',
_view_name: 'AccordionView'
_view_name: 'AccordionView',
};
}
}
Expand All @@ -55,7 +55,7 @@ export class JupyterLuminoAccordionWidget extends Accordion {
const view = options.view;
delete options.view;
super(options);
this._view = view;
this._view = view ?? null;
}

/**
Expand All @@ -66,7 +66,7 @@ export class JupyterLuminoAccordionWidget extends Accordion {
*/
processMessage(msg: Message): void {
super.processMessage(msg);
this._view.processLuminoMessage(msg);
this._view?.processLuminoMessage(msg);
}

/**
Expand All @@ -85,7 +85,7 @@ export class JupyterLuminoAccordionWidget extends Accordion {
this._view = null!;
}

private _view: DOMWidgetView;
private _view: DOMWidgetView | null;
}

export class AccordionView extends DOMWidgetView {
Expand Down Expand Up @@ -127,7 +127,7 @@ export class AccordionView extends DOMWidgetView {
accordion.addClass('jupyter-widgets');
accordion.addClass('widget-accordion');
accordion.addClass('widget-container');
accordion.selection.selectionChanged.connect(sender => {
accordion.selection.selectionChanged.connect((sender) => {
if (!this.updatingChildren) {
this.model.set('selected_index', accordion.selection.index);
this.touch();
Expand Down Expand Up @@ -219,7 +219,7 @@ export class TabModel extends SelectionContainerModel {
return {
...super.defaults(),
_model_name: 'TabModel',
_view_name: 'TabView'
_view_name: 'TabView',
};
}
}
Expand All @@ -232,14 +232,14 @@ export class JupyterLuminoTabPanelWidget extends TabPanel {
const view = options.view;
delete options.view;
super(options);
this._view = view;
this._view = view ?? null;
// We want the view's messages to be the messages the tabContents panel
// gets.
MessageLoop.installMessageHook(this.tabContents, (handler, msg) => {
// There may be times when we want the view's handler to be called
// *after* the message has been processed by the widget, in which
// case we'll need to revisit using a message hook.
this._view.processLuminoMessage(msg);
this._view?.processLuminoMessage(msg);
return true;
});
}
Expand All @@ -257,16 +257,16 @@ export class JupyterLuminoTabPanelWidget extends TabPanel {
if (this._view) {
this._view.remove();
}
this._view = null!;
this._view = null;
}

private _view: DOMWidgetView;
private _view: DOMWidgetView | null;
}

export class TabView extends DOMWidgetView {
_createElement(tagName: string): HTMLElement {
this.luminoWidget = new JupyterLuminoTabPanelWidget({
view: this
view: this,
});
return this.luminoWidget.node;
}
Expand All @@ -288,7 +288,7 @@ export class TabView extends DOMWidgetView {
super.initialize(parameters);
this.childrenViews = new ViewList(
this.addChildView,
view => {
(view) => {
view.remove();
},
this
Expand Down Expand Up @@ -433,7 +433,7 @@ export class StackedModel extends SelectionContainerModel {
return {
...super.defaults(),
_model_name: 'StackedModel',
_view_name: 'StackedView'
_view_name: 'StackedView',
};
}
}
Expand All @@ -452,7 +452,7 @@ export class StackedView extends BoxView {
?.update([selected_child])
.then((views: DOMWidgetView[]) => {
// Notify all children that their sizes may have changed.
views.forEach(view => {
views.forEach((view) => {
MessageLoop.postMessage(
view.luminoWidget,
Widget.ResizeMessage.UnknownSize
Expand Down

0 comments on commit d5b900c

Please sign in to comment.