feat(example): add examples of effects not based on the Actions stream#1845
Conversation
|
When you have time, let me know if this is close to what you had in mind :) |
|
Preview docs changes for 199a37e at https://previews.ngrx.io/pr1845-199a37e/ |
timdeschryver
left a comment
There was a problem hiding this comment.
LGTM 👍
Should we create some tests for these changes?
|
Another idea that pops to my mind would be to use google analytics. |
|
An example that still uses the actions but are not dispatching an action back to the store: |
| @@ -0,0 +1,3 @@ | |||
| import { createAction } from '@ngrx/store'; | |||
|
|
|||
| export const idle = createAction('[User] Idle'); | |||
There was a problem hiding this comment.
| export const idle = createAction('[User] Idle'); | |
| export const idleTimeout = createAction('[User] Idle Timeout'); |
| () => | ||
| this.router.events.pipe( | ||
| filter(event => event instanceof NavigationEnd), | ||
| map(() => this.activatedRoute), |
There was a problem hiding this comment.
Combine the 2 maps into 1
| return route; | ||
| }), | ||
| mergeMap(route => route.data), | ||
| map(data => data['title']), |
There was a problem hiding this comment.
| map(data => data['title']), | |
| map(data => `Book Collection - ${data['title'])}`, |
| @@ -0,0 +1,32 @@ | |||
| import { Injectable } from '@angular/core'; | |||
| import { createEffect } from '@ngrx/effects'; | |||
| import { Router, NavigationEnd, ActivatedRoute } from '@angular/router'; | |||
There was a problem hiding this comment.
Move all @angular imports to the top of the file, following by rxjs and @ngrx
|
|
||
| idle$ = createEffect(() => | ||
| merge(this.clicks$, this.keys$, this.mouse$).pipe( | ||
| switchMapTo(timer(60 * 1000)), |
There was a problem hiding this comment.
| switchMapTo(timer(60 * 1000)), | |
| switchMapTo(timer(5 * 60 * 1000)), // 5 minute inactivity timeout |
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 ngrx#1830
199a37e to
38784a9
Compare
|
Updated with all the requested changes and added tests for user and router effects. |
|
Preview docs changes for 38784a9 at https://previews.ngrx.io/pr1845-38784a9/ |
| export class RouterEffects { | ||
| updateTitle$ = createEffect( | ||
| () => | ||
| this.router.events.pipe( |
There was a problem hiding this comment.
We have store-router package installed in the example app. Wouldn't that be better to listen to those changes?
@brandonroberts
There was a problem hiding this comment.
I thought about that when I was writing the code :) The idea was to show how one would create effects not based on the Actions stream, so I went for this instead.
There was a problem hiding this comment.
I think these are good for what we asked for
|
Preview docs changes for 524c21e at https://previews.ngrx.io/pr1845-524c21e/ |
As suggested, added two examples of effects not based on Actions stream:
Closes #1830
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Closes #1830
What is the new behavior?
Does this PR introduce a breaking change?
Other information