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

fix: Actually fix layout serialization #426

Merged
merged 2 commits into from
Jan 31, 2024
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
5 changes: 5 additions & 0 deletions .changeset/kind-geckos-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"anywidget": patch
---

Fix serialization of `layout` trait
22 changes: 3 additions & 19 deletions packages/anywidget/src/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,25 +309,9 @@ export default function ({ DOMWidgetModel, DOMWidgetView }) {
let serialize = serializers[k]?.serialize;
if (serialize) {
state[k] = serialize(state[k], this);
} else if (
(k === "layout" || k === "style") &&
state[k] instanceof Promise
) {
// We can't serialize a Promise, so just skip it.
//
// N.B. This handles a race condition from ipywidgets,
// where an initial promise for a WidgetModel is being
// serialized on initially.
//
// The default behavior of ipywidgets is to use JSON.stringify + JSON.parse,
// "trick", which silently returns an empty object for _all_ Promises.
//
// If we try to use `structuredClone` we'll get an error.
//
// We explicitly handle case (rather than falling back to JSON.stringify)
// so that users will see an error if they accidently try to serialize
// a Promise.
state[k] = undefined;
} else if (k === "layout" || k === "style") {
// These keys come from ipywidgets, rely on JSON.stringify trick.
state[k] = JSON.parse(JSON.stringify(state[k]));
} else {
state[k] = structuredClone(state[k]);
}
Expand Down
Loading