computed

See source code

Decorator factory for creating computed methods with options.

function computed<Value, Diff = unknown>(
  options?: ComputedOptions<Value, Diff>,
): ((
  target: any,
  key: string,
  descriptor: PropertyDescriptor,
) => PropertyDescriptor) &
  (<This>(
    compute: () => Value,
    context: ClassMethodDecoratorContext<This, () => Value>,
  ) => () => Value);

Example

class MyClass {
  items = atom("items", [1, 2, 3]);

  @computed({ historyLength: 10 })
  sum() {
    return this.items.get().reduce((a, b) => a + b, 0);
  }
}

Parameters

NameDescription

options

ComputedOptions<Value, Diff>;

Configuration options for the computed signal

Returns

((
  target: any,
  key: string,
  descriptor: PropertyDescriptor,
) => PropertyDescriptor) &
  (<This>(
    compute: () => Value,
    context: ClassMethodDecoratorContext<This, () => Value>,
  ) => () => Value);

A decorator function that can be applied to methods

Prev
atom
Next
getComputedInstance