@efesto-cloud/observable
v0.0.4
Published
Observable type for efesto-cloud
Readme
@efesto-cloud/observable
A minimal observable value container — holds state, lets consumers read it, and notifies subscribers when it changes.
Use Observable when you need current value + updates. Use @efesto-cloud/publisher for fire-and-forget events without stored state.
Installation
pnpm add @efesto-cloud/observable @efesto-cloud/publisherQuick Start
import { Observable } from "@efesto-cloud/observable";
const count = new Observable(0);
const off = count.subscribe((value) => {
console.log("count changed:", value);
});
count.set(1); // logs: count changed: 1
count.get(); // 1
off();
count.dispose(); // removes all subscribersAPI
interface IObservable<T> {
get(): T;
set(value: T): void;
subscribe(callback: (value: T) => void): Unsubscribe;
dispose(): void;
}new Observable(initialState)— seeded with a starting value.get()— current value.set(value)— replace the value and notify subscribers.subscribe(cb)— returns anUnsubscribefunction.dispose()— unsubscribe every listener.
Rules
- If you only need events (no stored value), use
Publisherinstead. - Always call the returned unsubscribe function, or
dispose()when tearing down the owning object. set(value)always notifies — even if the new value is identical to the previous one. If you need deduped updates, layerComputedor check before callingset.
Example: shared state between components
const theme = new Observable<"light" | "dark">("light");
// Component A
const off = theme.subscribe((t) => applyTheme(t));
// Component B
theme.set("dark");
// On unmount
off();Related
@efesto-cloud/publisher— pub/sub without state.@efesto-cloud/compute— derive a value from one or more observables.
