Home / Class/ SvelteDate Class — svelte Architecture

SvelteDate Class — svelte Architecture

Architecture documentation for the SvelteDate class in date.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  ead2ff3c_1f9f_37b6_47f8_3d9514f78e14["SvelteDate"]
  7c09b4ab_171c_0277_b8c0_61940f33ff53["date.js"]
  ead2ff3c_1f9f_37b6_47f8_3d9514f78e14 -->|defined in| 7c09b4ab_171c_0277_b8c0_61940f33ff53
  64359ac7_3fd3_7d7d_f176_c60a2f9b9112["constructor()"]
  ead2ff3c_1f9f_37b6_47f8_3d9514f78e14 -->|method| 64359ac7_3fd3_7d7d_f176_c60a2f9b9112
  cb2c7213_663e_b384_6fd0_98cacca887fb["inited()"]
  ead2ff3c_1f9f_37b6_47f8_3d9514f78e14 -->|method| cb2c7213_663e_b384_6fd0_98cacca887fb

Relationship Graph

Source Code

packages/svelte/src/reactivity/date.js lines 42–118

export class SvelteDate extends Date {
	#time = state(super.getTime());

	/** @type {Map<keyof Date, Source<unknown>>} */
	#deriveds = new Map();

	#reaction = active_reaction;

	/** @param {any[]} params */
	constructor(...params) {
		// @ts-ignore
		super(...params);

		if (DEV) {
			tag(this.#time, 'SvelteDate.#time');
		}

		if (!inited) this.#init();
	}

	#init() {
		inited = true;

		var proto = SvelteDate.prototype;
		var date_proto = Date.prototype;

		var methods = /** @type {Array<keyof Date & string>} */ (
			Object.getOwnPropertyNames(date_proto)
		);

		for (const method of methods) {
			if (method.startsWith('get') || method.startsWith('to') || method === 'valueOf') {
				// @ts-ignore
				proto[method] = function (...args) {
					// don't memoize if there are arguments
					// @ts-ignore
					if (args.length > 0) {
						get(this.#time);
						// @ts-ignore
						return date_proto[method].apply(this, args);
					}

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

					if (d === undefined) {
						// lazily create the derived, but as though it were being
						// created at the same time as the class instance
						const reaction = active_reaction;
						set_active_reaction(this.#reaction);

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

						this.#deriveds.set(method, d);

						set_active_reaction(reaction);
					}

					return get(d);
				};
			}

			if (method.startsWith('set')) {
				// @ts-ignore
				proto[method] = function (...args) {
					// @ts-ignore
					var result = date_proto[method].apply(this, args);
					set(this.#time, date_proto.getTime.call(this));
					return result;
				};
			}
		}
	}
}

Frequently Asked Questions

What is the SvelteDate class?
SvelteDate is a class in the svelte codebase, defined in packages/svelte/src/reactivity/date.js.
Where is SvelteDate defined?
SvelteDate is defined in packages/svelte/src/reactivity/date.js at line 42.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free