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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 60x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 1x 6954x 6954x 1x 49x 49x 49x 49x 49x 49x 49x 115x 115x 26x 115x 89x 89x 17x 17x 89x 89x 115x 49x 4x 4x 4x 4x 4x 4x 4x 49x 115x 74x 74x 4x 4x 4x 74x 74x 115x 74x 74x 74x 74x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 74x 74x 115x 115x 49x 7x 7x 49x 49x 49x 72x 72x 49x 37x 37x 49x 4x 4x 49x 115x 115x 49x 7x 7x 49x 49x 49x 1x 20x 12x 12x 15x 11x 11x 11x 1x 1x 20x 1x 1x 1x 12x 20x 20x 1x 8x 8x 8x 7x 7x 7x 1x 1x 1x 1x 1x 1x 8x 1x 1x 1x 8x 8x 8x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 1x 6952x 1x 1x | import type { KeyLike, Linkable, StateChange, StateMetadata, StateObserver, StateObserverList, StatePublicTracker, StateTracker, } from './types.js'; import { captureStack } from './exception.js'; import { getDevTool } from './dev.js'; import { META_REGISTRY } from './registry.js'; import { shortId } from './utils/index.js'; import { isFunction } from '@beerush/utils'; import { ANCHOR_SETTINGS } from './constant.js'; let currentObserver: StateObserver | undefined = undefined; let currentRestorer: (() => void) | undefined = undefined; /** * 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 */ export function setObserver(observer: StateObserver) { // 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 prevObserver = currentObserver; const prevRestorer = currentRestorer; currentObserver = observer; currentRestorer = () => { if (!restored) { restored = true; currentObserver = prevObserver; currentRestorer = prevRestorer; } }; return currentRestorer; } /** * Gets the current observer context. * * @returns The current observer or undefined if none is set */ export function getObserver(): StateObserver | undefined { return currentObserver; } /** * 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 * @returns A new observer instance with states management, onChange handler, onDestroy hook, and cleanup functionality */ export function createObserver( onChange: (event: StateChange) => void, onTrack?: (state: Linkable, key: KeyLike) => void ): StateObserver { let observedSize = 0; const states = new WeakMap(); const destroyers = 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 = () => { for (const fn of destroyers) { if (typeof fn === 'function') { fn(); } } destroyers.clear(); }; const assign = ((init, observers) => { if (!observers.has(observer)) { observers.add(observer); destroyers.add(() => { states.delete(init); observers.delete(observer); getDevTool()?.onUntrack?.(META_REGISTRY.get(init) as StateMetadata, observer); }); } if (!states.has(init)) { states.set(init, new Set()); 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 => { return withinObserver(fn, observer); }; const observer = { id: shortId(), get states() { return states; }, get onChange() { return onChange; }, get destroy() { return destroy; }, 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 */ 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; } /** * 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 outsideObserver<R>(fn: () => R): R { const restore = setObserver(undefined as never); let result: R | undefined = undefined; if (typeof fn === 'function') { try { result = fn(); } catch (error) { captureStack.error.external( 'Unable to execute the outside of observer function', error as Error, outsideObserver ); } } else { const error = new Error('Invalid argument.'); captureStack.error.argument('The given argument is not a function', error, outsideObserver); } restore?.(); return result as R; } let currentTracker: StatePublicTracker | undefined; let currentTrackerRestore: (() => void) | undefined; /** * 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 */ export function setTracker(tracker: StatePublicTracker) { if (currentTracker === tracker) return currentTrackerRestore; let restored = false; const prevTracker = currentTracker; currentTracker = tracker; currentTrackerRestore = () => { if (!restored) { currentTracker = prevTracker; restored = true; } }; return currentTrackerRestore; } /** * 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 */ export function getTracker(): StatePublicTracker | undefined { return currentTracker; } /** * 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 */ export function track(init: Linkable, observers: StateObserverList, key: KeyLike) { if (typeof currentTracker !== 'function') return; currentTracker(init, observers, key); } |