Skip to content

Commit 6c1fae8

Browse files
committed
Binary buffers
1 parent 4bac3f4 commit 6c1fae8

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

packages/voila/src/manager.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -196,35 +196,49 @@ export class WidgetManager extends JupyterLabManager {
196196

197197
async _build_models(): Promise<{ [key: string]: base.WidgetModel }> {
198198
const models: { [key: string]: base.WidgetModel } = {};
199-
const comm_id = base.uuid();
199+
const commId = base.uuid();
200200
const initComm = await this._create_comm(
201201
'jupyter.widget.control',
202-
comm_id,
202+
commId,
203203
{ widgets: null }
204204
);
205205

206-
const widgets_info: any = await new Promise(resolve => {
206+
const [widgetsInfo, buffers]: any = await new Promise(resolve => {
207207
initComm.on_msg(msg => {
208208
const info = JSON.parse(
209209
Buffer.from(new Int8Array(msg.buffers[0].buffer)).toString('utf-8')
210210
);
211-
resolve(info);
211+
resolve([info, msg.buffers.slice(1)]);
212212
});
213213
});
214214

215215
initComm.close();
216216

217-
const widget_states: any = widgets_info[0];
218-
// const buffers: any = widgets_info[1];
217+
const widgetStates: any = widgetsInfo[0];
219218

220-
const widget_promises: Promise<base.WidgetModel>[] = [];
219+
// Extract buffer paths
220+
// Why do we have to do this? Is there another way?
221+
const bufferPaths: any = {};
222+
for (const bufferPath of widgetsInfo[1]) {
223+
if (!bufferPaths[bufferPath[0]]) {
224+
bufferPaths[bufferPath[0]] = [];
225+
}
226+
bufferPaths[bufferPath[0]].push(bufferPath.slice(1));
227+
};
228+
229+
const widgetPromises: Promise<base.WidgetModel>[] = [];
221230

222-
for (const widget_id in widget_states) {
223-
const state = widget_states[widget_id];
231+
for (const widget_id in widgetStates) {
232+
const state = widgetStates[widget_id];
224233

225234
try {
226235
const comm = await this._create_comm('jupyter.widget', widget_id);
227236

237+
// If we have binary buffers
238+
if (widget_id in bufferPaths) {
239+
base.put_buffers(state, bufferPaths[widget_id], buffers);
240+
}
241+
228242
const modelPromise = this.new_model(
229243
{
230244
model_name: state.model_name,
@@ -235,15 +249,15 @@ export class WidgetManager extends JupyterLabManager {
235249
},
236250
state.state
237251
);
238-
widget_promises.push(modelPromise);
252+
widgetPromises.push(modelPromise);
239253
} catch (error) {
240254
// Failed to create a widget model, we continue creating other models so that
241255
// other widgets can render
242256
console.error(error);
243257
}
244258
}
245259

246-
const widgets = await Promise.all(widget_promises);
260+
const widgets = await Promise.all(widgetPromises);
247261
for (const model of widgets) {
248262
models[model.model_id] = model;
249263
}

0 commit comments

Comments
 (0)