Skip to content

Commit a611f17

Browse files
committed
refactor(notification): new utility provideSwPush
1 parent 017bd04 commit a611f17

File tree

5 files changed

+57
-46
lines changed

5 files changed

+57
-46
lines changed

apps/demo/src/app/app.browser.module.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import {LocationStrategy, PathLocationStrategy} from '@angular/common';
22
import {NgModule, SecurityContext} from '@angular/core';
33
import {BrowserModule} from '@angular/platform-browser';
44
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
5-
import {ServiceWorkerModule} from '@angular/service-worker';
5+
import {ServiceWorkerModule, SwPush} from '@angular/service-worker';
66
import {POSITION_OPTIONS} from '@ng-web-apis/geolocation';
7+
import {provideSwPush} from '@ng-web-apis/notification';
78
import {TuiLinkModule, TuiRootModule, TuiSvgModule} from '@taiga-ui/core';
89
import {HIGHLIGHT_OPTIONS, HighlightModule} from 'ngx-highlightjs';
910
import {MarkdownModule} from 'ngx-markdown';
@@ -35,6 +36,7 @@ import {AppRoutingModule} from './app.routes';
3536
declarations: [AppComponent],
3637
bootstrap: [AppComponent],
3738
providers: [
39+
provideSwPush(SwPush),
3840
{
3941
provide: HIGHLIGHT_OPTIONS,
4042
useValue: {fullLibraryLoader: () => import('highlight.js')},

libs/notification/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
export * from './tokens/support';
66
export * from './tokens/notification-factory';
77
export * from './services/notification.service';
8+
export * from './utils/provide-sw-push';
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
import {inject, InjectFlags, InjectionToken} from '@angular/core';
2-
import {SwPush} from '@angular/service-worker';
1+
import {InjectionToken} from '@angular/core';
2+
import type {SwPush} from '@angular/service-worker';
33
import {NEVER} from 'rxjs';
44

5-
export const NOTIFICATION_SW_CLICKS = new InjectionToken(
5+
export const NOTIFICATION_SW_CLICKS = new InjectionToken<SwPush['notificationClicks']>(
66
`Global listener for events when ANY system notification spawned by Notification API (and only inside Service Worker!) has been clicked`,
77
{
8-
factory: () => {
9-
const swPush = inject(SwPush, InjectFlags.Optional);
10-
11-
return swPush?.isEnabled ? swPush.notificationClicks : NEVER;
12-
},
8+
factory: () => NEVER,
139
},
1410
);
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,9 @@
1-
import {inject, InjectionToken, NgZone} from '@angular/core';
2-
import {ANIMATION_FRAME, SERVICE_WORKER, zoneOptimized} from '@ng-web-apis/common';
3-
import {
4-
combineLatest,
5-
filter,
6-
from,
7-
map,
8-
NEVER,
9-
Observable,
10-
pairwise,
11-
share,
12-
switchMap,
13-
} from 'rxjs';
1+
import {InjectionToken} from '@angular/core';
2+
import {NEVER, Observable} from 'rxjs';
143

15-
export const NOTIFICATION_SW_CLOSES = new InjectionToken(
4+
export const NOTIFICATION_SW_CLOSES = new InjectionToken<Observable<Notification>>(
165
`Global listener for events when ANY system notification spawned by Notification API (and only inside Service Worker!) has been closed`,
176
{
18-
/**
19-
* TODO: refactor the token's factory after this issue will be solved:
20-
* https://github.com/angular/angular/issues/52244
21-
* ```
22-
* const swPush = inject(SwPush, InjectFlags.Optional);
23-
* return swPush && swPush.isEnabled ? swPush.notificationCloses : NEVER;
24-
* ```
25-
*/
26-
factory: (): Observable<Notification> =>
27-
combineLatest([
28-
from(inject(SERVICE_WORKER).getRegistration()),
29-
inject(ANIMATION_FRAME),
30-
]).pipe(
31-
switchMap(([reg]) => (reg ? from(reg.getNotifications()) : NEVER)),
32-
pairwise(),
33-
filter(([prev, cur]) => prev.length > cur.length),
34-
map(
35-
([prev, cur]) =>
36-
prev.find((notification, i) => notification !== cur[i])!,
37-
),
38-
zoneOptimized(inject(NgZone)),
39-
share(),
40-
),
7+
factory: () => NEVER,
418
},
429
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {inject, NgZone, Provider, Type} from '@angular/core';
2+
import type {SwPush} from '@angular/service-worker';
3+
import {ANIMATION_FRAME, SERVICE_WORKER, zoneOptimized} from '@ng-web-apis/common';
4+
import {combineLatest, filter, from, map, NEVER, pairwise, share, switchMap} from 'rxjs';
5+
import {NOTIFICATION_SW_CLICKS} from '../tokens/notification-clicks';
6+
import {NOTIFICATION_SW_CLOSES} from '../tokens/notification-closes';
7+
8+
export function provideSwPush(swPush: Type<SwPush>): Provider[] {
9+
return [
10+
{
11+
provide: NOTIFICATION_SW_CLICKS,
12+
deps: [swPush],
13+
useFactory: ({isEnabled, notificationClicks}: SwPush) =>
14+
isEnabled ? notificationClicks : NEVER,
15+
},
16+
{
17+
provide: NOTIFICATION_SW_CLOSES,
18+
/**
19+
* TODO: refactor the token's factory after this issue will be solved:
20+
* https://github.com/angular/angular/issues/52244
21+
* ```
22+
* {
23+
* provide: NOTIFICATION_SW_CLOSES,
24+
* useValue: swPush.isEnabled ? swPush.notificationCloses : NEVER,
25+
* },
26+
* ```
27+
*/
28+
useFactory: () =>
29+
combineLatest([
30+
from(inject(SERVICE_WORKER).getRegistration()),
31+
inject(ANIMATION_FRAME),
32+
]).pipe(
33+
switchMap(([reg]) => (reg ? from(reg.getNotifications()) : NEVER)),
34+
pairwise(),
35+
filter(([prev, cur]) => prev.length > cur.length),
36+
map(
37+
([prev, cur]) =>
38+
prev.find((notification, i) => notification !== cur[i])!,
39+
),
40+
zoneOptimized(inject(NgZone)),
41+
share(),
42+
),
43+
},
44+
];
45+
}

0 commit comments

Comments
 (0)