-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(notification): new utility
provideSwPush
- Loading branch information
1 parent
017bd04
commit a611f17
Showing
5 changed files
with
57 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
), | ||
}, | ||
]; | ||
} |