Skip to content

Commit 51939e2

Browse files
fix: recreate SvelteDate methods deriveds if they are destroyed (#13515)
* fix: recreate `SvelteDate` methods deriveds if they are destroyed * fix: duh, forgot to add the code back after testing the test was failing before
1 parent c6af26f commit 51939e2

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

.changeset/ten-laws-grow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: recreate `SvelteDate` methods deriveds if they are destroyed

packages/svelte/src/reactivity/date.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/** @import { Source } from '#client' */
2+
import { DESTROYED } from '../internal/client/constants.js';
23
import { derived } from '../internal/client/index.js';
34
import { source, set } from '../internal/client/reactivity/sources.js';
45
import { get } from '../internal/client/runtime.js';
@@ -42,7 +43,7 @@ export class SvelteDate extends Date {
4243

4344
var d = this.#deriveds.get(method);
4445

45-
if (d === undefined) {
46+
if (d === undefined || (d.f & DESTROYED) !== 0) {
4647
d = derived(() => {
4748
get(this.#time);
4849
// @ts-ignore

packages/svelte/src/reactivity/date.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { render_effect, effect_root } from '../internal/client/reactivity/effect
22
import { flushSync } from '../index-client.js';
33
import { SvelteDate } from './date.js';
44
import { assert, test } from 'vitest';
5+
import { derived, get } from 'svelte/internal/client';
56

67
const initial_date = new Date(2023, 0, 2, 0, 0, 0, 0);
78
const a = new Date(2024, 1, 3, 1, 1, 1, 1);
@@ -582,3 +583,30 @@ test('Date.toLocaleString', () => {
582583
test('Date.instanceOf', () => {
583584
assert.equal(new SvelteDate() instanceof Date, true);
584585
});
586+
587+
test('Date methods invoked for the first time in a derived', () => {
588+
const date = new SvelteDate(initial_date);
589+
const log: any = [];
590+
591+
const cleanup = effect_root(() => {
592+
const months = derived(() => {
593+
return date.getMonth();
594+
});
595+
596+
render_effect(() => {
597+
log.push(get(months));
598+
});
599+
600+
flushSync(() => {
601+
date.setMonth(date.getMonth() + 1);
602+
});
603+
604+
flushSync(() => {
605+
date.setMonth(date.getMonth() + 1);
606+
});
607+
});
608+
609+
assert.deepEqual(log, [0, 1, 2]);
610+
611+
cleanup();
612+
});

0 commit comments

Comments
 (0)