带有角度可观察数组的组合管道

时间:2019-04-09 14:36:40

标签: javascript angular rxjs observable

我在以下位置将其管道连接到多管管道上具有Observable。

getOrders(filters: any, page: any = 1, orderBy: string = 'deliver_on', sort: string = 'asc') {

  const opCityPipe = pipe(
    filter((obj: any) => obj.payload.order.op_city_id === 1)
  );

  const storeRefPipe = pipe(
    filter((obj: any) => obj.payload.order.store.ref.match(/^Your.*/))
  );

  const statusPipe = pipe(
    filter((obj: any) => ['assign_request', 'accepted', 
    'in_store', 'en_route_to_client', 'arrived', 
    'canceled'].indexOf(obj.payload.order.status) !== -1)
  );

  return Observable.create((observer: Observer<any>) => {
     this.socket.on('private-order.status.changed.1:order.status', 
     (obj: any) => {
       observer.next(obj);
     });
  }).pipe(opCityPipe, storeRefPipe, statusPipe);
}

如何使管道成为数组?我想动态填充它。我试图添加数组,但出现错误。

  

错误TypeError:Object(...)(...)不是函数

我想做类似的事情

const pipes = [opCityPipe, storeRefPipe, statusPipe];
Observable.pipe(pipes);

更新解决方案

Observable.pipe(...pipes);

1 个答案:

答案 0 :(得分:1)

我认为这应该可行,但是您得到的错误尚不清楚。

getOrders(filters: any, page: any = 1, orderBy: string = 'deliver_on', sort: string = 'asc') {

  const opCityPipe = filter((obj: any) => obj.payload.order.op_city_id === 1);

  const storeRefPipe = filter((obj: any) => obj.payload.order.store.ref.match(/^Your.*/));

  const statusPipe = filter((obj: any) => ['assign_request', 'accepted', 
    'in_store', 'en_route_to_client', 'arrived', 
    'canceled'].indexOf(obj.payload.order.status) !== -1);
   
  const filters = [opCityPipe, storeRefPipe, statusPipe]

  return Observable.create((observer: Observer<any>) => {
     this.socket.on('private-order.status.changed.1:order.status', 
     (obj: any) => {
       observer.next(obj);
     });
  }).pipe(...filters);
}