Skip to content

Commit ab7ea4b

Browse files
authored
Merge pull request #3689 from manzt/patch-1
fix: deepcopy with `structuredClone` over JSON.parse/stringify
2 parents 6547f84 + 5dceb78 commit ab7ea4b

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

packages/base/src/widget.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,8 @@ export class WidgetModel extends Backbone.Model {
524524
* binary array buffers.
525525
*/
526526
serialize(state: Dict<any>): JSONObject {
527+
const deepcopy =
528+
globalThis.structuredClone || ((x: any) => JSON.parse(JSON.stringify(x)));
527529
const serializers =
528530
(this.constructor as typeof WidgetModel).serializers || {};
529531
for (const k of Object.keys(state)) {
@@ -532,7 +534,7 @@ export class WidgetModel extends Backbone.Model {
532534
state[k] = serializers[k].serialize!(state[k], this);
533535
} else {
534536
// the default serializer just deep-copies the object
535-
state[k] = JSON.parse(JSON.stringify(state[k]));
537+
state[k] = deepcopy(state[k]);
536538
}
537539
if (state[k] && state[k].toJSON) {
538540
state[k] = state[k].toJSON();

0 commit comments

Comments
 (0)