All files / src/internal/client proxy.js

97.4% Statements 300/308
96.93% Branches 95/98
91.66% Functions 11/12
97.33% Lines 292/300

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 3012x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3326x 3326x 1512x 1512x 1814x 1814x 1814x 3326x 52x 52x 1762x 1762x 1762x 1762x 1762x 1762x 1762x 1762x 1762x 1762x 1762x 1762x 1762x 1762x 1762x 14x 14x 14x 14x 14x 1762x 1748x 1748x 1748x 1070x 1070x 1748x 1748x 1762x 1762x 1762x 1762x 3x 3x 3x 3x 2x 3x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 3x     2x 2x 1762x 1762x 1762x 33x 33x 33x 2x 1x 1x 33x 31x 31x 31x 33x 33x 1762x 1762x 1762x 4344x 97x 97x 4247x 4344x 24x 24x 4223x 4223x 4223x 4223x 4223x 4344x 872x 872x 872x 4223x 4344x 3870x 3870x 3870x 353x 353x 1762x 1762x 1762x 227x 227x 227x 206x 206x 226x 21x 21x 21x 21x 5x 5x 5x 5x 5x 5x 5x 21x 222x 222x 1762x 1762x 1762x 290x     290x 290x 106x 106x 184x 184x 290x 290x 290x 290x 16x 290x 179x 11x 11x 11x 179x 179x 179x     179x 184x 184x 1762x 1762x 1762x 1014x 1014x 1014x 1014x 1014x 1014x 1014x 1014x 99x 93x 93x 93x 93x 1014x 915x 915x 915x 1014x 1014x 1014x 1014x 1014x 4x 4x 1014x 1014x 1014x 1014x 1014x 108x 22x 22x 22x 108x 1014x 1014x 1014x 1014x 1014x 6x 6x 1014x 1014x 115x 115x 115x 115x 115x 89x 89x 89x 85x 85x 85x 85x 85x 85x 89x 115x 115x 115x 1014x 1014x 1762x 1762x 1762x 265x 265x 265x 192x 192x 265x 265x 265x 154x 4x 4x 154x 265x 265x 1762x 1762x 1762x     1762x 1762x 2x 2x 2x 2x 2x 146x 146x 146x 2x 2x 2x 2x 2x 1808800x 10x 10x 1808790x 1808790x 1808790x 2x 2x 2x 2x 2x 2x 568x 568x  
/** @import { ProxyMetadata, ProxyStateObject, Source } from '#client' */
import { DEV } from 'esm-env';
import { get, current_component_context, untrack, current_effect } from './runtime.js';
import {
	array_prototype,
	get_descriptor,
	get_prototype_of,
	is_array,
	object_prototype
} from '../shared/utils.js';
import { check_ownership, widen_ownership } from './dev/ownership.js';
import { source, set } from './reactivity/sources.js';
import { STATE_SYMBOL, STATE_SYMBOL_METADATA } from './constants.js';
import { UNINITIALIZED } from '../../constants.js';
import * as e from './errors.js';
 
/**
 * @template T
 * @param {T} value
 * @param {ProxyMetadata | null} [parent]
 * @param {Source<T>} [prev] dev mode only
 * @returns {T}
 */
export function proxy(value, parent = null, prev) {
	// 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;
	}
 
	var sources = new Map();
	var is_proxied_array = is_array(value);
	var version = source(0);
 
	/** @type {ProxyMetadata} */
	var metadata;
 
	if (DEV) {
		metadata = {
			parent,
			owners: null
		};
 
		if (prev) {
			// Reuse owners from previous state; necessary because reassignment is not guaranteed to have correct component context.
			// If no previous proxy exists we play it safe and assume ownerless state
			// @ts-expect-error
			const prev_owners = prev.v?.[STATE_SYMBOL_METADATA]?.owners;
			metadata.owners = prev_owners ? new Set(prev_owners) : null;
		} else {
			metadata.owners =
				parent === null
					? current_component_context !== null
						? new Set([current_component_context.function])
						: null
					: new Set();
		}
	}
 
	return new Proxy(/** @type {any} */ (value), {
		defineProperty(_, prop, descriptor) {
			if (
				!('value' in descriptor) ||
				descriptor.configurable === false ||
				descriptor.enumerable === false ||
				descriptor.writable === false
			) {
				// we disallow non-basic descriptors, because unless they are applied to the
				// target object — which we avoid, so that state can be forked — we will run
				// afoul of the various invariants
				// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/getOwnPropertyDescriptor#invariants
				e.state_descriptors_fixed();
			}
 
			var s = sources.get(prop);
 
			if (s === undefined) {
				s = source(descriptor.value);
				sources.set(prop, s);
			} else {
				set(s, proxy(descriptor.value, metadata));
			}
 
			return true;
		},
 
		deleteProperty(target, prop) {
			var s = sources.get(prop);
 
			if (s === undefined) {
				if (prop in target) {
					sources.set(prop, source(UNINITIALIZED));
				}
			} else {
				set(s, UNINITIALIZED);
				update_version(version);
			}
 
			return true;
		},
 
		get(target, prop, receiver) {
			if (DEV && prop === STATE_SYMBOL_METADATA) {
				return metadata;
			}
 
			if (prop === STATE_SYMBOL) {
				return value;
			}
 
			var s = sources.get(prop);
			var exists = prop in target;
 
			// create a source, but only if it's an own property and not a prototype property
			if (s === undefined && (!exists || get_descriptor(target, prop)?.writable)) {
				s = source(proxy(exists ? target[prop] : UNINITIALIZED, metadata));
				sources.set(prop, s);
			}
 
			if (s !== undefined) {
				var v = get(s);
				return v === UNINITIALIZED ? undefined : v;
			}
 
			return Reflect.get(target, prop, receiver);
		},
 
		getOwnPropertyDescriptor(target, prop) {
			var descriptor = Reflect.getOwnPropertyDescriptor(target, prop);
 
			if (descriptor && 'value' in descriptor) {
				var s = sources.get(prop);
				if (s) descriptor.value = get(s);
			} else if (descriptor === undefined) {
				var source = sources.get(prop);
				var value = source?.v;
 
				if (source !== undefined && value !== UNINITIALIZED) {
					return {
						enumerable: true,
						configurable: true,
						value,
						writable: true
					};
				}
			}
 
			return descriptor;
		},
 
		has(target, prop) {
			if (DEV && prop === STATE_SYMBOL_METADATA) {
				return true;
			}
 
			if (prop === STATE_SYMBOL) {
				return true;
			}
 
			var s = sources.get(prop);
			var has = (s !== undefined && s.v !== UNINITIALIZED) || Reflect.has(target, prop);
 
			if (
				s !== undefined ||
				(current_effect !== null && (!has || get_descriptor(target, prop)?.writable))
			) {
				if (s === undefined) {
					s = source(has ? proxy(target[prop], metadata) : UNINITIALIZED);
					sources.set(prop, s);
				}
 
				var value = get(s);
				if (value === UNINITIALIZED) {
					return false;
				}
			}
 
			return has;
		},
 
		set(target, prop, value, receiver) {
			var s = sources.get(prop);
			var has = prop in target;
 
			// If we haven't yet created a source for this property, we need to ensure
			// we do so otherwise if we read it later, then the write won't be tracked and
			// the heuristics of effects will be different vs if we had read the proxied
			// object property before writing to that property.
			if (s === undefined) {
				if (!has || get_descriptor(target, prop)?.writable) {
					s = source(undefined);
					set(s, proxy(value, metadata));
					sources.set(prop, s);
				}
			} else {
				has = s.v !== UNINITIALIZED;
				set(s, proxy(value, metadata));
			}
 
			if (DEV) {
				/** @type {ProxyMetadata | undefined} */
				var prop_metadata = value?.[STATE_SYMBOL_METADATA];
				if (prop_metadata && prop_metadata?.parent !== metadata) {
					widen_ownership(metadata, prop_metadata);
				}
				check_ownership(metadata);
			}
 
			// variable.length = value -> clear all signals with index >= value
			if (is_proxied_array && prop === 'length') {
				for (var i = value; i < target.length; i += 1) {
					var other_s = sources.get(i + '');
					if (other_s !== undefined) set(other_s, UNINITIALIZED);
				}
			}
 
			var descriptor = Reflect.getOwnPropertyDescriptor(target, prop);
 
			// Set the new value before updating any signals so that any listeners get the new value
			if (descriptor?.set) {
				descriptor.set.call(receiver, value);
			}
 
			if (!has) {
				// If we have mutated an array directly, we might need to
				// signal that length has also changed. Do it before updating metadata
				// to ensure that iterating over the array as a result of a metadata update
				// will not cause the length to be out of sync.
				if (is_proxied_array && typeof prop === 'string') {
					var ls = sources.get('length');
 
					if (ls !== undefined) {
						var n = Number(prop);
 
						if (Number.isInteger(n) && n >= ls.v) {
							set(ls, n + 1);
						}
					}
				}
 
				update_version(version);
			}
 
			return true;
		},
 
		ownKeys(target) {
			get(version);
 
			var own_keys = Reflect.ownKeys(target).filter((key) => {
				var source = sources.get(key);
				return source === undefined || source.v !== UNINITIALIZED;
			});
 
			for (var [key, source] of sources) {
				if (source.v !== UNINITIALIZED && !(key in target)) {
					own_keys.push(key);
				}
			}
 
			return own_keys;
		},
 
		setPrototypeOf() {
			e.state_prototype_fixed();
		}
	});
}
 
/**
 * @param {Source<number>} signal
 * @param {1 | -1} [d]
 */
function update_version(signal, d = 1) {
	set(signal, signal.v + d);
}
 
/**
 * @param {any} value
 */
export function get_proxied_value(value) {
	if (value !== null && typeof value === 'object' && STATE_SYMBOL in value) {
		return value[STATE_SYMBOL];
	}
 
	return value;
}
 
/**
 * @param {any} a
 * @param {any} b
 */
export function is(a, b) {
	return Object.is(get_proxied_value(a), get_proxied_value(b));
}