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

Simplify Effects #2043

Closed
alvipeo opened this issue Aug 5, 2019 · 1 comment
Closed

Simplify Effects #2043

alvipeo opened this issue Aug 5, 2019 · 1 comment

Comments

@alvipeo
Copy link

alvipeo commented Aug 5, 2019

ngrx 8.2.0, angular 8.2.0

I have these kind of effects all over the place:

loadPageBlocksApi$ = createEffect(() =>
    this.actions$.pipe(
      ofType(loadPageBlocksApi),
      concatMap(action =>
        of(action).pipe(
          withLatestFrom(this.store.pipe(select(fromLangs.getCurrentLang)))
        )
      ),
      filter(([, currentLang]) => !!currentLang),
      map(([, currentLang]) => currentLang as Language),
      exhaustMap(currentLang => [
        of(fromInProgress.startWorkInProgress()),
        this.pageBlockSvc
          .getByCulture(currentLang.cultureName)
          .pipe(map(pageBlocks => addPageBlocks({ pageBlocks })))
      ]),
      concatAll(),
      catchError(() =>
        of(savingFailed({ errorMsg: "Could not load pages information" }))
      )
    )
  );

// another one, for example
upsertBasicInfo$ = createEffect(() =>
    this.actions$.pipe(
      ofType(storeBasicInfo),
      exhaustMap(action => [
        of(fromInProgress.startWorkInProgress()),
        this.pageBlockSvc
          .storeBasicInfo(action.pbInfo)
          .pipe(map(pageBlock => upsertPageBlock({ pageBlock })))
      ]),
      concatAll(),
      catchError(() =>
        of(savingFailed({ errorMsg: "Could not save page info" }))
      )
    )
  );

They're all first emit startWorkingProgress action to show global working UI state and then they actually do the work and emit resulting actions. But I was thinking if we could have some simplified method for it, like:

loadPageBlocksApi$ = createEffect(
  loadPageBlocksApi,                          // action type
  select(fromLangs.getCurrentLang),  // state selector (optional)
  (actionProps, stateSelectorResult) => { ... some pre-processing },
  [ one_or_more_actions_to_dispatch... ]                   // actions to dispatch (and their params are set based on the previous func results
);

then we wouldn't probably deal with all these RxJS multiple operators.

Well, just an idea - to create a simplified syntax for splitters and maybe aggregators.
@timdeschryver
Copy link
Member

I think this case is too specific, but we're working on it:

That being said, you can write your own higher order effect to handle state selectors and pre-processing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants