UNPKG

seitu

Version:
61 lines • 2.31 kB
//#region src/core/subscription/index.d.ts interface SubscribeOptions { /** Call the callback immediately with the current value. */ immediate?: boolean; } interface Subscribable<V> { subscribe: (callback: (value: V) => any, options?: SubscribeOptions) => () => void; '~': { output: V; notify: () => void; }; } interface Readable<T> { get: () => T; /** * SSR/hydration value. Bindings use this instead of `get()` so the first * client render matches server HTML. Omitted: `get()` is used on both sides. */ getServer?: () => T; } /** A `Readable` that always has `getServer()`. Every `seitu/web` handle implements it. */ interface ServerReadable<T> extends Readable<T> { getServer: () => T; } interface Writable<T, P = T> { set: (value: T | ((prev: P) => T)) => any; } interface Clearable { clear: () => void; } declare function createReadableSubscription<T>(get: () => T, subscribe: (callback: () => any, options?: SubscribeOptions) => () => void, notify: () => void, getServer: () => T): ServerReadable<T> & Subscribable<T>; declare function createReadableSubscription<T>(get: () => T, subscribe: (callback: () => any, options?: SubscribeOptions) => () => void, notify: () => void, getServer?: (() => T) | undefined): Readable<T> & Subscribable<T>; interface Subscription { subscribe: (callback: () => any, options?: SubscribeOptions) => () => void; notify: () => void; readonly size: number; } /** * Subscribe/notify primitive used by every reactive handle. * * - `onFirstSubscribe` attaches on first subscriber and cleans up on last. * - `notify` calls every subscriber; the first throw is rethrown after the loop. * - `size` is the subscriber count. * * @example * ```ts twoslash * import { createSubscription } from 'seitu' * * const { subscribe, notify, size } = createSubscription({ * onFirstSubscribe: () => { * const id = setInterval(notify, 1000) * return () => clearInterval(id) * }, * }) * ``` */ declare function createSubscription(options?: { onFirstSubscribe?: () => void | (() => void); }): Subscription; //#endregion export { SubscribeOptions as a, createReadableSubscription as c, Subscribable as i, createSubscription as l, Readable as n, Subscription as o, ServerReadable as r, Writable as s, Clearable as t };