All files / src/internal scheduler.ts

100% Statements 21/21
100% Branches 7/7
100% Functions 2/2
100% Lines 21/21

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                    1x       1x     1x           1x   219x   105x     105x 105x   105x 105x   105x 105x 105x 105x 105x           1x 355x 353x 353x 355x 355x  
// A Subscriber is an effect that has dependencies.
// We store the dependencies on the effect itself for easy cleanup.
export type Subscriber = {
  execute: () => void;
  dependencies: Set<Set<Subscriber>>;
  name?: string; // Add the optional name property
};
 
// The global stack of effects currently being executed.
// The effect at the top of the stack is the one that is currently tracking dependencies.
export const effectStack: Subscriber[] = [];
 
// A set of effects that have been marked as "dirty" and need to be re-run.
// Using a Set automatically handles deduplication.
export const dirtyEffects = new Set<Subscriber>();
 
// A flag to prevent effects from running while a batch is in progress.
export let isBatching = false;
 
/**
 * Schedules a flush of the dirty effects queue to run in the next microtask.
 * This is the core of our batching mechanism.
 */
export function flushQueue() {
  // If we are already in a batch or a flush is already scheduled, do nothing.
  if (isBatching) return;
 
  isBatching = true;
  // Use queueMicrotask to defer the execution until the current synchronous
  // code block has finished executing.
  queueMicrotask(() => {
    try {
      // Run all unique effects that have been marked as dirty.
      dirtyEffects.forEach((effect) => effect.execute());
    } finally {
      // Clear the queue and reset the batching flag after execution.
      dirtyEffects.clear();
      isBatching = false;
    }
  });
}
 
/**
 * Cleans up an effect by removing it from all of its dependencies (the signals it subscribes to).
 * This is crucial for preventing memory leaks.
 */
export function cleanup(effect: Subscriber) {
  for (const dependency of effect.dependencies) {
    dependency.delete(effect);
  }
  effect.dependencies.clear();
}