Skip to content

Interface: ReadonlyAtom<T>

API / @xmachines/play-atom / ReadonlyAtom

Defined in: play-atom/src/types.ts:97

The computed atom. It evaluates late, and it memoizes the result

createAtom returns this type for a function argument. The atom tracks each dependency when it runs the function, and it runs the function again only after a dependency changes. The atom therefore tracks each dependency for you, and you manage no subscription.

Example

import { createAtom } from "@xmachines/play-atom";
const count = createAtom(0);
const doubled = createAtom(() => count.get() * 2);
console.log(doubled.get()); // 0
count.set(5);
console.log(doubled.get()); // 10 (the atom computed it again)
console.log(doubled.get()); // 10 (from the memory, with no new computation)

Extends

Type Parameters

Type Parameter
T

Methods

get()

get(): T;

Defined in: play-atom/src/types.ts:59

Returns

T

Inherited from

BaseAtom.get


subscribe()

Call Signature

subscribe(observer): AtomSubscription;

Defined in: play-atom/src/types.ts:60

Parameters
ParameterType
observerAtomObserver<T>
Returns

AtomSubscription

Inherited from

BaseAtom.subscribe

Call Signature

subscribe(
next,
error?,
complete?
): AtomSubscription;

Defined in: play-atom/src/types.ts:61

Parameters
ParameterType
next(value) => void
error?(error) => void
complete?() => void
Returns

AtomSubscription

Inherited from

BaseAtom.subscribe