Skip to content

Commit 694ed40

Browse files
committed
Iterate
1 parent 6f013d7 commit 694ed40

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

packages/voila/src/manager.ts

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -202,29 +202,42 @@ export class WidgetManager extends JupyterLabManager {
202202
{ version: '1.0.0' }
203203
);
204204

205-
// Send a models request msg
206-
initComm.send({method: 'request_states'}, {});
207-
208-
let [data, buffers]: any = await new Promise(resolve => {
209-
initComm.on_msg(msg => {
210-
const data = msg['content']['data'];
205+
// Fetch widget states
206+
let data: any;
207+
let buffers: any;
208+
try {
209+
await new Promise((resolve, reject) => {
210+
initComm.on_msg(msg => {
211+
data = msg['content']['data'];
212+
213+
if (data.method != 'update_states') {
214+
console.warn(`Unknown ${data.method} message on the Control channel`);
215+
return;
216+
}
211217

212-
if (data.method != 'update_states') {
213-
console.warn(`Unknown ${data.method} message on the Control channel`);
214-
return;
215-
}
218+
buffers = (msg.buffers || []).map((b: any) => {
219+
if (b instanceof DataView) {
220+
return b;
221+
} else {
222+
return new DataView(b instanceof ArrayBuffer ? b : b.buffer);
223+
}
224+
});
216225

217-
const buffers = (msg.buffers || []).map((b: any) => {
218-
if (b instanceof DataView) {
219-
return b;
220-
} else {
221-
return new DataView(b instanceof ArrayBuffer ? b : b.buffer);
222-
}
226+
resolve(null);
223227
});
224228

225-
resolve([data, buffers]);
229+
initComm.on_close(reject);
230+
231+
// Send a states request msg
232+
initComm.send({method: 'request_states'}, {});
226233
});
227-
});
234+
}
235+
catch {
236+
console.warn('Failed to open "jupyter.widget.control" comm channel, fallback to slow fetching of widgets.');
237+
// TODO Fallback to the old implementation for old ipywidgets versions
238+
// return this._build_models_slow();
239+
return {};
240+
}
228241

229242
initComm.close();
230243

@@ -242,6 +255,7 @@ export class WidgetManager extends JupyterLabManager {
242255

243256
const widgetPromises: Promise<base.WidgetModel>[] = [];
244257

258+
// Start creating all widgets
245259
for (const widget_id in states) {
246260
const state = states[widget_id];
247261

@@ -273,6 +287,7 @@ export class WidgetManager extends JupyterLabManager {
273287
}
274288
}
275289

290+
// Wait for widgets to be created
276291
const widgets = await Promise.all(widgetPromises);
277292
for (const model of widgets) {
278293
models[model.model_id] = model;

0 commit comments

Comments
 (0)