@aws-amplify/core
Version:
Core category of aws-amplify
114 lines (113 loc) • 5.29 kB
TypeScript
/**
* Handler invoked on every service worker `statechange` event, receiving the
* worker's current lifecycle state.
*/
export type ServiceWorkerStateChangeHandler = (state: ServiceWorkerState) => void;
/**
* Options for {@link ServiceWorker.register}.
*/
export interface ServiceWorkerOptions {
/**
* Optional handler invoked on every service worker `statechange` event.
*
* When provided, this handler replaces the built-in Pinpoint auto-recording:
* the built-in analytics event is only recorded when no handler is supplied,
* which prevents duplicate telemetry for the same state change.
*/
onStateChange?: ServiceWorkerStateChangeHandler;
}
/**
* Provides a means to registering a service worker in the browser
* and communicating with it via postMessage events.
* https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/
*
* postMessage events are currently not supported in all browsers. See:
* https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API
*
* At the minmum this class will register the service worker and listen
* and attempt to dispatch messages on state change and record analytics
* events based on the service worker lifecycle.
*/
export declare class ServiceWorkerClass {
private _serviceWorker?;
private _registration?;
private _publicKey?;
private _subscription?;
private _onStateChange?;
private _logger;
/**
* Get the currently active service worker
*/
get serviceWorker(): ServiceWorker;
/**
* Register the service-worker.js file in the browser
* Make sure the service-worker.js is part of the build
* for example with Angular, modify the angular-cli.json file
* and add to "assets" array "service-worker.js"
*
* Note: when `options.onStateChange` is omitted, this method implicitly
* records service worker lifecycle (`statechange`) events to Amazon
* Pinpoint. That built-in auto-recording is deprecated and will be removed
* in a future major version — only the implicit Pinpoint recording is
* deprecated, not `register()` itself. Provide `options.onStateChange` to
* observe lifecycle state changes and emit vendor-neutral telemetry instead.
* @param {string} filePath Service worker file. Defaults to "/service-worker.js"
* @param {string} scope The service worker scope. Defaults to "/"
* - API Doc: https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register
* @param {ServiceWorkerOptions} [options] Optional registration options. When
* `onStateChange` is provided it is invoked on every service worker state
* change and replaces the built-in Pinpoint auto-recording. It is also
* invoked once with the worker's current state at registration time, so an
* already-active worker (which dispatches no `statechange` event) is still
* observed. This initial emit applies only to `onStateChange`; the built-in
* Pinpoint path is unaffected.
* @returns {Promise}
* - resolve(ServiceWorkerRegistration)
* - reject(Error)
**/
register(filePath?: string, scope?: string, options?: ServiceWorkerOptions): Promise<unknown>;
/**
* Enable web push notifications. If not subscribed, a new subscription will
* be created and registered.
* Test Push Server: https://web-push-codelab.glitch.me/
* Push Server Libraries: https://github.com/web-push-libs/
* API Doc: https://developers.google.com/web/fundamentals/codelabs/push-notifications/
* @param publicKey
* @returns {Promise}
* - resolve(PushSubscription)
* - reject(Error)
*/
enablePush(publicKey: string): Promise<unknown>;
/**
* Convert a base64 encoded string to a Uint8 array for the push server key
* @param base64String
*/
private _urlB64ToUint8Array;
/**
* Send a message to the service worker. The service worker needs
* to implement `self.addEventListener('message') to handle the
* message. This ***currently*** does not work in Safari or IE.
* @param {object | string} message An arbitrary JSON object or string message to send to the service worker
* - see: https://developer.mozilla.org/en-US/docs/Web/API/Transferable
* @returns {Promise}
**/
send(message: object | string): void;
/**
* Listen for service worker state change and message events
* https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/state
*
* Each call to `register()` attaches its own `statechange` listener. The
* `onStateChange` handler is captured in a local at listener-creation time,
* so re-registering with a different handler only affects its own listener
* and never re-targets a previously attached one.
**/
_setupListeners(): void;
/**
* Invoke the consumer `onStateChange` handler with the given state, isolating
* any error it throws (or rejects with, for an async handler) so it cannot
* surface as an unhandled rejection. Awaiting the handler means a rejected
* promise from an async handler is caught here too; `await undefined`
* resolves immediately for sync or absent handlers.
*/
private _notifyStateChange;
}