Skip to content

Commit

Permalink
Merge pull request jupyter-widgets#3697 from maartenbreddels/backport…
Browse files Browse the repository at this point in the history
…_deepcopy

fix: deepcopy with structuredClone over JSON.parse/stringify
  • Loading branch information
jasongrout committed Feb 13, 2023
2 parents f72a992 + 2701b5e commit 5f2e97a
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/base/src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,14 +492,16 @@ class WidgetModel extends Backbone.Model {
* binary array buffers.
*/
serialize(state: {[key: string]: any}) {
const deepcopy =
(globalThis as any).structuredClone || ((x: any) => JSON.parse(JSON.stringify(x)));
const serializers = (this.constructor as typeof WidgetModel).serializers || {};
for (const k of Object.keys(state)) {
try {
if (serializers[k] && serializers[k].serialize) {
state[k] = (serializers[k].serialize)(state[k], this);
} else {
// the default serializer just deep-copies the object
state[k] = JSON.parse(JSON.stringify(state[k]));
state[k] = deepcopy(state[k]);
}
if (state[k] && state[k].toJSON) {
state[k] = state[k].toJSON();
Expand Down

0 comments on commit 5f2e97a

Please sign in to comment.