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 | 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x | import { effectStack } from '../internal/scheduler';
/**
* Executes a function without tracking any of its dependencies.
*
* This is a powerful utility that temporarily disables reactive dependency tracking.
* Any signals read inside the untracked function will not cause the currently
* executing effect to re-run when those signals change.
*
* Common use cases:
* - Creating component instances that should maintain their own state
* - Performing side effects that shouldn't trigger reactive updates
* - Breaking unwanted dependency chains in complex reactive graphs
*
* @param fn The function to execute without tracking dependencies.
* @returns The return value of the function.
*
* @example
* ```typescript
* const [count, setCount] = createSignal(0);
*
* // This effect would normally re-run every time count changes
* createEffect(() => {
* console.log('Count changed:', count());
* });
*
* // But this won't trigger the effect
* untrack(() => {
* setCount(count() + 1); // Reading count here doesn't track it
* });
* ```
*/
export function untrack<T>(fn: () => T): T {
// 1. Save the current effect stack to restore later
const prevStack = [...effectStack];
// 2. Clear the global tracking stack to disable dependency tracking
effectStack.length = 0;
try {
// 3. Execute the function without any dependency tracking
return fn();
} finally {
// 4. Always restore the previous stack, even if an error occurs
effectStack.push(...prevStack);
}
}
|