All files / src/primitives memo.ts

100% Statements 9/9
100% Branches 2/2
100% Functions 1/1
100% Lines 9/9

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 291x 1x                   1x     47x       47x 89x 47x           47x 47x  
import { createSignal } from './signal';
import { createEffect } from './effect';
import type { Memo, ReactiveOptions } from '../types';
 
/**
 * Creates a derived, memoized signal that only re-computes when its dependencies change.
 *
 * @param fn The function to compute the value.
 * @param options Optional configuration, including a debug name.
 * @returns A read-only Memo object.
 */
export function createMemo<T>(fn: () => T, options?: ReactiveOptions): Memo<T> {
  // A memo is essentially a signal that is updated by an effect.
  // The signal can hold either the computed value T or be undefined initially.
  const [memo, setMemo] = createSignal<T | undefined>(undefined, options);
 
  // This effect tracks the dependencies of the memo function and updates the signal's value.
  // Pass the name to the underlying effect for better debugging
  createEffect(() => {
    setMemo(fn());
  }, options);
 
  // The type cast here is safe and intentional.
  // The internal effect runs synchronously upon creation, so the `memo` signal
  // is guaranteed to have a value of type `T` before it's returned to the user.
  // This hides the initial `undefined` state from the public API.
  return memo as Memo<T>;
}