Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
martinRenou committed Sep 13, 2023
1 parent a2147e2 commit 2fa9dca
Showing 1 changed file with 3 additions and 17 deletions.
20 changes: 3 additions & 17 deletions packages/base/src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ import {
*/
const IPY_MODEL_ = 'IPY_MODEL_';

/**
* A best-effort method for performing deep copies.
*/
const deepcopyJSON = (x: JSONValue) => JSON.parse(JSON.stringify(x));

const deepcopy = (globalThis as any).structuredClone || deepcopyJSON;

/**
* Replace model ids with models recursively.
*/
Expand Down Expand Up @@ -538,18 +531,11 @@ class WidgetModel extends Backbone.Model {
const serializers = (this.constructor as typeof WidgetModel).serializers || {};
for (const k of Object.keys(state)) {
try {
const keySerializers : any = serializers[k] || null;
let serialize = keySerializers?.serialize || null;
if (serialize == null && keySerializers?.deserialize === unpack_models) {
// handle https://github.com/jupyter-widgets/ipywidgets/issues/3735
serialize = deepcopyJSON;
}

if (serialize) {
state[k] = serialize(state[k], this);
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] = deepcopy(state[k]);
state[k] = JSON.parse(JSON.stringify(state[k]));
}

if (state[k] && state[k].toJSON) {
Expand Down

0 comments on commit 2fa9dca

Please sign in to comment.