type EventHandler = (data: T) => void; export interface EventType { __subscribe: (fn: EventHandler) => () => void; trigger: (data?: T) => void; name: string; __isEvent___: true; } /** * Creates a typed pub/sub event. * * @param name - Optional human-readable name for the event. If omitted, a random * string is generated. The name appears in devtools traces. * * @remarks * - `trigger(data?)` — fires all subscribers synchronously and logs to devtools. * - `__subscribe(fn)` — registers a handler and returns an unsubscribe function. * - Pass the event as a dependency to `on()` so the callback re-runs whenever * the event fires: `on(callback, [myEvent])`. * - Cannot be created inside a template function (a restricted-context guard is * applied via `assertNotInRestrictedContext`). * * @example * ```ts * const userLoggedIn = event<{ userId: string }>("user:login"); * * const unsub = userLoggedIn.__subscribe((data) => { * console.log("logged in:", data.userId); * }); * * userLoggedIn.trigger({ userId: "abc123" }); * unsub(); // stop listening * ``` */ export declare const event: (name?: string) => EventType; export interface ChannelType { join: (handler: EventHandler) => { leave: () => void; send: (data?: T) => void; }; name?: string; __isChannel___: true; } type CleanupHandler = (data: T) => (() => void) | undefined; export interface CleanupEventType { __subscribe: (fn: CleanupHandler) => () => void; trigger: (data?: T) => void; cleanup: () => void; cleanupAndClearSubs: () => void; readonly hasSubscribers: boolean; name: string; __isCleanupEvent___: true; __isEvent___: true; } /** * Like `event`, but each subscriber can return a cleanup function that runs * automatically before the handler is called again on the next `trigger()`. * * @param name - Optional human-readable name for the event. If omitted, a random * string is generated. The name appears in devtools traces. * * @remarks * - `trigger(data?)` — for every subscriber: runs its previous cleanup (if any), * then calls the handler with the new data, and stores whatever the handler * returns as the next cleanup. * - `__subscribe(fn)` — registers a handler; returns an unsubscribe function that * also runs any pending cleanup for that handler. * - `cleanup()` — runs all stored cleanups without removing subscribers. * - `cleanupAndClearSubs()` — runs all stored cleanups AND removes every subscriber. * - `hasSubscribers` — boolean getter; `true` when at least one subscriber is registered. * - Cannot be created inside a template function (restricted-context guard applies). * * @example * ```ts * const tabVisible = cleanupEvent("tab:visible"); * * tabVisible.__subscribe((visible) => { * const timer = setInterval(() => {}, 1000); * return () => clearInterval(timer); // runs before next trigger * }); * * tabVisible.trigger(true); * tabVisible.trigger(false); // runs previous cleanup first * tabVisible.cleanup(); // run all cleanups without removing subs * tabVisible.cleanupAndClearSubs(); // run cleanups + remove all subs * ``` */ export declare const cleanupEvent: (name?: string) => CleanupEventType; /** * Creates a many-to-many pub/sub channel where any member can broadcast to all * OTHER members — the sender never receives its own message. * * @remarks * - `join(handler)` — adds `handler` to the channel and returns `{ send, leave }`. * - `send(data?)` — broadcasts `data` to every member except the caller. * - `leave()` — removes the handler from the channel. * - Auto-cleanup: when `join()` is called inside a component, `leave()` is * automatically registered via `registerCleanup` so the handler is removed when * the component unmounts. * - Cannot be created inside a template function, and `join()` also enforces the * same restricted-context guard. * * @example * ```ts * const chat = channel(); * * // Component A joins: * const { send: sendA, leave: leaveA } = chat.join((msg) => { * console.log("A received:", msg); * }); * * // Component B joins: * const { send: sendB } = chat.join((msg) => { * console.log("B received:", msg); * }); * * sendA("hello from A"); // only B receives * sendB("hello from B"); // only A receives * leaveA(); // A leaves the channel * ``` */ export declare const channel: () => ChannelType; export { MATES_XTAB_CHANNEL, type XTabEventType, type XTabHandler, xTabEvent, } from "./xTabEvent"; //# sourceMappingURL=events.d.ts.map