module@warp-drive/alien-signals/primitives
Function createMemo() sincev5.10.0
ts
function createMemo<T>(
obj: object,
key: string | symbol,
fn: () => T
): MemoNode<T>;Defined in: primitives.ts:259
Creates a MemoNode that caches the result of fn. Read it with readMemo: the first read runs fn, and later reads return the cached result until a signal or memo fn read changes. If fn throws, the error is cached and rethrown by each read until then.
Type Parameters
T
T
Parameters
obj
object
the object the memo belongs to, kept for debugging in development builds
key
string | symbol
the key the memo is for, kept for debugging in development builds
fn
() => T
the function whose result to cache
Returns
MemoNode<T>
a new memo, which has not run fn yet
Example
ts
import { createMemo, readMemo } from '@warp-drive/alien-signals/primitives';
const fullName = createMemo(user, 'fullName', () => `${user.firstName} ${user.lastName}`);
readMemo(fullName); // runs the function
readMemo(fullName); // returns the cached result