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

Conversation

davidbrochart
Copy link
Member

Fixes #2588

@jasongrout jasongrout added this to the 8.0 milestone Feb 18, 2021
@vidartf
Copy link
Member

vidartf commented Feb 19, 2021

This code should probably be made DRY as well then?

// 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);
}
});
put_buffers(data.state, buffer_paths, buffers);

@@ -100,18 +100,25 @@ export function reject(message: string, log: boolean) {
export function put_buffers(
state: Dict<BufferJSON>,
buffer_paths: (string | number)[][],
buffers: DataView[]
buffers: any[]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any seems overly permissive. AFAICT, this should be a union of:

  • DataView
  • ArrayBuffer
  • a generic type that includes a buffer attribute of the correct type (ArrayBuffer). This would catch TypedArrays.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, we also need to add ArrayBufferView in the union.

@davidbrochart
Copy link
Member Author

This code should probably be made DRY as well then?

// 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);
}
});
put_buffers(data.state, buffer_paths, buffers);

Good catch, and here too:

// 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);
}
});
utils.put_buffers(state, buffer_paths, buffers);

Copy link
Member

@vidartf vidartf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thanks 👍

@jasongrout jasongrout merged commit bcac4af into jupyter-widgets:master Feb 24, 2021
@jasongrout
Copy link
Member

Thanks @davidbrochart, and thanks for the review @vidartf.

@davidbrochart davidbrochart deleted the deserialization branch February 24, 2021 07:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Factor out state deserialization logic
3 participants