Skip to content

Commit

Permalink
refactor(notification): new utility provideSwPush
Browse files Browse the repository at this point in the history
  • Loading branch information
nsbarsukov authored and splincode committed Nov 16, 2023
1 parent 893b757 commit c683182
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 46 deletions.
4 changes: 3 additions & 1 deletion apps/demo/src/app/app.browser.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import {LocationStrategy, PathLocationStrategy} from '@angular/common';
import {NgModule, SecurityContext} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {ServiceWorkerModule} from '@angular/service-worker';
import {ServiceWorkerModule, SwPush} from '@angular/service-worker';
import {POSITION_OPTIONS} from '@ng-web-apis/geolocation';
import {provideSwPush} from '@ng-web-apis/notification';
import {TuiLinkModule, TuiRootModule, TuiSvgModule} from '@taiga-ui/core';
import {HIGHLIGHT_OPTIONS, HighlightModule} from 'ngx-highlightjs';
import {MarkdownModule} from 'ngx-markdown';
Expand Down Expand Up @@ -35,6 +36,7 @@ import {AppRoutingModule} from './app.routes';
declarations: [AppComponent],
bootstrap: [AppComponent],
providers: [
provideSwPush(SwPush),
{
provide: HIGHLIGHT_OPTIONS,
useValue: {fullLibraryLoader: () => import('highlight.js')},
Expand Down
1 change: 1 addition & 0 deletions libs/notification/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
export * from './tokens/support';
export * from './tokens/notification-factory';
export * from './services/notification.service';
export * from './utils/provide-sw-push';
12 changes: 4 additions & 8 deletions libs/notification/src/tokens/notification-clicks.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import {inject, InjectFlags, InjectionToken} from '@angular/core';
import {SwPush} from '@angular/service-worker';
import {InjectionToken} from '@angular/core';
import type {SwPush} from '@angular/service-worker';
import {NEVER} from 'rxjs';

export const NOTIFICATION_SW_CLICKS = new InjectionToken(
export const NOTIFICATION_SW_CLICKS = new InjectionToken<SwPush['notificationClicks']>(
`Global listener for events when ANY system notification spawned by Notification API (and only inside Service Worker!) has been clicked`,
{
factory: () => {
const swPush = inject(SwPush, InjectFlags.Optional);

return swPush?.isEnabled ? swPush.notificationClicks : NEVER;
},
factory: () => NEVER,
},
);
41 changes: 4 additions & 37 deletions libs/notification/src/tokens/notification-closes.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,9 @@
import {inject, InjectionToken, NgZone} from '@angular/core';
import {ANIMATION_FRAME, SERVICE_WORKER, zoneOptimized} from '@ng-web-apis/common';
import {
combineLatest,
filter,
from,
map,
NEVER,
Observable,
pairwise,
share,
switchMap,
} from 'rxjs';
import {InjectionToken} from '@angular/core';
import {NEVER, Observable} from 'rxjs';

export const NOTIFICATION_SW_CLOSES = new InjectionToken(
export const NOTIFICATION_SW_CLOSES = new InjectionToken<Observable<Notification>>(
`Global listener for events when ANY system notification spawned by Notification API (and only inside Service Worker!) has been closed`,
{
/**
* TODO: refactor the token's factory after this issue will be solved:
* https://github.com/angular/angular/issues/52244
* ```
* const swPush = inject(SwPush, InjectFlags.Optional);
* return swPush && swPush.isEnabled ? swPush.notificationCloses : NEVER;
* ```
*/
factory: (): Observable<Notification> =>
combineLatest([
from(inject(SERVICE_WORKER).getRegistration()),
inject(ANIMATION_FRAME),
]).pipe(
switchMap(([reg]) => (reg ? from(reg.getNotifications()) : NEVER)),
pairwise(),
filter(([prev, cur]) => prev.length > cur.length),
map(
([prev, cur]) =>
prev.find((notification, i) => notification !== cur[i])!,
),
zoneOptimized(inject(NgZone)),
share(),
),
factory: () => NEVER,
},
);
45 changes: 45 additions & 0 deletions libs/notification/src/utils/provide-sw-push.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {inject, NgZone, Provider, Type} from '@angular/core';
import type {SwPush} from '@angular/service-worker';
import {ANIMATION_FRAME, SERVICE_WORKER, zoneOptimized} from '@ng-web-apis/common';
import {combineLatest, filter, from, map, NEVER, pairwise, share, switchMap} from 'rxjs';
import {NOTIFICATION_SW_CLICKS} from '../tokens/notification-clicks';
import {NOTIFICATION_SW_CLOSES} from '../tokens/notification-closes';

export function provideSwPush(swPush: Type<SwPush>): Provider[] {
return [
{
provide: NOTIFICATION_SW_CLICKS,
deps: [swPush],
useFactory: ({isEnabled, notificationClicks}: SwPush) =>
isEnabled ? notificationClicks : NEVER,
},
{
provide: NOTIFICATION_SW_CLOSES,
/**
* TODO: refactor the token's factory after this issue will be solved:
* https://github.com/angular/angular/issues/52244
* ```
* {
* provide: NOTIFICATION_SW_CLOSES,
* useValue: swPush.isEnabled ? swPush.notificationCloses : NEVER,
* },
* ```
*/
useFactory: () =>
combineLatest([
from(inject(SERVICE_WORKER).getRegistration()),
inject(ANIMATION_FRAME),
]).pipe(
switchMap(([reg]) => (reg ? from(reg.getNotifications()) : NEVER)),
pairwise(),
filter(([prev, cur]) => prev.length > cur.length),
map(
([prev, cur]) =>
prev.find((notification, i) => notification !== cur[i])!,
),
zoneOptimized(inject(NgZone)),
share(),
),
},
];
}

0 comments on commit c683182

Please sign in to comment.