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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 4x 4x 4x 2x 2x 2x 6x 6x 3x 3x 3x 3x 6x 2x 2x 2x 2x 2x 2x 2x 2x 1x 16x 16x 16x 15x 15x 15x 1x 15x 15x 15x 16x 1x 1x 1x 2x 2x 1x 45x 45x 45x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 1x 6917x 6917x 1x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 135x 135x 33x 135x 102x 102x 17x 17x 102x 102x 135x 62x 8x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 8x 62x 4x 4x 4x 4x 4x 4x 62x 135x 83x 83x 7x 7x 7x 83x 83x 135x 83x 83x 83x 4x 83x 83x 83x 83x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 83x 83x 135x 135x 62x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 62x 44x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 43x 44x 62x 62x 62x 62x 62x 62x 79x 79x 62x 44x 44x 62x 8x 8x 62x 4x 4x 62x 135x 135x 62x 26x 26x 62x 62x 62x 1x 6x 5x 5x 6x 4x 4x 4x 1x 1x 6x 1x 1x 1x 5x 6x 6x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 1x 6736x 6736x 1x 1x | import { ANCHOR_SETTINGS } from './constant.js';
import { getDevTool } from './dev.js';
import { captureStack } from './exception.js';
import { onCleanup } from './lifecycle.js';
import { META_REGISTRY } from './registry.js';
import type {
EffectHandler,
KeyLike,
Linkable,
StateChange,
StateMetadata,
StateObserver,
StateObserverList,
StatePublicTracker,
StateTracker,
StateUnsubscribe,
} from './types.js';
import { closure, isFunction, shortId } from './utils/index.js';
const OBSERVER_SYMBOL = Symbol('state-observer');
const OBSERVER_RESTORER_SYMBOL = Symbol('state-observer-restore');
/**
* Creates a reactive effect that automatically tracks dependencies and re-runs when those dependencies change.
* The effect function will be executed immediately and then again whenever any tracked state changes.
*
* @param fn - The effect function to execute. It receives a StateChange event object containing
* information about what triggered the effect (init, set, delete, etc.) and which keys changed.
* @param displayName - Optional effect name for debugging.
* @returns A cleanup function that can be called to manually dispose of the effect and unsubscribe
* from all tracked dependencies. This is automatically called when the current scope is cleaned up.
*/
export function effect<T>(fn: EffectHandler<T>, displayName?: string): StateUnsubscribe {
let cleanup: StateUnsubscribe | undefined;
const observer = createObserver((event) => {
cleanup?.();
observer.reset();
runEffect(event);
});
observer.name = `Effect(${displayName ?? 'Anonymous'})`;
const runEffect = (event: StateChange) => {
const unobserve = observer.run(() => fn(event));
if (typeof unobserve === 'function') {
cleanup = unobserve as StateUnsubscribe;
} else {
cleanup = undefined;
}
};
const runCleanup = () => {
cleanup?.();
observer.destroy();
};
onCleanup(runCleanup);
runEffect({ type: 'init', keys: [] });
return runCleanup;
}
/**
* Executes a function outside any observer context.
* This function temporarily removes the current observer context,
* executes the provided function, and then restores the previous observer context.
* It's useful for running code that shouldn't be tracked by the reactive system.
*
* @param fn - The function to execute outside of observer context
*/
export function untrack<R>(fn: () => R): R {
const prevObserver = closure.get<StateObserver>(OBSERVER_SYMBOL);
closure.set(OBSERVER_SYMBOL, undefined as never);
if (typeof fn === 'function') {
try {
return fn();
} catch (error) {
captureStack.error.external('Unable to execute the outside of observer function', error as Error, untrack);
} finally {
closure.set(OBSERVER_SYMBOL, prevObserver);
}
} else {
const error = new Error('Invalid argument.');
captureStack.error.argument('The given argument is not a function', error, untrack);
}
return undefined as R;
}
/**
* @deprecated This function is deprecated.
* Sets the current observer context for state tracking.
* This function is used internally to manage the observer stack during state derivation.
*
* @param observer - The observer to set as the current context
* @returns A cleanup function that restores the previous observer context
* @warning This is a low-level API designed for library authors or advanced use cases.
*/
export function setObserver(observer: StateObserver) {
const currentObserver = closure.get<StateObserver>(OBSERVER_SYMBOL);
const currentRestorer = closure.get<() => void>(OBSERVER_RESTORER_SYMBOL);
// Make sure it handles duplicate observer such as when evaluated in React's strict mode.
if (currentObserver === observer) return currentRestorer as () => void;
let restored = false;
const nextRestore = () => {
if (!restored) {
closure.set(OBSERVER_SYMBOL, currentObserver);
closure.set(OBSERVER_RESTORER_SYMBOL, currentRestorer);
restored = true;
}
};
closure.set(OBSERVER_SYMBOL, observer);
closure.set(OBSERVER_RESTORER_SYMBOL, nextRestore);
return nextRestore;
}
/**
* Gets the current observer context.
*
* @returns The current observer or undefined if none is set
* @warning This is a low-level API designed for library authors or advanced use cases.
*/
export function getObserver(): StateObserver | undefined {
return closure.get(OBSERVER_SYMBOL);
}
/**
* Creates a new observer instance for tracking state changes.
* An observer manages subscriptions and provides lifecycle hooks for state tracking.
*
* @param onChange - Callback function that will be called when state changes occur
* @param onTrack - Callback function that will be called when a new state is tracked
* @param controlled - A flag indicating whether the observer is controlled by the user
* @returns A new observer instance with states management, onChange handler, onDestroy hook, and cleanup functionality
* @warning This is a low-level API designed for library authors or advanced use cases.
*/
export function createObserver(
onChange: (event: StateChange) => void,
onTrack?: (state: Linkable, key: KeyLike) => void,
controlled?: boolean
): StateObserver {
let observedSize = 0;
let isObserving = false;
let isDestroyed = false;
const states = new WeakMap();
const cleaners = new Set<() => void>();
const resetters = new Set<() => void>();
const track = ((state, key) => {
const keys = states.get(state) as Set<KeyLike>;
if (keys.has(key)) {
return true;
} else {
keys.add(key);
if (isFunction(onTrack)) {
onTrack(state, key);
}
}
return false;
}) satisfies StateTracker;
const destroy = () => {
if (isDestroyed) return;
const currentCleaners = Array.from(cleaners);
for (const clear of currentCleaners) {
if (typeof clear === 'function') {
clear();
}
}
observedSize = 0;
cleaners.clear();
resetters.clear();
isDestroyed = true;
};
const reset = () => {
resetters.forEach((reset) => {
if (typeof reset === 'function') {
reset();
}
});
};
const assign = ((init, observers) => {
if (!observers.has(observer)) {
observers.add(observer);
cleaners.add(() => {
states.delete(init);
observers.delete(observer);
getDevTool()?.onUntrack?.(META_REGISTRY.get(init) as StateMetadata, observer);
});
}
if (!states.has(init)) {
const keys = new Set();
states.set(init, keys);
resetters.add(() => {
keys.clear();
});
if (ANCHOR_SETTINGS.safeObservation) {
observedSize += 1;
if (observedSize > ANCHOR_SETTINGS.safeObservationThreshold) {
const error = new Error('Observation limit exceeded.');
captureStack.violation.general(
'Unsafe observation detected:',
`Attempted to observe too many (${observedSize}) states within a single observer.`,
error,
[
`We always recommend keeping observations small.`,
`- It's likely you are trying to perform an extensive read operation such as JSON.stringify during the observation phase.`,
`- Use the optimized reader utility such as "anchor.read" to perform an extensive operation while maintain immutability.`,
],
assign
);
}
}
}
return (key) => track(init, key);
}) satisfies StateObserver['assign'];
const run = <R>(fn: () => R): R => {
isObserving = true;
isDestroyed = false;
const prevObserver = closure.get<StateObserver>(OBSERVER_SYMBOL);
closure.set(OBSERVER_SYMBOL, observer);
try {
return fn();
} finally {
closure.set(OBSERVER_SYMBOL, prevObserver);
isObserving = false;
}
};
const propagate = (event: StateChange) => {
if (isObserving) {
const error = new Error('Circular mutation.');
captureStack.violation.general(
'Circular mutation detected:',
`Attempted to mutate a state while observing the state itself.`,
error,
[
'Circular state mutation is highly discouraged as it can lead to infinite loops and unpredictable behavior.',
'- This happens when you mutate a reactive property inside a function that’s tracking that same property.',
'- To prevent this, avoid mutating properties that you depend on inside an observer, or use the "untrack" utility to mark the read as non-reactive.',
],
propagate
);
return;
}
onChange(event);
};
if (!controlled) {
onCleanup(destroy);
}
const observer = {
id: shortId(),
get states() {
return states;
},
get onChange() {
return propagate;
},
get destroy() {
return destroy;
},
get reset() {
return reset;
},
get assign() {
return assign;
},
get run() {
return run;
},
};
return observer;
}
/**
* Executes a function within a specific observer context.
* This function temporarily sets the provided observer as the current context,
* executes the provided function, and then restores the previous observer context.
* It's useful for running code that should be tracked by a specific observer.
*
* @template R - The type of the return value of the function.
* @param {() => R} fn - The function to execute within the observer context
* @param {StateObserver} observer - The observer to set as the current context
* @warning This is a low-level API designed for library authors or advanced use cases.
*/
export function withinObserver<R>(fn: () => R, observer: StateObserver): R;
export function withinObserver<R>(observer: StateObserver, fn: () => R): R;
export function withinObserver<R>(observerOrFn: StateObserver | (() => R), fnOrObserver: (() => R) | StateObserver): R {
if (isFunction(observerOrFn)) return withinObserver(fnOrObserver as StateObserver, observerOrFn);
const restore = setObserver(observerOrFn);
let result: R | undefined = undefined;
if (typeof fnOrObserver === 'function') {
try {
result = fnOrObserver();
} catch (error) {
captureStack.error.external('Unable to execute the within observer function', error as Error, withinObserver);
}
} else {
const error = new Error('Invalid argument.');
captureStack.error.argument('The given argument is not a function', error, withinObserver);
}
restore?.();
return result as R;
}
/**
* @deprecated Use {@link untrack} instead
*/
export const outsideObserver = untrack;
const TRACKER_SYMBOL = Symbol('state-tracker');
const TRACKER_RESTORE_SYMBOL = Symbol('state-tracker-restore');
/**
* Sets the current tracker function for state observation.
* This function manages the tracker stack, allowing for nested tracker contexts.
* If the same tracker is already set, it returns the existing restore function.
*
* A tracker is meant for library author to implement global tracking, which
* then they control how they track the state.
*
* @param tracker - The tracker function to set as current
* @returns A restore function that reverts to the previous tracker when called
* @warning This is a low-level API designed for library authors or advanced use cases.
*/
export function setTracker(tracker: StatePublicTracker) {
const currentTracker = closure.get<StatePublicTracker>(TRACKER_SYMBOL);
const currentTrackerRestore = closure.get<() => void>(TRACKER_RESTORE_SYMBOL);
if (currentTracker === tracker) return currentTrackerRestore;
let restored = false;
const nextRestore = () => {
if (!restored) {
closure.set(TRACKER_SYMBOL, currentTracker);
closure.set(TRACKER_RESTORE_SYMBOL, currentTrackerRestore);
restored = true;
}
};
closure.set(TRACKER_SYMBOL, tracker);
closure.set(TRACKER_RESTORE_SYMBOL, nextRestore);
return nextRestore;
}
/**
* Gets the current tracker function for state observation.
* This function returns the currently active tracker, which is used to monitor
* state changes and dependencies during reactive computations.
*
* @returns The current tracker function or undefined if no tracker is set
* @warning This is a low-level API designed for library authors or advanced use cases.
*/
export function getTracker(): StatePublicTracker | undefined {
return closure.get<StatePublicTracker>(TRACKER_SYMBOL);
}
/**
* Tracks a state change by invoking the current tracker function if one is set.
* This function is used internally by the reactive system to notify observers
* about state changes and their dependencies.
*
* @param init - The initial state value that is being tracked
* @param observers - A collection of observers that are watching this state
* @param key - The key or property identifier for the state change
* @warning This is a low-level API designed for library authors or advanced use cases.
*/
export function track(init: Linkable, observers: StateObserverList, key: KeyLike) {
const currentTracker = closure.get<StatePublicTracker>(TRACKER_SYMBOL);
if (typeof currentTracker !== 'function') return;
currentTracker(init, observers, key);
}
|