Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improved fine-grainability of ReactiveDate #12110

Merged
merged 22 commits into from
Jul 3, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tweaks
  • Loading branch information
Rich-Harris committed Jun 29, 2024
commit 36f9cdbedfbcb9479762e96232eb038d8b2de4c2
29 changes: 14 additions & 15 deletions packages/svelte/src/reactivity/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { source, set } from '../internal/client/reactivity/sources.js';
import { get } from '../internal/client/runtime.js';

var inited = false;

export class ReactiveDate extends Date {
#raw_time = source(super.getTime());
#time = source(super.getTime());

/** @type {Map<keyof Date, Source<unknown>>} */
#deriveds = new Map();
Expand All @@ -18,11 +19,9 @@ export class ReactiveDate extends Date {
}

#init() {
if (inited) {
return;
}

if (inited) return;
inited = true;

var reactive_date_proto = ReactiveDate.prototype;
var date_proto = Date.prototype;

Expand All @@ -39,16 +38,19 @@ export class ReactiveDate extends Date {
for (const method of read_props) {
// @ts-ignore
reactive_date_proto[method] = function (...args) {
var sig = this.#deriveds.get(method);
if (!sig) {
sig = derived(() => {
get(this.#raw_time);
var d = this.#deriveds.get(method);

if (!d) {
d = derived(() => {
get(this.#time);
// @ts-ignore
return date_proto[method].apply(this, args);
});
this.#deriveds.set(method, sig);

this.#deriveds.set(method, d);
}
return get(sig);

return get(d);
};
}

Expand All @@ -57,10 +59,7 @@ export class ReactiveDate extends Date {
reactive_date_proto[method] = function (...args) {
// @ts-ignore
var result = date_proto[method].apply(this, args);
var new_time = date_proto.getTime.call(this);
if (this.#raw_time.v !== new_time) {
set(this.#raw_time, new_time);
}
set(this.#time, date_proto.getTime.call(this));
return result;
};
}
Expand Down