Skip to content

defineStateChannel

function defineStateChannel<S>(namespace): StateChannel<S>;

Define a typed state channel for a namespace. One definition is shared by the writer and every reader, so the namespace string and its payload shape are declared once instead of restated (and risked drifting) at each call site.

For a per-key family (e.g. one namespace per record id), compose a one-line factory in your app — each call returns a channel keyed by the produced namespace string, never by object identity, so the writer’s editDraft(id) and a reader’s editDraft(id) address the same namespace for the same id even though they are distinct channel objects.

S = unknown

string

StateChannel<S>

const wizard = defineStateChannel<WizardState>("wizard");
wizard.publish(conn.state, initial); // writer (e.g. a modal body)
wizard.subscribe(platform.state, cb); // reader (e.g. the parent app)
const editDraft = (id: string) => defineStateChannel<EditDraft>(`editDraft:${id}`);
editDraft(id).publish(conn.state, initial);
editDraft(id).subscribe(platform.state, cb);