Home / File/ this.js — svelte Source File

this.js — svelte Source File

Architecture documentation for this.js, a javascript file in the svelte codebase. 8 imports, 0 dependents.

File javascript ClientRuntime Hydration 8 imports 2 functions

Entity Profile

Dependency Diagram

graph LR
  74650c92_257d_4a26_2826_31c4edae73a3["this.js"]
  1ae6fa4e_16ee_acdf_5e28_17eb0819fddb["effects.js"]
  74650c92_257d_4a26_2826_31c4edae73a3 --> 1ae6fa4e_16ee_acdf_5e28_17eb0819fddb
  a985ae40_8ef8_7ef2_adad_116fbf97e70c["effect"]
  74650c92_257d_4a26_2826_31c4edae73a3 --> a985ae40_8ef8_7ef2_adad_116fbf97e70c
  7494b934_a3b8_689e_91b6_8435e26461c5["render_effect"]
  74650c92_257d_4a26_2826_31c4edae73a3 --> 7494b934_a3b8_689e_91b6_8435e26461c5
  bde4209f_8ffc_1594_4024_b1835a44bcf6["runtime.js"]
  74650c92_257d_4a26_2826_31c4edae73a3 --> bde4209f_8ffc_1594_4024_b1835a44bcf6
  a814b193_e12a_4037_c3c8_dfd45f3bd0bb["untrack"]
  74650c92_257d_4a26_2826_31c4edae73a3 --> a814b193_e12a_4037_c3c8_dfd45f3bd0bb
  8e9a8b22_9754_09a3_8bf1_af0aeb68deca["task.js"]
  74650c92_257d_4a26_2826_31c4edae73a3 --> 8e9a8b22_9754_09a3_8bf1_af0aeb68deca
  2cab0f64_6d19_d981_66e2_d2555c252702["queue_micro_task"]
  74650c92_257d_4a26_2826_31c4edae73a3 --> 2cab0f64_6d19_d981_66e2_d2555c252702
  54c2bfce_50b6_b8cc_4371_e1e14f283fb3["constants"]
  74650c92_257d_4a26_2826_31c4edae73a3 --> 54c2bfce_50b6_b8cc_4371_e1e14f283fb3
  style 74650c92_257d_4a26_2826_31c4edae73a3 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { STATE_SYMBOL } from '#client/constants';
import { effect, render_effect } from '../../../reactivity/effects.js';
import { untrack } from '../../../runtime.js';
import { queue_micro_task } from '../../task.js';

/**
 * @param {any} bound_value
 * @param {Element} element_or_component
 * @returns {boolean}
 */
function is_bound_this(bound_value, element_or_component) {
	return (
		bound_value === element_or_component || bound_value?.[STATE_SYMBOL] === element_or_component
	);
}

/**
 * @param {any} element_or_component
 * @param {(value: unknown, ...parts: unknown[]) => void} update
 * @param {(...parts: unknown[]) => unknown} get_value
 * @param {() => unknown[]} [get_parts] Set if the this binding is used inside an each block,
 * 										returns all the parts of the each block context that are used in the expression
 * @returns {void}
 */
export function bind_this(element_or_component = {}, update, get_value, get_parts) {
	effect(() => {
		/** @type {unknown[]} */
		var old_parts;

		/** @type {unknown[]} */
		var parts;

		render_effect(() => {
			old_parts = parts;
			// We only track changes to the parts, not the value itself to avoid unnecessary reruns.
			parts = get_parts?.() || [];

			untrack(() => {
				if (element_or_component !== get_value(...parts)) {
					update(element_or_component, ...parts);
					// If this is an effect rerun (cause: each block context changes), then nullify the binding at
					// the previous position if it isn't already taken over by a different effect.
					if (old_parts && is_bound_this(get_value(...old_parts), element_or_component)) {
						update(null, ...old_parts);
					}
				}
			});
		});

		return () => {
			// We cannot use effects in the teardown phase, we we use a microtask instead.
			queue_micro_task(() => {
				if (parts && is_bound_this(get_value(...parts), element_or_component)) {
					update(null, ...parts);
				}
			});
		};
	});

	return element_or_component;
}

Domain

Subdomains

Frequently Asked Questions

What does this.js do?
this.js is a source file in the svelte codebase, written in javascript. It belongs to the ClientRuntime domain, Hydration subdomain.
What functions are defined in this.js?
this.js defines 2 function(s): bind_this, is_bound_this.
What does this.js depend on?
this.js imports 8 module(s): constants, effect, effects.js, queue_micro_task, render_effect, runtime.js, task.js, untrack.
Where is this.js in the architecture?
this.js is located at packages/svelte/src/internal/client/dom/elements/bindings/this.js (domain: ClientRuntime, subdomain: Hydration, directory: packages/svelte/src/internal/client/dom/elements/bindings).

Analyze Your Own Codebase

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

Try Supermodel Free