api / const

compute

Creates a derived signal from a function with signalified arguments.

Source: src/api/compute.ts

Creates a derived signal from a function with signalified arguments.

This is a convenience wrapper around derive that automatically unwraps signal arguments before passing them to the function. The derived signal recomputes whenever any of the signalified arguments changes.

Signature

export const compute = <F extends (...args: any[]) => any>(
  computerFn: F,
  ...restArgs: MaybeSignalValues<Parameters<F>>
): DerivedSignal<ReturnType<F>> => {
  return derive(() => {
    const plainArgs = restArgs.map((arg) => value(arg)) as PlainValues<
      typeof restArgs
    >;
    return computerFn(...plainArgs);
  });
};

Type Parameters

  • The function type

Parameters

  • computerFn: A function that receives plain values and returns a value
  • restArgs: Signalified arguments matching the function parameters

Returns

A derived signal of the function's return type

Remarks

  • Works with functions of any arity
  • Can mix signals and plain values in arguments
  • Plain value arguments do not trigger recomputation
  • Arguments are unwrapped using the value() helper

Examples

const a = signal(5);
const b = signal(3);
const sum = compute((x: number, y: number) => x + y, a, b);
console.log(sum.value); // 8

// Mixed signals and plain values
const sum2 = compute((x: number, y: number) => x + y, a, 10);
console.log(sum2.value); // 15

// Complex computation
const base = signal(100);
const rate = signal(0.1);
const years = signal(5);
const interest = compute(
  (b: number, r: number, y: number) => b * Math.pow(1 + r, y),
  base, rate, years
);

See Also

  • derive - For the underlying derived signal primitive
  • value - For unwrapping signalified values