Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure buffer is a DataView #3127

Merged
merged 2 commits into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions jupyterlab_widgets/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down
9 changes: 1 addition & 8 deletions packages/base-manager/src/manager-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down
20 changes: 16 additions & 4 deletions packages/base/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,30 @@ export function reject(message: string, log: boolean) {
export function put_buffers(
state: Dict<BufferJSON>,
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;
}
}

Expand Down
10 changes: 1 addition & 9 deletions packages/base/src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down