Skip to content

Commit 5e239c9

Browse files
authored
Dynamic widgets registry (#3988)
* Dynamic widgets registry * Linter * Rename registeredWidget -> widgetRegistered
1 parent f1664e6 commit 5e239c9

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

python/jupyterlab_widgets/src/plugin.ts

+32-3
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,31 @@ import '@jupyter-widgets/controls/css/widgets-base.css';
5858
import { KernelMessage } from '@jupyterlab/services';
5959
import { ITranslator, nullTranslator } from '@jupyterlab/translation';
6060
import { ISessionContext } from '@jupyterlab/apputils';
61+
import { ISignal, Signal } from '@lumino/signaling';
6162

62-
const WIDGET_REGISTRY: base.IWidgetRegistryData[] = [];
63+
class WidgetRegistry {
64+
get widgets(): base.IWidgetRegistryData[] {
65+
return [...this._registry];
66+
}
67+
68+
push(data: base.IWidgetRegistryData) {
69+
this._registry.push(data);
70+
this._widgetRegistered.emit(data);
71+
}
72+
73+
get widgetRegistered(): ISignal<WidgetRegistry, base.IWidgetRegistryData> {
74+
return this._widgetRegistered;
75+
}
76+
77+
private _widgetRegistered = new Signal<
78+
WidgetRegistry,
79+
base.IWidgetRegistryData
80+
>(this);
81+
82+
private _registry: base.IWidgetRegistryData[] = [];
83+
}
84+
85+
const WIDGET_REGISTRY = new WidgetRegistry();
6386

6487
/**
6588
* The cached settings.
@@ -178,7 +201,10 @@ async function registerWidgetHandler(
178201

179202
if (!wManager) {
180203
wManager = widgetManagerFactory();
181-
WIDGET_REGISTRY.forEach((data) => wManager!.register(data));
204+
WIDGET_REGISTRY.widgets.forEach((data) => wManager!.register(data));
205+
WIDGET_REGISTRY.widgetRegistered.connect((_, data) => {
206+
wManager!.register(data);
207+
});
182208
Private.widgetManagerProperty.set(wManagerOwner, wManager);
183209
currentOwner = wManagerOwner;
184210
content.disposed.connect((_) => {
@@ -241,7 +267,10 @@ export function registerWidgetManager(
241267
) as WidgetManager;
242268
if (!currentManager) {
243269
wManager = new WidgetManager(context, rendermime, SETTINGS);
244-
WIDGET_REGISTRY.forEach((data) => wManager!.register(data));
270+
WIDGET_REGISTRY.widgets.forEach((data) => wManager!.register(data));
271+
WIDGET_REGISTRY.widgetRegistered.connect((_, data) => {
272+
wManager!.register(data);
273+
});
245274
Private.widgetManagerProperty.set(wManagerOwner, wManager);
246275
} else {
247276
wManager = currentManager;

0 commit comments

Comments
 (0)