Home / Function/ proxy() — svelte Function Reference

proxy() — svelte Function Reference

Architecture documentation for the proxy() function in proxy.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  c55b2607_d45b_c327_8826_7bdf245d80f6["proxy()"]
  71020d3b_ab64_9fea_2a06_dab93412f92f["proxy.js"]
  c55b2607_d45b_c327_8826_7bdf245d80f6 -->|defined in| 71020d3b_ab64_9fea_2a06_dab93412f92f
  665ade04_d03d_9b82_ca8d_aba0158bc3b7["prop()"]
  665ade04_d03d_9b82_ca8d_aba0158bc3b7 -->|calls| c55b2607_d45b_c327_8826_7bdf245d80f6
  63ee8247_ada4_9f1d_e139_0c1167cd5b1c["set()"]
  63ee8247_ada4_9f1d_e139_0c1167cd5b1c -->|calls| c55b2607_d45b_c327_8826_7bdf245d80f6
  1e2f7428_6050_5cb7_69db_bf5db719f6d1["source()"]
  c55b2607_d45b_c327_8826_7bdf245d80f6 -->|calls| 1e2f7428_6050_5cb7_69db_bf5db719f6d1
  cc46feba_170d_5970_a6be_f512f15aa0ee["get_error()"]
  c55b2607_d45b_c327_8826_7bdf245d80f6 -->|calls| cc46feba_170d_5970_a6be_f512f15aa0ee
  311ef9f4_9b68_c178_c1db_3b8696f7d964["set_active_reaction()"]
  c55b2607_d45b_c327_8826_7bdf245d80f6 -->|calls| 311ef9f4_9b68_c178_c1db_3b8696f7d964
  f144db4b_3212_e33b_8d55_9b81f394fdce["set_update_version()"]
  c55b2607_d45b_c327_8826_7bdf245d80f6 -->|calls| f144db4b_3212_e33b_8d55_9b81f394fdce
  63ee8247_ada4_9f1d_e139_0c1167cd5b1c["set()"]
  c55b2607_d45b_c327_8826_7bdf245d80f6 -->|calls| 63ee8247_ada4_9f1d_e139_0c1167cd5b1c
  7a03d99a_97a2_84ec_f817_160480e2c5b8["inspectable_array()"]
  c55b2607_d45b_c327_8826_7bdf245d80f6 -->|calls| 7a03d99a_97a2_84ec_f817_160480e2c5b8
  62511000_fb03_0d9c_03d6_7b13cd37ac90["trace()"]
  c55b2607_d45b_c327_8826_7bdf245d80f6 -->|calls| 62511000_fb03_0d9c_03d6_7b13cd37ac90
  4dfcf957_8573_ff55_bd31_4181227109e3["tag()"]
  c55b2607_d45b_c327_8826_7bdf245d80f6 -->|calls| 4dfcf957_8573_ff55_bd31_4181227109e3
  12797ddd_ee65_4450_93da_cd2e45a50827["get_label()"]
  c55b2607_d45b_c327_8826_7bdf245d80f6 -->|calls| 12797ddd_ee65_4450_93da_cd2e45a50827
  3086f82b_cdf1_11bf_2154_ee5083b82091["state_descriptors_fixed()"]
  c55b2607_d45b_c327_8826_7bdf245d80f6 -->|calls| 3086f82b_cdf1_11bf_2154_ee5083b82091
  a08b6cc5_af73_1be4_d02f_3113cf8a8305["get()"]
  c55b2607_d45b_c327_8826_7bdf245d80f6 -->|calls| a08b6cc5_af73_1be4_d02f_3113cf8a8305
  7602fc38_fab9_8955_83f0_8643d7c6c7a4["increment()"]
  c55b2607_d45b_c327_8826_7bdf245d80f6 -->|calls| 7602fc38_fab9_8955_83f0_8643d7c6c7a4
  style c55b2607_d45b_c327_8826_7bdf245d80f6 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/internal/client/proxy.js lines 40–354

export function proxy(value) {
	// if non-proxyable, or is already a proxy, return `value`
	if (typeof value !== 'object' || value === null || STATE_SYMBOL in value) {
		return value;
	}

	const prototype = get_prototype_of(value);

	if (prototype !== object_prototype && prototype !== array_prototype) {
		return value;
	}

	/** @type {Map<any, Source<any>>} */
	var sources = new Map();
	var is_proxied_array = is_array(value);
	var version = source(0);

	var stack = DEV && tracing_mode_flag ? get_error('created at') : null;
	var parent_version = update_version;

	/**
	 * Executes the proxy in the context of the reaction it was originally created in, if any
	 * @template T
	 * @param {() => T} fn
	 */
	var with_parent = (fn) => {
		if (update_version === parent_version) {
			return fn();
		}

		// child source is being created after the initial proxy —
		// prevent it from being associated with the current reaction
		var reaction = active_reaction;
		var version = update_version;

		set_active_reaction(null);
		set_update_version(parent_version);

		var result = fn();

		set_active_reaction(reaction);
		set_update_version(version);

		return result;
	};

	if (is_proxied_array) {
		// We need to create the length source eagerly to ensure that
		// mutations to the array are properly synced with our proxy
		sources.set('length', source(/** @type {any[]} */ (value).length, stack));
		if (DEV) {
			value = /** @type {any} */ (inspectable_array(/** @type {any[]} */ (value)));
		}
	}

	/** Used in dev for $inspect.trace() */
	var path = '';
	let updating = false;
	/** @param {string} new_path */
	function update_path(new_path) {
		if (updating) return;
		updating = true;
		path = new_path;

		tag(version, `${path} version`);

		// rename all child sources and child proxies
		for (const [prop, source] of sources) {
			tag(source, get_label(path, prop));
		}
		updating = false;
	}

	return new Proxy(/** @type {any} */ (value), {
		defineProperty(_, prop, descriptor) {
			if (
				!('value' in descriptor) ||
				descriptor.configurable === false ||
				descriptor.enumerable === false ||
				descriptor.writable === false
			) {

Domain

Subdomains

Called By

Frequently Asked Questions

What does proxy() do?
proxy() is a function in the svelte codebase, defined in packages/svelte/src/internal/client/proxy.js.
Where is proxy() defined?
proxy() is defined in packages/svelte/src/internal/client/proxy.js at line 40.
What does proxy() call?
proxy() calls 13 function(s): get, get_error, get_label, increment, inspectable_array, set, set_active_reaction, set_update_version, and 5 more.
What calls proxy()?
proxy() is called by 2 function(s): prop, set.

Analyze Your Own Codebase

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

Try Supermodel Free