export type TinyPushContext = { /** * - The native push event. */ event: PushEvent; /** * - The normalized message. */ message: import('./shared/TinyPushPayload.mjs').TinyPushMessage; /** * - The engine that received the push. */ engine: import('./TinyServiceWorkerEngine.mjs').default; /** * - Set to `true` to stop the chain. */ handled: boolean; /** * - Set to `true` when a notification was displayed. */ notificationShown: boolean; }; export type TinyPushHandler = (message: import('./shared/TinyPushPayload.mjs').TinyPushMessage, context: TinyPushContext) => Promise | boolean | void; export type TinyPushGuard = (message: import('./shared/TinyPushPayload.mjs').TinyPushMessage, context: TinyPushContext) => Promise | boolean; /** * @file Routes a normalized push message to a handler, based on `message.type`. * * The router is transport agnostic: it never touches the Notification API. That * responsibility belongs to the handler, which keeps the class testable in Node. */ /** * Describes the mutable state shared between the router and the active handler * during a single dispatch cycle. * * @typedef {Object} TinyPushContext * @property {PushEvent} event - The native push event. * @property {import('./shared/TinyPushPayload.mjs').TinyPushMessage} message - The normalized message. * @property {import('./TinyServiceWorkerEngine.mjs').default} engine - The engine that received the push. * @property {boolean} handled - Set to `true` to stop the chain. * @property {boolean} notificationShown - Set to `true` when a notification was displayed. */ /** * Handles a normalized push message that matched a route. * * @callback TinyPushHandler * @param {import('./shared/TinyPushPayload.mjs').TinyPushMessage} message - The normalized message. * @param {TinyPushContext} context - The dispatch context. * @returns {Promise|boolean|void} Return `false` to fall through to the next handler. */ /** * Decides whether a normalized push message is allowed to reach a handler. * * @callback TinyPushGuard * @param {import('./shared/TinyPushPayload.mjs').TinyPushMessage} message - The normalized message. * @param {TinyPushContext} context - The dispatch context. * @returns {Promise|boolean} Return `false` to drop the message. */ /** * Routes normalized push messages to registered handlers based on `message.type`. * * @beta */ declare class TinyPushRouter { #private; static get PUSH_TYPE(): Readonly<{ NOTIFICATION: "notification"; DATA: "data"; SYNC: "sync"; COMMAND: "command"; UNKNOWN: "unknown"; }>; /** * Registers a guard. Every guard runs before the handler. * * A guard that returns `false` drops the message: no handler runs and the * `push` promise resolves with `true`, so the browser does not show a * generic "This site has been updated in the background" notification. * * @param {TinyPushGuard} guard - The guard to register. * @returns {this} The router, for chaining. * @throws {TypeError} If `guard` is not a function. */ use(guard: TinyPushGuard): this; /** * Registers a handler for an exact `message.type`. * * @param {string} type - The type to match. * @param {TinyPushHandler} handler - The handler. * @returns {this} The router, for chaining. * @throws {TypeError} If `type` is not a non-empty string or `handler` is not a function. */ on(type: string, handler: TinyPushHandler): this; /** * Registers a handler for every `message.type` that matches a regular expression. * * @param {RegExp} pattern - The pattern applied to `message.type`. * @param {TinyPushHandler} handler - The handler. * @returns {this} The router, for chaining. * @throws {TypeError} If `pattern` is not a RegExp or `handler` is not a function. */ onMatch(pattern: RegExp, handler: TinyPushHandler): this; /** * Registers the handler used when no other handler matches. * * @param {TinyPushHandler} handler - The fallback handler. * @returns {this} The router, for chaining. * @throws {TypeError} If `handler` is not a function. */ otherwise(handler: TinyPushHandler): this; /** * Registers an error handler. * * @param {(error: unknown, message: unknown) => void} handler - The error handler. * @returns {this} The router, for chaining. * @throws {TypeError} If `handler` is not a function. */ catch(handler: (error: unknown, message: unknown) => void): this; /** * Reports whether a handler is registered for the given type. * * @param {string} type - The type to look up. * @returns {boolean} `true` when a handler is registered. */ has(type: string): boolean; /** @returns {number} The number of registered handlers. */ get size(): number; /** * Dispatches a message to the first matching handler. * * @param {import('./shared/TinyPushPayload.mjs').TinyPushMessage} message - The normalized message. * @param {TinyPushContext} context - The dispatch context. * @returns {Promise} `true` when a handler claimed the message. * @throws {TypeError} If `message` or `context` are invalid. */ dispatch(message: import('./shared/TinyPushPayload.mjs').TinyPushMessage, context: TinyPushContext): Promise; } export default TinyPushRouter; //# sourceMappingURL=TinyPushRouter.d.mts.map