UiAtom unstable
Shares effect's Atoms with components, as owners of shared state.
share turns an Atom into a SharedKey. A component lists it in reads
like any other owner and re-renders when the Atom changes. set and
refresh write it with a silent Command. The value comes from the reader's
AtomRegistry, where the Atom stays mounted while a reader is. family
does the same for an Atom.family, with each reader picking its member.
Example A counter whose count is an Atom
import { Component, UiAtom, UiSchema } from "effect/ui"import { Schema } from "effect"import { Atom } from "effect/reactivity"
const countAtom = Atom.make(0).pipe(Atom.keepAlive)const Count = UiAtom.share(countAtom, { name: "Count", schema: Schema.Number })
const Message = UiSchema.TaggedEnum({ ClickedIncrement: {} })
// Requires `AtomRegistry`: provide `AtomRegistry.layer`, or one registry the app shares.const Counter = Component.define({ name: "Counter", model: Schema.Null, message: Message, reads: { count: Count }}).make({ init: () => null, update: ($) => $.run(Count.set($.reads.count + 1)), view: (h) => h.el("button").on("click", Message.ClickedIncrement()).children(`count: ${h.reads.count}`)})Constructors
Shares an Atom.family as one key. Each reader reads the member that
select(model, props) in its reads entry returns the argument for.
Details
When a props change or an update makes select return another
argument, the reader moves to that member before it renders, in the same
flush. It stays mounted, keeps its Model, and gets no onChange. In the
update that changes the selection, $.reads still holds the old
member, as $.props holds the old props.
family must return the same Atom for an equal argument, as
Atom.family does. set(arg, value) and refresh(arg) put arg in
their args, encoded as MessageLog.structural does, so the log shows
which member was written. A reader lists a family at most once: to read
two members, share the Atom.family again under another name. Otherwise
it behaves as share.
Signature
declare const family: { <Name extends string, Arg, R, W>(family: (arg: Arg) => Writable<R, W>, options: { readonly name: Name; readonly schema?: Schema.Codec<R, any>; }): WritableFamily<Name, Arg, R, W>; <Name extends string, Arg, A>(family: (arg: Arg) => Atom<A>, options: { readonly name: Name; readonly schema?: Schema.Codec<A, any>; }): Family<Name, Arg, A>;}Example One view per todo, picked by id
import { Component, UiAtom, UiSchema } from "effect/ui"import { Schema } from "effect"import { Atom } from "effect/reactivity"
const Todo = Schema.Struct({ title: Schema.String, done: Schema.Boolean })const todoAtom = Atom.family((id: string) => Atom.make({ title: id, done: false }).pipe(Atom.keepAlive))const Todos = UiAtom.family(todoAtom, { name: "Todo", schema: Todo })
const Message = UiSchema.TaggedEnum({ Toggled: {} })
const TodoView = Component.define({ name: "TodoView", props: Schema.Struct({ id: Schema.String }), model: Schema.Null, message: Message, reads: { todo: { owner: Todos, select: (_model, props) => props.id } }}).make({ init: () => null, update: ($) => $.run(Todos.set($.props.id, { ...$.reads.todo, done: !$.reads.todo.done })), view: (h) => h.el("button").on("click", Message.Toggled()).children(h.reads.todo.title)})Models
An Atom.family shared with family, whose readers each pick a member.
Signature
interface Family<in out Name extends string, in out Arg, in out A> extends SharedKey<Name, A, never, never, AtomRegistry.AtomRegistry>, Selecting<Arg> { readonly family: (arg: Arg) => Atom<A>; refresh(arg: Arg): Write;}A writable Atom shared with share, which set writes.
Signature
interface Writable<in out Name extends string, in out R, in out W> extends Shared<Name, R> { readonly atom: Writable<R, W>; set(value: W): Write;}A family of writable Atoms shared with family, which set writes.
Signature
interface WritableFamily<in out Name extends string, in out Arg, in out R, in out W> extends Family<Name, Arg, R> { readonly family: (arg: Arg) => Writable<R, W>; set(arg: Arg, value: W): Write;}A silent Command that writes an Atom in the issuing component's
AtomRegistry.
Signature
type Write = Command.Issued<Component.Noop, AtomRegistry.AtomRegistry, Component.Noop, "Effect">