UNPKG

17.2 kBTypeScriptView Raw
1/**
2 * @license Angular v14.1.2
3 * (c) 2010-2022 Google LLC. https://angular.io/
4 * License: MIT
5 */
6
7
8import * as i0 from '@angular/core';
9import { ModuleWithProviders } from '@angular/core';
10import { Observable } from 'rxjs';
11
12/**
13 * @publicApi
14 */
15declare class NgswCommChannel {
16 private serviceWorker;
17 readonly worker: Observable<ServiceWorker>;
18 readonly registration: Observable<ServiceWorkerRegistration>;
19 readonly events: Observable<TypedEvent>;
20 constructor(serviceWorker: ServiceWorkerContainer | undefined);
21 postMessage(action: string, payload: Object): Promise<void>;
22 postMessageWithOperation(type: string, payload: Object, operationNonce: number): Promise<boolean>;
23 generateNonce(): number;
24 eventsOfType<T extends TypedEvent>(type: T['type'] | T['type'][]): Observable<T>;
25 nextEventOfType<T extends TypedEvent>(type: T['type']): Observable<T>;
26 waitForOperationCompleted(nonce: number): Promise<boolean>;
27 get isEnabled(): boolean;
28}
29
30/**
31 * An event emitted when the service worker has checked the version of the app on the server and it
32 * didn't find a new version that it doesn't have already downloaded.
33 *
34 * @see {@link guide/service-worker-communications Service worker communication guide}
35 *
36 * @publicApi
37 */
38declare interface NoNewVersionDetectedEvent {
39 type: 'NO_NEW_VERSION_DETECTED';
40 version: {
41 hash: string;
42 appData?: Object;
43 };
44}
45
46/**
47 * @publicApi
48 */
49export declare class ServiceWorkerModule {
50 /**
51 * Register the given Angular Service Worker script.
52 *
53 * If `enabled` is set to `false` in the given options, the module will behave as if service
54 * workers are not supported by the browser, and the service worker will not be registered.
55 */
56 static register(script: string, opts?: SwRegistrationOptions): ModuleWithProviders<ServiceWorkerModule>;
57 static ɵfac: i0.ɵɵFactoryDeclaration<ServiceWorkerModule, never>;
58 static ɵmod: i0.ɵɵNgModuleDeclaration<ServiceWorkerModule, never, never, never>;
59 static ɵinj: i0.ɵɵInjectorDeclaration<ServiceWorkerModule>;
60}
61
62/**
63 * Subscribe and listen to
64 * [Web Push
65 * Notifications](https://developer.mozilla.org/en-US/docs/Web/API/Push_API/Best_Practices) through
66 * Angular Service Worker.
67 *
68 * @usageNotes
69 *
70 * You can inject a `SwPush` instance into any component or service
71 * as a dependency.
72 *
73 * <code-example path="service-worker/push/module.ts" region="inject-sw-push"
74 * header="app.component.ts"></code-example>
75 *
76 * To subscribe, call `SwPush.requestSubscription()`, which asks the user for permission.
77 * The call returns a `Promise` with a new
78 * [`PushSubscription`](https://developer.mozilla.org/en-US/docs/Web/API/PushSubscription)
79 * instance.
80 *
81 * <code-example path="service-worker/push/module.ts" region="subscribe-to-push"
82 * header="app.component.ts"></code-example>
83 *
84 * A request is rejected if the user denies permission, or if the browser
85 * blocks or does not support the Push API or ServiceWorkers.
86 * Check `SwPush.isEnabled` to confirm status.
87 *
88 * Invoke Push Notifications by pushing a message with the following payload.
89 *
90 * ```ts
91 * {
92 * "notification": {
93 * "actions": NotificationAction[],
94 * "badge": USVString,
95 * "body": DOMString,
96 * "data": any,
97 * "dir": "auto"|"ltr"|"rtl",
98 * "icon": USVString,
99 * "image": USVString,
100 * "lang": DOMString,
101 * "renotify": boolean,
102 * "requireInteraction": boolean,
103 * "silent": boolean,
104 * "tag": DOMString,
105 * "timestamp": DOMTimeStamp,
106 * "title": DOMString,
107 * "vibrate": number[]
108 * }
109 * }
110 * ```
111 *
112 * Only `title` is required. See `Notification`
113 * [instance
114 * properties](https://developer.mozilla.org/en-US/docs/Web/API/Notification#Instance_properties).
115 *
116 * While the subscription is active, Service Worker listens for
117 * [PushEvent](https://developer.mozilla.org/en-US/docs/Web/API/PushEvent)
118 * occurrences and creates
119 * [Notification](https://developer.mozilla.org/en-US/docs/Web/API/Notification)
120 * instances in response.
121 *
122 * Unsubscribe using `SwPush.unsubscribe()`.
123 *
124 * An application can subscribe to `SwPush.notificationClicks` observable to be notified when a user
125 * clicks on a notification. For example:
126 *
127 * <code-example path="service-worker/push/module.ts" region="subscribe-to-notification-clicks"
128 * header="app.component.ts"></code-example>
129 *
130 * You can read more on handling notification clicks in the [Service worker notifications
131 * guide](guide/service-worker-notifications).
132 *
133 * @see [Push Notifications](https://developers.google.com/web/fundamentals/codelabs/push-notifications/)
134 * @see [Angular Push Notifications](https://blog.angular-university.io/angular-push-notifications/)
135 * @see [MDN: Push API](https://developer.mozilla.org/en-US/docs/Web/API/Push_API)
136 * @see [MDN: Notifications API](https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API)
137 * @see [MDN: Web Push API Notifications best practices](https://developer.mozilla.org/en-US/docs/Web/API/Push_API/Best_Practices)
138 *
139 * @publicApi
140 */
141export declare class SwPush {
142 private sw;
143 /**
144 * Emits the payloads of the received push notification messages.
145 */
146 readonly messages: Observable<object>;
147 /**
148 * Emits the payloads of the received push notification messages as well as the action the user
149 * interacted with. If no action was used the `action` property contains an empty string `''`.
150 *
151 * Note that the `notification` property does **not** contain a
152 * [Notification][Mozilla Notification] object but rather a
153 * [NotificationOptions](https://notifications.spec.whatwg.org/#dictdef-notificationoptions)
154 * object that also includes the `title` of the [Notification][Mozilla Notification] object.
155 *
156 * [Mozilla Notification]: https://developer.mozilla.org/en-US/docs/Web/API/Notification
157 */
158 readonly notificationClicks: Observable<{
159 action: string;
160 notification: NotificationOptions & {
161 title: string;
162 };
163 }>;
164 /**
165 * Emits the currently active
166 * [PushSubscription](https://developer.mozilla.org/en-US/docs/Web/API/PushSubscription)
167 * associated to the Service Worker registration or `null` if there is no subscription.
168 */
169 readonly subscription: Observable<PushSubscription | null>;
170 /**
171 * True if the Service Worker is enabled (supported by the browser and enabled via
172 * `ServiceWorkerModule`).
173 */
174 get isEnabled(): boolean;
175 private pushManager;
176 private subscriptionChanges;
177 constructor(sw: NgswCommChannel);
178 /**
179 * Subscribes to Web Push Notifications,
180 * after requesting and receiving user permission.
181 *
182 * @param options An object containing the `serverPublicKey` string.
183 * @returns A Promise that resolves to the new subscription object.
184 */
185 requestSubscription(options: {
186 serverPublicKey: string;
187 }): Promise<PushSubscription>;
188 /**
189 * Unsubscribes from Service Worker push notifications.
190 *
191 * @returns A Promise that is resolved when the operation succeeds, or is rejected if there is no
192 * active subscription or the unsubscribe operation fails.
193 */
194 unsubscribe(): Promise<void>;
195 private decodeBase64;
196 static ɵfac: i0.ɵɵFactoryDeclaration<SwPush, never>;
197 static ɵprov: i0.ɵɵInjectableDeclaration<SwPush>;
198}
199
200/**
201 * Token that can be used to provide options for `ServiceWorkerModule` outside of
202 * `ServiceWorkerModule.register()`.
203 *
204 * You can use this token to define a provider that generates the registration options at runtime,
205 * for example via a function call:
206 *
207 * {@example service-worker/registration-options/module.ts region="registration-options"
208 * header="app.module.ts"}
209 *
210 * @publicApi
211 */
212export declare abstract class SwRegistrationOptions {
213 /**
214 * Whether the ServiceWorker will be registered and the related services (such as `SwPush` and
215 * `SwUpdate`) will attempt to communicate and interact with it.
216 *
217 * Default: true
218 */
219 enabled?: boolean;
220 /**
221 * A URL that defines the ServiceWorker's registration scope; that is, what range of URLs it can
222 * control. It will be used when calling
223 * [ServiceWorkerContainer#register()](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register).
224 */
225 scope?: string;
226 /**
227 * Defines the ServiceWorker registration strategy, which determines when it will be registered
228 * with the browser.
229 *
230 * The default behavior of registering once the application stabilizes (i.e. as soon as there are
231 * no pending micro- and macro-tasks) is designed to register the ServiceWorker as soon as
232 * possible but without affecting the application's first time load.
233 *
234 * Still, there might be cases where you want more control over when the ServiceWorker is
235 * registered (for example, there might be a long-running timeout or polling interval, preventing
236 * the app from stabilizing). The available option are:
237 *
238 * - `registerWhenStable:<timeout>`: Register as soon as the application stabilizes (no pending
239 * micro-/macro-tasks) but no later than `<timeout>` milliseconds. If the app hasn't
240 * stabilized after `<timeout>` milliseconds (for example, due to a recurrent asynchronous
241 * task), the ServiceWorker will be registered anyway.
242 * If `<timeout>` is omitted, the ServiceWorker will only be registered once the app
243 * stabilizes.
244 * - `registerImmediately`: Register immediately.
245 * - `registerWithDelay:<timeout>`: Register with a delay of `<timeout>` milliseconds. For
246 * example, use `registerWithDelay:5000` to register the ServiceWorker after 5 seconds. If
247 * `<timeout>` is omitted, is defaults to `0`, which will register the ServiceWorker as soon
248 * as possible but still asynchronously, once all pending micro-tasks are completed.
249 * - An [Observable](guide/observables) factory function: A function that returns an `Observable`.
250 * The function will be used at runtime to obtain and subscribe to the `Observable` and the
251 * ServiceWorker will be registered as soon as the first value is emitted.
252 *
253 * Default: 'registerWhenStable:30000'
254 */
255 registrationStrategy?: string | (() => Observable<unknown>);
256}
257
258/**
259 * Subscribe to update notifications from the Service Worker, trigger update
260 * checks, and forcibly activate updates.
261 *
262 * @see {@link guide/service-worker-communications Service worker communication guide}
263 *
264 * @publicApi
265 */
266export declare class SwUpdate {
267 private sw;
268 /**
269 * Emits a `VersionDetectedEvent` event whenever a new version is detected on the server.
270 *
271 * Emits a `VersionInstallationFailedEvent` event whenever checking for or downloading a new
272 * version fails.
273 *
274 * Emits a `VersionReadyEvent` event whenever a new version has been downloaded and is ready for
275 * activation.
276 */
277 readonly versionUpdates: Observable<VersionEvent>;
278 /**
279 * Emits an `UpdateAvailableEvent` event whenever a new app version is available.
280 *
281 * @deprecated Use {@link versionUpdates} instead.
282 *
283 * The of behavior `available` can be rebuild by filtering for the `VersionReadyEvent`:
284 * ```
285 * import {filter, map} from 'rxjs/operators';
286 * // ...
287 * const updatesAvailable = swUpdate.versionUpdates.pipe(
288 * filter((evt): evt is VersionReadyEvent => evt.type === 'VERSION_READY'),
289 * map(evt => ({
290 * type: 'UPDATE_AVAILABLE',
291 * current: evt.currentVersion,
292 * available: evt.latestVersion,
293 * })));
294 * ```
295 */
296 readonly available: Observable<UpdateAvailableEvent>;
297 /**
298 * Emits an `UpdateActivatedEvent` event whenever the app has been updated to a new version.
299 *
300 * @deprecated Use the return value of {@link SwUpdate#activateUpdate} instead.
301 *
302 */
303 readonly activated: Observable<UpdateActivatedEvent>;
304 /**
305 * Emits an `UnrecoverableStateEvent` event whenever the version of the app used by the service
306 * worker to serve this client is in a broken state that cannot be recovered from without a full
307 * page reload.
308 */
309 readonly unrecoverable: Observable<UnrecoverableStateEvent>;
310 /**
311 * True if the Service Worker is enabled (supported by the browser and enabled via
312 * `ServiceWorkerModule`).
313 */
314 get isEnabled(): boolean;
315 constructor(sw: NgswCommChannel);
316 /**
317 * Checks for an update and waits until the new version is downloaded from the server and ready
318 * for activation.
319 *
320 * @returns a promise that
321 * - resolves to `true` if a new version was found and is ready to be activated.
322 * - resolves to `false` if no new version was found
323 * - rejects if any error occurs
324 */
325 checkForUpdate(): Promise<boolean>;
326 /**
327 * Updates the current client (i.e. browser tab) to the latest version that is ready for
328 * activation.
329 *
330 * @returns a promise that
331 * - resolves to `true` if an update was activated successfully
332 * - resolves to `false` if no update was available (for example, the client was already on the
333 * latest version).
334 * - rejects if any error occurs
335 */
336 activateUpdate(): Promise<boolean>;
337 static ɵfac: i0.ɵɵFactoryDeclaration<SwUpdate, never>;
338 static ɵprov: i0.ɵɵInjectableDeclaration<SwUpdate>;
339}
340
341declare interface TypedEvent {
342 type: string;
343}
344
345/**
346 * An event emitted when the version of the app used by the service worker to serve this client is
347 * in a broken state that cannot be recovered from and a full page reload is required.
348 *
349 * For example, the service worker may not be able to retrieve a required resource, neither from the
350 * cache nor from the server. This could happen if a new version is deployed to the server and the
351 * service worker cache has been partially cleaned by the browser, removing some files of a previous
352 * app version but not all.
353 *
354 * @see {@link guide/service-worker-communications Service worker communication guide}
355 *
356 * @publicApi
357 */
358export declare interface UnrecoverableStateEvent {
359 type: 'UNRECOVERABLE_STATE';
360 reason: string;
361}
362
363/**
364 * An event emitted when a new version of the app has been downloaded and activated.
365 *
366 * @see {@link guide/service-worker-communications Service worker communication guide}
367 *
368 * @deprecated
369 * This event is only emitted by the deprecated {@link SwUpdate#activated}.
370 * Use the return value of {@link SwUpdate#activateUpdate} instead.
371 *
372 * @publicApi
373 */
374export declare interface UpdateActivatedEvent {
375 type: 'UPDATE_ACTIVATED';
376 previous?: {
377 hash: string;
378 appData?: Object;
379 };
380 current: {
381 hash: string;
382 appData?: Object;
383 };
384}
385
386/**
387 * An event emitted when a new version of the app is available.
388 *
389 * @see {@link guide/service-worker-communications Service worker communication guide}
390 *
391 * @deprecated
392 * This event is only emitted by the deprecated {@link SwUpdate#available}.
393 * Use the {@link VersionReadyEvent} instead, which is emitted by {@link SwUpdate#versionUpdates}.
394 * See {@link SwUpdate#available} docs for an example.
395 *
396 * @publicApi
397 */
398export declare interface UpdateAvailableEvent {
399 type: 'UPDATE_AVAILABLE';
400 current: {
401 hash: string;
402 appData?: Object;
403 };
404 available: {
405 hash: string;
406 appData?: Object;
407 };
408}
409
410/**
411 * An event emitted when the service worker has detected a new version of the app on the server and
412 * is about to start downloading it.
413 *
414 * @see {@link guide/service-worker-communications Service worker communication guide}
415 *
416 * @publicApi
417 */
418export declare interface VersionDetectedEvent {
419 type: 'VERSION_DETECTED';
420 version: {
421 hash: string;
422 appData?: object;
423 };
424}
425
426/**
427 * A union of all event types that can be emitted by
428 * {@link api/service-worker/SwUpdate#versionUpdates SwUpdate#versionUpdates}.
429 *
430 * @publicApi
431 */
432export declare type VersionEvent = VersionDetectedEvent | VersionInstallationFailedEvent | VersionReadyEvent | NoNewVersionDetectedEvent;
433
434/**
435 * An event emitted when the installation of a new version failed.
436 * It may be used for logging/monitoring purposes.
437 *
438 * @see {@link guide/service-worker-communications Service worker communication guide}
439 *
440 * @publicApi
441 */
442export declare interface VersionInstallationFailedEvent {
443 type: 'VERSION_INSTALLATION_FAILED';
444 version: {
445 hash: string;
446 appData?: object;
447 };
448 error: string;
449}
450
451/**
452 * An event emitted when a new version of the app is available.
453 *
454 * @see {@link guide/service-worker-communications Service worker communication guide}
455 *
456 * @publicApi
457 */
458export declare interface VersionReadyEvent {
459 type: 'VERSION_READY';
460 currentVersion: {
461 hash: string;
462 appData?: object;
463 };
464 latestVersion: {
465 hash: string;
466 appData?: object;
467 };
468}
469
470export { }
471
\No newline at end of file