fix: ensure $state.snapshot correctly clones Date objects (#12564)

pull/12568/head
Dominic Gannaway 4 months ago committed by GitHub
parent 7b2279d84c
commit d73c5b8434
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: ensure $state.snapshot correctly clones Date objects

@ -79,6 +79,10 @@ function clone(value, cloned, path, paths) {
return copy;
}
if (value instanceof Date) {
return /** @type {Snapshot<T>} */ (structuredClone(value));
}
if (typeof (/** @type {T & { toJSON?: any } } */ (value).toJSON) === 'function') {
return clone(
/** @type {T & { toJSON(): any } } */ (value).toJSON(),

@ -0,0 +1,5 @@
import { test } from '../../test';
export default test({
html: `true\ntrue\ntrue\ntrue`
});

@ -0,0 +1,16 @@
<script>
let test = $state({
a: new Date()
});
let test2 = $state.snapshot(test);
let test3 = {
a: new Date()
}
let test4 = structuredClone(test3);
</script>
{test.a instanceof Date}
{test2.a instanceof Date}
{test3.a instanceof Date}
{test4.a instanceof Date}
Loading…
Cancel
Save