-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(example): add examples of effects not based on the Actions stream #1845
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/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Combine the 2 maps into 1
return route; | ||
}), | ||
mergeMap(route => route.data), | ||
map(data => data['title']), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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