Home / Class/ Svelte4Component Class — svelte Architecture

Svelte4Component Class — svelte Architecture

Architecture documentation for the Svelte4Component class in legacy-client.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  4a6dd8eb_d702_b5bf_74e8_1b0f596492ee["Svelte4Component"]
  9478150a_17fe_efce_10bd_3938ed8558e2["legacy-client.js"]
  4a6dd8eb_d702_b5bf_74e8_1b0f596492ee -->|defined in| 9478150a_17fe_efce_10bd_3938ed8558e2
  b72b8566_6fe5_6e9f_7848_f28f37253eff["constructor()"]
  4a6dd8eb_d702_b5bf_74e8_1b0f596492ee -->|method| b72b8566_6fe5_6e9f_7848_f28f37253eff
  305392aa_5654_443f_1b37_389a8aee5428["$set()"]
  4a6dd8eb_d702_b5bf_74e8_1b0f596492ee -->|method| 305392aa_5654_443f_1b37_389a8aee5428
  886e4199_1a2e_3170_a9a6_cd048c976a0d["$on()"]
  4a6dd8eb_d702_b5bf_74e8_1b0f596492ee -->|method| 886e4199_1a2e_3170_a9a6_cd048c976a0d
  3d5f4e6a_7b79_bd7d_d361_8c8532b25d35["$destroy()"]
  4a6dd8eb_d702_b5bf_74e8_1b0f596492ee -->|method| 3d5f4e6a_7b79_bd7d_d361_8c8532b25d35

Relationship Graph

Source Code

packages/svelte/src/legacy/legacy-client.js lines 68–180

class Svelte4Component {
	/** @type {any} */
	#events;

	/** @type {Record<string, any>} */
	#instance;

	/**
	 * @param {ComponentConstructorOptions & {
	 *  component: any;
	 * }} options
	 */
	constructor(options) {
		var sources = new Map();

		/**
		 * @param {string | symbol} key
		 * @param {unknown} value
		 */
		var add_source = (key, value) => {
			var s = mutable_source(value, false, false);
			sources.set(key, s);
			return s;
		};

		// Replicate coarse-grained props through a proxy that has a version source for
		// each property, which is incremented on updates to the property itself. Do not
		// use our $state proxy because that one has fine-grained reactivity.
		const props = new Proxy(
			{ ...(options.props || {}), $$events: {} },
			{
				get(target, prop) {
					return get(sources.get(prop) ?? add_source(prop, Reflect.get(target, prop)));
				},
				has(target, prop) {
					// Necessary to not throw "invalid binding" validation errors on the component side
					if (prop === LEGACY_PROPS) return true;

					get(sources.get(prop) ?? add_source(prop, Reflect.get(target, prop)));
					return Reflect.has(target, prop);
				},
				set(target, prop, value) {
					set(sources.get(prop) ?? add_source(prop, value), value);
					return Reflect.set(target, prop, value);
				}
			}
		);

		this.#instance = (options.hydrate ? hydrate : mount)(options.component, {
			target: options.target,
			anchor: options.anchor,
			props,
			context: options.context,
			intro: options.intro ?? false,
			recover: options.recover
		});

		// We don't flushSync for custom element wrappers or if the user doesn't want it,
		// or if we're in async mode since `flushSync()` will fail
		if (!async_mode_flag && (!options?.props?.$$host || options.sync === false)) {
			flushSync();
		}

		this.#events = props.$$events;

		for (const key of Object.keys(this.#instance)) {
			if (key === '$set' || key === '$destroy' || key === '$on') continue;
			define_property(this, key, {
				get() {
					return this.#instance[key];
				},
				/** @param {any} value */
				set(value) {
					this.#instance[key] = value;
				},
				enumerable: true
			});
		}

		this.#instance.$set = /** @param {Record<string, any>} next */ (next) => {
			Object.assign(props, next);

Frequently Asked Questions

What is the Svelte4Component class?
Svelte4Component is a class in the svelte codebase, defined in packages/svelte/src/legacy/legacy-client.js.
Where is Svelte4Component defined?
Svelte4Component is defined in packages/svelte/src/legacy/legacy-client.js at line 68.

Analyze Your Own Codebase

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

Try Supermodel Free