diff --git a/jupyterlab_widgets/src/manager.ts b/jupyterlab_widgets/src/manager.ts index b2ea06247e..12050aac3e 100644 --- a/jupyterlab_widgets/src/manager.ts +++ b/jupyterlab_widgets/src/manager.ts @@ -133,14 +133,7 @@ export abstract class LabWidgetManager extends ManagerBase ) { const data = msg.content.data as any; const buffer_paths = data.buffer_paths || []; - // Make sure the buffers are DataViews - const buffers = (msg.buffers || []).map(b => { - if (b instanceof DataView) { - return b; - } else { - return new DataView(b instanceof ArrayBuffer ? b : b.buffer); - } - }); + const buffers = msg.buffers || []; put_buffers(data.state, buffer_paths, buffers); info.resolve({ comm, msg }); } diff --git a/packages/base-manager/src/manager-base.ts b/packages/base-manager/src/manager-base.ts index 7c429b601e..d92a1c1b1b 100644 --- a/packages/base-manager/src/manager-base.ts +++ b/packages/base-manager/src/manager-base.ts @@ -157,14 +157,7 @@ export abstract class ManagerBase implements IWidgetManager { } const data = (msg.content.data as unknown) as ISerializedState; const buffer_paths = data.buffer_paths || []; - // Make sure the buffers are DataViews - const buffers = (msg.buffers || []).map(b => { - if (b instanceof DataView) { - return b; - } else { - return new DataView(b instanceof ArrayBuffer ? b : b.buffer); - } - }); + const buffers = msg.buffers || []; put_buffers(data.state, buffer_paths, buffers); return this.new_model( { diff --git a/packages/base/src/utils.ts b/packages/base/src/utils.ts index 0c498458a6..e556965b60 100644 --- a/packages/base/src/utils.ts +++ b/packages/base/src/utils.ts @@ -100,18 +100,30 @@ export function reject(message: string, log: boolean) { export function put_buffers( state: Dict, buffer_paths: (string | number)[][], - buffers: DataView[] + buffers: ( + | DataView + | ArrayBuffer + | ArrayBufferView + | { buffer: ArrayBuffer } + )[] ): void { for (let i = 0; i < buffer_paths.length; i++) { const buffer_path = buffer_paths[i]; - // say we want to set state[x][y][z] = buffers[i] + // make sure the buffers are DataViews + let buffer = buffers[i]; + if (!(buffer instanceof DataView)) { + buffer = new DataView( + buffer instanceof ArrayBuffer ? buffer : buffer.buffer + ); + } + // say we want to set state[x][y][z] = buffer let obj = state as any; // we first get obj = state[x][y] for (let j = 0; j < buffer_path.length - 1; j++) { obj = obj[buffer_path[j]]; } - // and then set: obj[z] = buffers[i] - obj[buffer_path[buffer_path.length - 1]] = buffers[i]; + // and then set: obj[z] = buffer + obj[buffer_path[buffer_path.length - 1]] = buffer; } } diff --git a/packages/base/src/widget.ts b/packages/base/src/widget.ts index 45d39cc4e0..5f46865bd3 100644 --- a/packages/base/src/widget.ts +++ b/packages/base/src/widget.ts @@ -216,15 +216,7 @@ export class WidgetModel extends Backbone.Model { .then(() => { const state = data.state; const buffer_paths = data.buffer_paths || []; - // Make sure the buffers are DataViews - const buffers = (msg.buffers || []).map(b => { - if (b instanceof DataView) { - return b; - } else { - return new DataView(b instanceof ArrayBuffer ? b : b.buffer); - } - }); - + const buffers = msg.buffers || []; utils.put_buffers(state, buffer_paths, buffers); return (this.constructor as typeof WidgetModel)._deserialize_state( state,