RxJS过滤器阵列

RxJS过滤器阵列,rxjs,Rxjs,我正在尝试对rxjs中的数组执行一个过滤器。考虑以下事项: function guard1(): boolean | Observable<boolean> {} function guard2(): boolean | Observable<boolean> {} function guard3(): boolean | Observable<boolean> {} const routes = [ { na

我正在尝试对rxjs中的数组执行一个过滤器。考虑以下事项:

    function guard1(): boolean | Observable<boolean> {}
    function guard2(): boolean | Observable<boolean> {}
    function guard3(): boolean | Observable<boolean> {}

    const routes = [
        { name: 'Foo', canActivate: [guard1, guard2] },
        { name: 'Bar', canActivate: [guard3] },
        { name: 'Moo' }
    ];
如果我在RXJS之外编写此代码,代码可能如下所示:

    this.filteredRoutes = [];
    for (const route of this.routes) {
        if (route.canActivate) {
            let can = true;
            for (const act of route.canActivate) {
                let res = inst.canActivate();
                if (res.subscribe) {
                    res = await res.toPromise();
                }
                can = res;
                if (!can) {
                    break;
                }
            }
            if (can) {
                this.filteredRoutes.push(route);
            }
        } else {
            this.filteredRoutes.push(route);
        }
    }
谢谢

我确信还有其他(而且可能更好的)方法来处理这个问题,但它是有效的

from(routes).pipe(
    concatMap((route) => {
      // handle if nothing is in canActivate
      if (!route.canActivate || route.canActivate.length === 0) {
        // create an object that has the route and result for filtering
        return of({route, result: true})
      };

      const results = from(route.canActivate).pipe(
        // execute the guard
        switchMap(guard => {
          const result: boolean | Observable<boolean> = guard();
          if (result instanceof Observable) {
            return result;
          } else {
            return of(result);
          }
        }),
        // aggregate the guard results for the route
        toArray(),
        // ensure all results are true
        map(results => results.every(r => r)),
        // create an object that has the route and result for filtering
        map(result => ({route, result})),
      );

      return results;
    }),
    // filter out the invalid guards
    filter(routeCanActivateResult => routeCanActivateResult.result),
    // return just the route
    map(routeCanActivateResult => routeCanActivateResult.route),
    // turn it back into an array
    toArray()
  )
  // verify it works
  .subscribe(routes => routes.forEach(r => console.log(r.name)));
来自(管线)。管道(
concatMap((路线)=>{
//如果canActivate中没有任何内容,请处理
如果(!route.canActivate | | route.canActivate.length==0){
//创建具有用于筛选的路由和结果的对象
返回({route,result:true})
};
const results=from(route.canActivate).pipe(
//处决守卫
开关映射(保护=>{
常量结果:布尔值|可观察=保护();
if(可观测的结果实例){
返回结果;
}否则{
返回(结果);
}
}),
//聚合路由的防护结果
toArray(),
//确保所有结果都是真实的
映射(results=>results.every(r=>r)),
//创建具有用于筛选的路由和结果的对象
映射(结果=>({route,result})),
);
返回结果;
}),
//过滤掉无效的防护装置
过滤器(routeCanActivateResult=>routeCanActivateResult.result),
//只返回路线
映射(routeCanActivateResult=>routeCanActivateResult.route),
//将其转换回数组
toArray()
)
//验证它是否有效
.subscribe(routes=>routes.forEach(r=>console.log(r.name));
此外,下面是中的一个工作示例

from(routes).pipe(
    concatMap((route) => {
      // handle if nothing is in canActivate
      if (!route.canActivate || route.canActivate.length === 0) {
        // create an object that has the route and result for filtering
        return of({route, result: true})
      };

      const results = from(route.canActivate).pipe(
        // execute the guard
        switchMap(guard => {
          const result: boolean | Observable<boolean> = guard();
          if (result instanceof Observable) {
            return result;
          } else {
            return of(result);
          }
        }),
        // aggregate the guard results for the route
        toArray(),
        // ensure all results are true
        map(results => results.every(r => r)),
        // create an object that has the route and result for filtering
        map(result => ({route, result})),
      );

      return results;
    }),
    // filter out the invalid guards
    filter(routeCanActivateResult => routeCanActivateResult.result),
    // return just the route
    map(routeCanActivateResult => routeCanActivateResult.route),
    // turn it back into an array
    toArray()
  )
  // verify it works
  .subscribe(routes => routes.forEach(r => console.log(r.name)));