Skip to content

feat(example): add examples of effects not based on the Actions stream#1845

Merged
brandonroberts merged 2 commits into
ngrx:masterfrom
adrianfaciu:example/non-actions-effects
May 21, 2019
Merged

feat(example): add examples of effects not based on the Actions stream#1845
brandonroberts merged 2 commits into
ngrx:masterfrom
adrianfaciu:example/non-actions-effects

Conversation

@adrianfaciu

Copy link
Copy Markdown

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
Copy Markdown
Author

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

@ngrxbot

ngrxbot commented May 9, 2019

Copy link
Copy Markdown
Collaborator

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

@timdeschryver timdeschryver left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 adrianfaciu force-pushed the example/non-actions-effects branch from 199a37e to 38784a9 Compare May 15, 2019 05:51
@adrianfaciu

Copy link
Copy Markdown
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

ngrxbot commented May 15, 2019

Copy link
Copy Markdown
Collaborator

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

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

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

ngrxbot commented May 20, 2019

Copy link
Copy Markdown
Collaborator

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