@efesto-cloud/publisher
v0.0.4
Published
Publisher type for efesto-cloud
Downloads
535
Readme
@efesto-cloud/publisher
Tiny, typed pub/sub primitive for broadcasting events without stored state.
Use Publisher for fire-and-forget events; use @efesto-cloud/observable when you also need to keep the current value.
Installation
pnpm add @efesto-cloud/publisherQuick Start
import { Publisher } from "@efesto-cloud/publisher";
const bus = new Publisher<[string, number]>();
const off = bus.subscribe((event, code) => {
console.log(event, code);
});
bus.notify("saved", 200);
off(); // unsubscribeAPI
interface IPublisher<ARGS extends unknown[]> {
size: number;
subscribe(s: (...args: ARGS) => void): () => void;
unsubscribe(id: number): void;
notify(...args: ARGS): void;
unsubscribeAll(): void;
}new Publisher<ARGS>()— typed tuple of event args.subscribe(listener)— returns anUnsubscribefunction; keep it for cleanup.notify(...args)— invoke every subscriber synchronously.unsubscribeAll()— clear all listeners (e.g. on dispose).
Rules
- Do not use
Publisheras a state container — if you needget/set, useObservable. - Always keep and call the returned unsubscribe function to avoid leaks.
- Emitted arguments must match the declared tuple type.
Example: typed event bus
type Events = [event: "created" | "deleted", id: string];
const events = new Publisher<Events>();
events.subscribe((event, id) => {
if (event === "created") console.log("new", id);
});
events.notify("created", "post-123");