import { TinyPluginCore, TinyPluginLayer, TinyPlugin } from '../../plugin/TinyPlugin.mjs'; export type MessagePayload = any; export type ApiHandlerOptions = { /** * - The payload received from the browser. */ data?: MessagePayload; /** * - The request ID. */ correlationId: string; }; export type ApiHandlerCallback = (options: ApiHandlerOptions) => Promise | (MessagePayload | undefined); export type ServiceWorkerMessagePayload = { /** * - The identifier for the message type. */ type: string; /** * - The actual data content of the message. */ data?: MessagePayload; }; export type BeforeInstallPromptEvent = { /** * - Prevents the default browser behavior. */ preventDefault: () => void; /** * - A promise that resolves with the user's choice. */ userChoice: Promise<{ outcome: 'accepted' | 'dismissed'; }>; /** * - The method to show the native installation prompt. */ prompt: () => Promise; /** * - Indicates if the event can be shared. */ canShare: boolean; }; export type SwPluginInstaller = import('../../plugin/TinyPlugin.mjs').TinyPluginInstaller, Layer, IdString, VersionString, Options>; export type TinyServiceWorkerPlugin = TinyPlugin, Layer, IdString, VersionString, Options>; /** * The data payload contained within the message. * @typedef {any} MessagePayload - A key-value map representing the message content. */ /** * Represents the options object passed to an API handler registered via `onApi`. * @typedef {Object} ApiHandlerOptions * @property {MessagePayload} [data] - The payload received from the browser. * @property {string} correlationId - The request ID. */ /** * A function that processes an incoming API request coming from the Service Worker * and optionally returns the response payload. * @callback ApiHandlerCallback * @param {ApiHandlerOptions} options - The options for the API handler, containing the request data and correlation ID. * @returns {Promise | (MessagePayload|undefined)} The response payload. */ /** * Represents a message exchanged between the page and the Service Worker. * @typedef {Object} ServiceWorkerMessagePayload * @property {string} type - The identifier for the message type. * @property {MessagePayload} [data] - The actual data content of the message. */ /** * Represents the non-standard `beforeinstallprompt` event used to trigger * the native PWA installation flow. * @typedef {Object} BeforeInstallPromptEvent * @property {() => void} preventDefault - Prevents the default browser behavior. * @property {Promise<{ outcome: 'accepted' | 'dismissed' }>} userChoice - A promise that resolves with the user's choice. * @property {() => Promise} prompt - The method to show the native installation prompt. * @property {boolean} canShare - Indicates if the event can be shared. */ /** * A function used to install a plugin into the engine. * @template {TinyPluginLayer} Layer * @template {string} IdString * @template {string} VersionString * @template {any[]} Options * @typedef {import('../../plugin/TinyPlugin.mjs').TinyPluginInstaller, Layer, IdString, VersionString, Options>} SwPluginInstaller */ /** * Represents a plugin instance designed to be integrated into a TinyServiceWorker. * @template {TinyPluginLayer} Layer * @template {string} IdString * @template {string} VersionString * @template {any[]} Options * @typedef {TinyPlugin, Layer, IdString, VersionString, Options>} TinyServiceWorkerPlugin */ /** * Sends a message to the service worker controller. * * @param {*} message - The message to be sent to the service worker. * @param {Transferable[]} [transfer] - An optional array of transferable objects to transfer ownership of. * @returns {void} This function does not return a value. * @throws {Error} If the Service Worker is not available or not controlling the page. * @throws {TypeError} If the transfer argument is provided but is not an array. */ declare const postMessage: (message: any, transfer?: Transferable[]) => void; export type TinyPushMessage = import('../utils.mjs').TinyPushMessage; export type PushEventData = { /** * - The push payload merged with the notification state. */ data: TinyPushMessage & { notificationShown: boolean; }; /** * - The original message event. */ event: MessageEvent; /** * - Whether the Service Worker already displayed a notification. */ notificationShown: boolean; /** * - Helper that renders a fallback notification in the page. */ showNotification: (overrides?: Partial) => Notification | null; }; export type PushEventEmitter = import('../../../jsdoc/EventEmitter.mjs').ICustomEventEmitter void>>; /** @typedef {import('../utils.mjs').TinyPushMessage} TinyPushMessage */ /** * Represents the object emitted for every push event received from the Service Worker. * @typedef {Object} PushEventData * @property {TinyPushMessage & { notificationShown: boolean }} data - The push payload merged with the notification state. * @property {MessageEvent} event - The original message event. * @property {boolean} notificationShown - Whether the Service Worker already displayed a notification. * @property {(overrides?: Partial) => Notification|null} showNotification - Helper that renders a fallback notification in the page. */ /** * An event emitter that dispatches push events keyed by topic. * @typedef {import('../../../jsdoc/EventEmitter.mjs').ICustomEventEmitter void>>} PushEventEmitter */ /** * @template {string} IdWorker * @template {string | URL} SwUrl * Manages Service Worker registration, versioning, and messaging. */ declare class TinyServiceWorker extends TinyPluginCore { #private; static postMessage: typeof postMessage; static get PUSH_TYPE(): Readonly<{ NOTIFICATION: "notification"; DATA: "data"; SYNC: "sync"; COMMAND: "command"; UNKNOWN: "unknown"; }>; /** * Returns a promise that resolves when the Service Worker is ready. * @returns {Promise} A promise that resolves to the Service Worker registration. */ static waitForReady(): Promise; /** @returns {PushEventEmitter} The emitter to receive push events. */ get pushEvents(): PushEventEmitter; /** @returns {boolean} True if the service worker is ready. */ get isReady(): boolean; /** @returns {boolean} True if the registration process failed. */ get isFailed(): boolean; /** @returns {boolean} True if the instance has been destroyed. */ get isDestroyed(): boolean; /** @returns {boolean} True if a service worker is supported and an active controller exists. */ get isSwAvailable(): boolean; /** @returns {IdWorker} The instance ID. */ get id(): IdWorker; /** @returns {SwUrl} The service worker URL. */ get swUrl(): SwUrl; /** @returns {string} The current version. */ get version(): string; /** @returns {ServiceWorkerRegistration | null} The current registration object. */ get registration(): ServiceWorkerRegistration | null; /** * Returns a list of all registered event callbacks. * @returns {EventListener[]} An array of event listeners. */ get eventListeners(): EventListener[]; /** * Determines the current PWA display mode. * @returns {'twa' | 'standalone' | 'browser'} The current display mode. */ get displayMode(): 'twa' | 'standalone' | 'browser'; /** @returns {boolean} True if auto-notification is enabled. */ get autoNotifyPush(): boolean; /** @param {boolean} value - Whether to enable auto-notification. */ set autoNotifyPush(value: boolean); /** * Returns a copy of the blacklisted event names. * @returns {string[]} An array with every blacklisted event name. */ get eventBlacklist(): string[]; /** * Adds an event name to the blacklist. Blacklisted events are ignored when * they arrive from the Service Worker. * @param {string} type - The event name to ignore. * @returns {void} * @throws {TypeError} If the type is not a non-empty string. */ addToEventBlacklist(type: string): void; /** * Removes an event name from the blacklist. * @param {string} type - The event name to remove. * @returns {boolean} True if the event was removed, false otherwise. * @throws {TypeError} If the type is not a non-empty string. */ removeFromEventBlacklist(type: string): boolean; /** * Checks whether an event name is blacklisted. * @param {string} type - The event name to check. * @returns {boolean} True if the event is blacklisted, false otherwise. * @throws {TypeError} If the type is not a non-empty string. */ hasInEventBlacklist(type: string): boolean; /** * Removes every event name from the blacklist. * @returns {void} */ clearEventBlacklist(): void; /** * @param {Object} options - Configuration options for the instance. * @param {IdWorker} options.id - The unique identifier for this manager instance. * @param {SwUrl} options.swUrl - The path to the service worker file. * @param {string} options.version - The current application version. * @param {boolean} [options.debugMode=false] - Whether to enable internal debug logging. * @param {boolean} [options.useLogColors=false] - Whether to enable log color support. * @param {Partial} [options.logger=console] - A custom logger object (must implement console methods). * @throws {TypeError} If parameters are not the correct types or if id is empty. */ constructor(options: { id: IdWorker; swUrl: SwUrl; version: string; debugMode?: boolean; useLogColors?: boolean; logger?: Partial; }); /** * Triggers the native PWA installation prompt. * @returns {Promise} A promise that resolves when the prompt has been shown. * @throws {Error} If the prompt cannot be shown because the `beforeinstallprompt` event has not fired. */ promptInstallation(): Promise; /** * Returns a promise that resolves when the Service Worker is fully registered, * initialized, and has taken control of the current page. * @returns {Promise} A promise that resolves when the Service Worker is ready for use. * @throws {Error} If the Service Worker registration fails. */ waitForReady(): Promise; /** * Registers a new sync tag to be handled by the Service Worker. * This will trigger the 'sync' event in the Service Worker when connectivity is available. * * @param {string} tag - A unique identifier for the sync task. * @returns {Promise} A promise that resolves when the sync registration is successful. * @throws {Error} If Background Sync is not supported by the browser. * @throws {TypeError} If the tag is not a non-empty string. */ registerSync(tag: string): Promise; /** * Requests permission from the user to display notifications. * * @returns {Promise<'granted' | 'denied' | 'default'>} The resulting permission status. * @throws {Error} If the Notification API is not supported by the browser. */ requestNotificationPermission(): Promise<'granted' | 'denied' | 'default'>; /** * Registers the service worker and handles version updates. * @param {RegistrationOptions} [options] - Standard Service Worker registration options. * @returns {Promise} A promise that resolves when registration is attempted. * @throws {Error} If the registration process encounters an error. */ register(options?: RegistrationOptions): Promise; /** * Unregisters the Service Worker from the browser and destroys the manager instance. * @returns {Promise} A promise that resolves to `true` if the Service Worker * was successfully unregistered, or `false` otherwise. * @throws {Error} If an unexpected error occurs during the unregistration process. */ unregister(): Promise; /** * Removes a registered API handler. * @param {string} type - The call identifier. * @returns {boolean} True if a handler was removed, false otherwise. * @throws {TypeError} If the type is not a non-empty string. */ offApi(type: string): boolean; /** * Registers a handler for API calls coming from the Service Worker. * @param {string} type - The call identifier. * @param {ApiHandlerCallback} callback - Function that processes the request and returns the result. * @returns {void} * @throws {TypeError} If the type is not a non-empty string. * @throws {TypeError} If the callback is not a function. */ onApi(type: string, callback: ApiHandlerCallback): void; /** * Sends a message to the active Service Worker and waits for a response via Promise. * @param {string} type - The API call identifier. * @param {MessagePayload} [data] - The request payload. * @param {number} [timeout=10000] - Maximum waiting time in milliseconds. * @returns {Promise} A promise that resolves with the result object or undefined from the Service Worker. * @throws {TypeError} If the type is not a non-empty string. * @throws {TypeError} If the data is not a non-null object. * @throws {TypeError} If the timeout is not a number. * @throws {RangeError} If the timeout is not a finite number greater than or equal to 0. * @throws {Error} If the timeout is reached or if the Service Worker is unavailable. */ emitApi(type: string, data?: MessagePayload, timeout?: number): Promise; /** * Sends a message to the active Service Worker controller. * @param {string} type - The identifier for the message type. * @param {MessagePayload} [data] - The actual data content of the message. * @returns {boolean} True if the message was sent, false otherwise. * @throws {TypeError} If the type is not a string or data is not a non-null object. */ emitMessage(type: string, data?: MessagePayload): boolean; /** * Sends a message to the active Service Worker controller using a full payload. * @param {ServiceWorkerMessagePayload} payload - The message payload. * @throws {TypeError} If the payload is not an object, type is not a string, or data is not a non-null object. */ postMessage(payload: ServiceWorkerMessagePayload): void; /** * Adds an event listener for messages from the Service Worker. * @param {EventListener} callback - The callback function to be invoked. * @throws {TypeError} If the callback is not a function. */ addEventListener(callback: EventListener): void; /** * Removes an event listener from the Service Worker. * @param {EventListener} callback - The callback to be removed. * @returns {boolean} True if the event listener was removed, false otherwise. * @throws {TypeError} If the callback is not a function. */ removeEventListener(callback: EventListener): boolean; /** * Cleans up all event listeners and references to prevent memory leaks. * @returns {void} */ destroy(): void; } export default TinyServiceWorker; //# sourceMappingURL=TinyServiceWorker.d.mts.map