fix: recreate `SvelteDate` methods deriveds if they are destroyed

pull/13515/head
paoloricciuti 2 years ago
parent 14ecedf33b
commit 674995518f

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: recreate `SvelteDate` methods deriveds if they are destroyed

@ -1,4 +1,5 @@
/** @import { Source } from '#client' */
import { DESTROYED } from '../internal/client/constants.js';
import { derived } from '../internal/client/index.js';
import { source, set } from '../internal/client/reactivity/sources.js';
import { get } from '../internal/client/runtime.js';

@ -2,6 +2,7 @@ import { render_effect, effect_root } from '../internal/client/reactivity/effect
import { flushSync } from '../index-client.js';
import { SvelteDate } from './date.js';
import { assert, test } from 'vitest';
import { derived, get } from 'svelte/internal/client';
const initial_date = new Date(2023, 0, 2, 0, 0, 0, 0);
const a = new Date(2024, 1, 3, 1, 1, 1, 1);
@ -582,3 +583,30 @@ test('Date.toLocaleString', () => {
test('Date.instanceOf', () => {
assert.equal(new SvelteDate() instanceof Date, true);
});
test('Date methods invoked for the first time in a derived', () => {
const date = new SvelteDate(initial_date);
const log: any = [];
const cleanup = effect_root(() => {
const months = derived(() => {
return date.getMonth();
});
render_effect(() => {
log.push(get(months));
});
flushSync(() => {
date.setMonth(date.getMonth() + 1);
});
flushSync(() => {
date.setMonth(date.getMonth() + 1);
});
});
assert.deepEqual(log, [0, 1, 2]);
cleanup();
});

Loading…
Cancel
Save