Skip to content

AtomRef

Mutable reactive references for local, in-memory state.

AtomRef provides small observable state cells that can be read, updated, mapped, and subscribed to without going through an AtomRegistry. Mutable refs can also create refs for nested properties. The module also provides a collection helper that stores item refs and notifies subscribers when items are inserted, removed, or changed.

7 exports Added in v4.0.0 Source

Constructors

collection

Added in v4.0.0 Source

Creates a reactive collection from an iterable of initial item values.

Details

Each item is wrapped in an AtomRef, and changes to item refs notify the collection subscribers.

Signature

declare function collection<A>(items: Iterable<A>): Collection<A>;

make

Added in v4.0.0 Source

Creates a mutable reactive reference initialized with the supplied value.

Signature

declare function make<A>(value: A): AtomRef<A>;

Models

AtomRef interface

Added in v4.0.0 Source

A mutable reactive reference.

Details

It supports replacing the whole value, updating it from the current value, and creating mutable references to nested properties.

Signature

interface AtomRef<A> extends ReadonlyRef<A> {
  readonly prop: <K extends string | number | symbol>(prop: K) => AtomRef<A[K]>;
  readonly set: (value: A) => AtomRef<A>;
  readonly update: (f: (value: A) => A) => AtomRef<A>;
}

Collection interface

Added in v4.0.0 Source

A reactive collection of mutable item references.

Details

The collection can push, insert, and remove item refs, and toArray returns the current raw item values.

Signature

interface Collection<A> extends ReadonlyRef<ReadonlyArray<AtomRef<A>>> {
  readonly insertAt: (index: number, item: A) => Collection<A>;
  readonly push: (item: A) => Collection<A>;
  readonly remove: (ref: AtomRef<A>) => Collection<A>;
  readonly toArray: () => Array<A>;
}

ReadonlyRef interface

Added in v4.0.0 Source

A read-only reactive reference.

Details

It exposes a stable key, the current value, subscriptions to value changes, and map for creating derived read-only references. Equality and hashing are based on the current value.

Signature

interface ReadonlyRef<A> extends Equal {
  readonly "~effect/reactivity/AtomRef": "~effect/reactivity/AtomRef";
  readonly key: string;
  readonly map: <B>(f: (a: A) => B) => ReadonlyRef<B>;
  readonly subscribe: (f: (a: A) => void) => () => void;
  readonly value: A;
}

Type IDs

TypeId

Added in v4.0.0 Source

The runtime type id used to identify AtomRef values.

Signature

declare const TypeId: "~effect/reactivity/AtomRef";

TypeId type

Added in v4.0.0 Source

The literal type used to identify AtomRef values.

Signature

type TypeId = "~effect/reactivity/AtomRef";