NGRX:聆听按特定顺序派遣的动作链是一个反对者吗?

发布于 2025-02-08 10:10:19 字数 853 浏览 2 评论 0原文

我发现以下模式有用和通用:

  effectXY$ = createEffect(() =>
    this.actions$.pipe(
      ofType(actionX, actionY),
      switchMap(() =>
        this.myApi.something()
          .pipe(
            map(() => actionZ())
          )
      )
    )
  );

  effectXZ$ = createEffect(() =>
    this.actions$.pipe(
      ofType(
        actionX,
        actionZ
      ),
      pairwise(),
      // wait for both actions to be dispatched in the correct order
      filter( ([prevAction, currentAction]) =>
          prevAction.type === actionX.type &&
          currentAction.type === actionZ.type
      ),
      map(() => actionA())
    )
  );

发生的事情是,actiona仅在actionxactionz 才被调用。 。这样做我还避免创建许多其他动作来模仿这种模式,但是我错过了这可能会导致

编辑的任何含义:提醒您没有保证这些动作是连续的(没有其他动作在介于两者之间)

I find the following pattern usefull and versatile:

  effectXY$ = createEffect(() =>
    this.actions$.pipe(
      ofType(actionX, actionY),
      switchMap(() =>
        this.myApi.something()
          .pipe(
            map(() => actionZ())
          )
      )
    )
  );

  effectXZ$ = createEffect(() =>
    this.actions$.pipe(
      ofType(
        actionX,
        actionZ
      ),
      pairwise(),
      // wait for both actions to be dispatched in the correct order
      filter( ([prevAction, currentAction]) =>
          prevAction.type === actionX.type &&
          currentAction.type === actionZ.type
      ),
      map(() => actionA())
    )
  );

What is happening is that actionA is dispatchen only if actionX and actionZ have been dispatched in that specific order. Doing this i also avoid to create lots of other actions to mimic this pattern, but i miss whatever implications this might lead to

edit: reminder that there is no guarantee that the actions are consecutive (with no other actions being dispatched in between)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄风 2025-02-15 10:10:19

这里没有明显的反诉讼。您正在按书做所有事情。

您可以使用效果来观察操作$流,使用RXJS操作员从中挑选的RXJS操作员是一种与您的条件匹配的特定事件,并在发生该事件时返回操作。

如果没有更多关于您实际试图使用此代码在应用程序中完成的工作的背景,我看不到从原始理论的角度来看的理由来批评它。

There is no obvious anti-pattern here. You're doing everything by the book.

You use your Effect to observe the actions$ stream, using RxJs operators you pick from it a specific kind of event that matches your criteria, and return an Action when that event happens.

Without more context as to what you're actually trying to accomplish in your app with this code, I don't see grounds to criticize it from the raw theory standpoint.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文