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(effects): add ability to create functional effects #3669

Merged
merged 5 commits into from
Jan 25, 2023

Conversation

markostanimirovic
Copy link
Member

@markostanimirovic markostanimirovic commented Nov 19, 2022

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

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

What is the current behavior?

We are not able to create effects outside the effects classes.

Closes #3668

What is the new behavior?

We are able to create effects outside the effects class by adding functional: true flag within the effect config.

Examples:

// (file: users.effects.ts)

// mapping to a different action
export const loadUsers = createEffect(
  (actions$ = inject(Actions), usersService = inject(UsersService)) => {
    return actions$.pipe(
      ofType(UsersPageActions.opened),
      exhaustMap(() => {
        return usersService.getAll().pipe(
          map((users) => UsersApiActions.usersLoadedSuccess({ users })),
          catchError((error) =>
            of(UsersApiActions.usersLoadedFailure({ error }))
          )
        );
      })
    );
  },
  { functional: true }
);

// non-dispatching functional effect
export const logDispatchedActions = createEffect(
  () => inject(Actions).pipe(tap(console.log)),
  { functional: true, dispatch: false }
);

Functional effects can be registered by using the provideEffects function as follows:

import * as usersEffects from './users.effects';

bootstrapApplication(AppComponent, {
  providers: [provideEffects(usersEffects)],
});

Functional effects can be easily tested without TestBed:

// marble test
const result$ = usersEffects.loadUsers(actionsMock, usersServiceMock);
expect(result$).toBeObservable(/* ... */);
expect(usersServiceMock, 'getAll').toHaveBeenCalled();

// imperative test
usersEffects.loadUsers(actionsMock, usersServiceMock).subscribe((action) => {
  expect(action).toEqual(/* ... */);
  expect(usersServiceMock, 'getAll').toHaveBeenCalled();
  done();
});

Does this PR introduce a breaking change?

[ ] Yes
[x] No

@netlify
Copy link

netlify bot commented Nov 19, 2022

Deploy Preview for ngrx-io canceled.

Name Link
🔨 Latest commit 7ff7663
🔍 Latest deploy log https://app.netlify.com/sites/ngrx-io/deploys/63caa33dc7bf8a0008dff61f

@markostanimirovic markostanimirovic force-pushed the feat/effects/functional-effects branch 2 times, most recently from a892070 to 2e435c0 Compare November 20, 2022 21:40
@markostanimirovic markostanimirovic marked this pull request as ready for review November 20, 2022 21:40
Copy link
Member

@brandonroberts brandonroberts left a comment

Choose a reason for hiding this comment

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

🛳️

@sheikalthaf
Copy link
Contributor

Y it is still not merged? I love using functions

modules/effects/spec/effect_creator.spec.ts Outdated Show resolved Hide resolved
modules/effects/src/tokens.ts Outdated Show resolved Hide resolved
modules/effects/src/tokens.ts Outdated Show resolved Hide resolved
@brandonroberts brandonroberts merged commit dd76c63 into master Jan 25, 2023
@brandonroberts brandonroberts deleted the feat/effects/functional-effects branch January 25, 2023 21:51
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.

RFC: Add ability to create functional effects
4 participants