Skip to content

Commit 4e73335

Browse files
bourdaisjfinetjul
authored andcommitted
fix(getstate): serialize typed arrays as array
the behavior of JSON.stringify for typed arrays is to serialize them as objects with numeric keys. This cause crashes when re-building the object since it would try to fit an object where a typed array is required. This changes makes it so getState replaces typed arrays with plain javascript arrays.
1 parent 315045a commit 4e73335

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

Sources/macros.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ function safeArrays(model) {
174174
});
175175
}
176176

177+
function isTypedArray(value) {
178+
return Object.values(TYPED_ARRAYS).some((ctor) => value instanceof ctor);
179+
}
180+
177181
// ----------------------------------------------------------------------------
178182
// shallow equals
179183
// ----------------------------------------------------------------------------
@@ -378,6 +382,8 @@ export function obj(publicAPI = {}, model = {}) {
378382
jsonArchive[keyName] = jsonArchive[keyName].getState();
379383
} else if (Array.isArray(jsonArchive[keyName])) {
380384
jsonArchive[keyName] = jsonArchive[keyName].map(getStateArrayMapFunc);
385+
} else if (isTypedArray(jsonArchive[keyName])) {
386+
jsonArchive[keyName] = Array.from(jsonArchive[keyName]);
381387
}
382388
});
383389

0 commit comments

Comments
 (0)