-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(example): add examples of effects not based on the Actions stream
As suggested, added two examples of effects not based on Actions stream: - listen for router navigation events and update page title - log out the user after a specified period of inactivity Closes #1830
- Loading branch information
Adrian Fâciu
committed
May 8, 2019
1 parent
eec9cc6
commit 199a37e
Showing
10 changed files
with
94 additions
and
8 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
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,3 +1,4 @@ | ||
import * as LayoutActions from './layout.actions'; | ||
import * as UserActions from './user.actions'; | ||
|
||
export { LayoutActions }; | ||
export { LayoutActions, UserActions }; |
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,3 @@ | ||
import { createAction } from '@ngrx/store'; | ||
|
||
export const idle = createAction('[User] Idle'); |
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,2 @@ | ||
export * from './user.effects'; | ||
export * from './router.effects'; |
32 changes: 32 additions & 0 deletions
32
projects/example-app/src/app/core/effects/router.effects.ts
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,32 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { createEffect } from '@ngrx/effects'; | ||
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router'; | ||
import { Title } from '@angular/platform-browser'; | ||
import { tap, filter, map, mergeMap } from 'rxjs/operators'; | ||
|
||
@Injectable() | ||
export class RouterEffects { | ||
updateTitle$ = createEffect( | ||
() => | ||
this.router.events.pipe( | ||
filter(event => event instanceof NavigationEnd), | ||
map(() => this.activatedRoute), | ||
map(route => { | ||
while (route.firstChild) route = route.firstChild; | ||
return route; | ||
}), | ||
mergeMap(route => route.data), | ||
map(data => data['title']), | ||
tap(title => this.titleService.setTitle(title)) | ||
), | ||
{ | ||
dispatch: false, | ||
} | ||
); | ||
|
||
constructor( | ||
private router: Router, | ||
private titleService: Title, | ||
private activatedRoute: ActivatedRoute | ||
) {} | ||
} |
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,21 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { createEffect } from '@ngrx/effects'; | ||
|
||
import { fromEvent, merge, timer } from 'rxjs'; | ||
import { map, switchMapTo } from 'rxjs/operators'; | ||
|
||
import { UserActions } from '@example-app/core/actions'; | ||
|
||
@Injectable() | ||
export class UserEffects { | ||
clicks$ = fromEvent(document, 'click'); | ||
keys$ = fromEvent(document, 'keydown'); | ||
mouse$ = fromEvent(document, 'mousemove'); | ||
|
||
idle$ = createEffect(() => | ||
merge(this.clicks$, this.keys$, this.mouse$).pipe( | ||
switchMapTo(timer(60 * 1000)), | ||
map(() => UserActions.idle()) | ||
) | ||
); | ||
} |