Home > @pago/reactive > memoize

memoize() function

Returns a function that is only executed again if any of its tracked values have changed. The controller can be used to establish a notification system and is largely irrelevant to end users of the API.

Signature:

export declare function memoize<T>(fn: () => T, controller?: SubscriptionController): () => T;

Parameters

Parameter Type Description
fn () => T A memoized function.
controller SubscriptionController A controller that can be used to manage subscribing to tracked values.

Returns:

() => T

Example

const person = ref('Preact');
const message = memoize(() => `Hello ${person.current}`);

console.log(message()); // => 'Hello Preact'
console.log(message()); // => 'Hello Preact', but this time the memoized function was not executed at all