Skip to content
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

Merged
merged 2 commits into from
May 21, 2019

Conversation

adrianfaciu
Copy link

As suggested, added two examples of effects not based on Actions stream:

  • listen for router navigation events and update the page title
  • log out the user after a specified period of inactivity

Closes #1830

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

Closes #1830

What is the new behavior?

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

@adrianfaciu
Copy link
Author

When you have time, let me know if this is close to what you had in mind :)

@ngrxbot
Copy link
Collaborator

ngrxbot commented May 9, 2019

Preview docs changes for 199a37e at https://previews.ngrx.io/pr1845-199a37e/

Copy link
Member

@timdeschryver timdeschryver left a 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?

@timdeschryver
Copy link
Member

Another idea that pops to my mind would be to use google analytics.
We could NgRx-ify Stephen's post https://fluin.io/blog/google-analytics-with-tag-manager-and-angular

@timdeschryver
Copy link
Member

An example that still uses the actions but are not dispatching an action back to the store:
Showing a snackbar when a book has been added/removed.

@@ -0,0 +1,3 @@
import { createAction } from '@ngrx/store';

export const idle = createAction('[User] Idle');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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),
Copy link
Member

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']),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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';
Copy link
Member

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)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
@adrianfaciu
Copy link
Author

Updated with all the requested changes and added tests for user and router effects.
If we need more examples, I can add some effects for the snackbar and look into google analytics.

@ngrxbot
Copy link
Collaborator

ngrxbot commented May 15, 2019

Preview docs changes for 38784a9 at https://previews.ngrx.io/pr1845-38784a9/

export class RouterEffects {
updateTitle$ = createEffect(
() =>
this.router.events.pipe(
Copy link
Member

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

Copy link
Author

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.

Copy link
Member

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

@ngrxbot
Copy link
Collaborator

ngrxbot commented May 20, 2019

Preview docs changes for 524c21e at https://previews.ngrx.io/pr1845-524c21e/

@brandonroberts brandonroberts merged commit 3454e70 into ngrx:master May 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Example: Add examples of effects not based on the Actions stream
5 participants