RxJS iif 参数何时不应该被调用 [英] RxJS iif arguments are called when shouldn't

查看:46
本文介绍了RxJS iif 参数何时不应该被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用来自 RxJS 的 iif 实用程序有条件地分派一些操作.问题是即使测试函数返回 false,也会调用 iif 的第二个参数.这会引发错误并且应用程序立即崩溃.我对 RxJS 的功能不熟悉,所以我可能不知道什么.如果重要的话,我正在使用 connected-react-router 包.

<前>export const roomRouteEpic: Epic = (action$, state$) =>action$.ofType(LOCATION_CHANGE).pipe(采摘('有效载荷'),合并地图(有效载荷 =>如果(() => {console.log('未记录');return/^\/room\/\d+$/.test(payload.location.pathname);//设置为/登录"},合并(tap(v => console.log('NOT LOGGED TOO')),的(//立即评估随后的状态值state$.value.rooms.list[payload.location.pathname.split('/')[1]]?actions.rooms.initRoomEnter():actions.rooms.initRoomCreate(),),of(actions.global.setIsLoading(true)),),空的(),),),);

解决方案

好的,我自己找到了答案.我的解决方案是完全删除 iif 并仅依赖于 mergeMap 中的三元运算符.这样它就不会在每次LOCATION_CHANGE"之后进行评估,并且仅当 regExp 返回 true 时.感谢您的关注.

export const roomRouteEpic: Epic = (action$, state$) =>action$.ofType(LOCATION_CHANGE).pipe(pluck('payload'),合并地图(有效载荷 =>/^\/room\/\d+$/.test(payload.location.pathname)?的(state$.value.rooms.list[payload.location.pathname.split('/')[2]]?actions.rooms.initRoomEnter():actions.rooms.initRoomCreate(),actions.global.setIsLoading(true),): 空的,),);

I want to conditionally dispatch some actions using iif utility from RxJS. The problem is that second argument to iif is called even if test function returns false. This throws an error and app crashes immediately. I am new to to the power of RxJS so i probably don't know something. And i am using connected-react-router package if that matters.

export const roomRouteEpic: Epic = (action$, state$) =>
  action$.ofType(LOCATION_CHANGE).pipe(
    pluck('payload'),
    mergeMap(payload =>
      iif(
        () => {
          console.log('NOT LOGGED');
          return /^\/room\/\d+$/.test(payload.location.pathname); // set as '/login'
        },
        merge(
          tap(v => console.log('NOT LOGGED TOO')),
          of(
            // following state value is immediately evaluated
            state$.value.rooms.list[payload.location.pathname.split('/')[1]]
              ? actions.rooms.initRoomEnter()
              : actions.rooms.initRoomCreate(),
          ),
          of(actions.global.setIsLoading(true)),
        ),
        empty(),
      ),
    ),
  );

解决方案

Ok, i found an answer on my own. My solution is to remove iif completely and rely on just ternary operator inside mergeMap. that way its not evaluated after every 'LOCATION_CHANGE' and just if regExp returns true. Thanks for your interest.

export const roomRouteEpic: Epic = (action$, state$) =>
  action$.ofType(LOCATION_CHANGE).pipe(
    pluck<any, any>('payload'),
    mergeMap(payload =>
      /^\/room\/\d+$/.test(payload.location.pathname)
        ? of(
            state$.value.rooms.list[payload.location.pathname.split('/')[2]]
              ? actions.rooms.initRoomEnter()
              : actions.rooms.initRoomCreate(),
            actions.global.setIsLoading(true),
          )
        : EMPTY,
    ),
  );

这篇关于RxJS iif 参数何时不应该被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆