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
pull/13520/head
Paolo Ricciuti 3 months ago committed by GitHub
parent c6af26ffd1
commit 51939e2743
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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';
@ -42,7 +43,7 @@ export class SvelteDate extends Date {
var d = this.#deriveds.get(method);
if (d === undefined) {
if (d === undefined || (d.f & DESTROYED) !== 0) {
d = derived(() => {
get(this.#time);
// @ts-ignore

@ -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