Skip to content

Commit

Permalink
fix: deepcopy with structuredClone over JSON.parse/stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenbreddels committed Feb 6, 2023
1 parent d24c9e7 commit f0e1946
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.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 f0e1946

Please sign in to comment.