Garine Pondikian
Garine Pondikian

Reputation: 255

Compine Pipes in array with angular Observable

I have Observable where I'm piping it to multipe pipes as follows.

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);
}

How can I make the pipe as array? I want to dynamically populate it. I tried to add the array but got an error.

ERROR TypeError: Object(...)(...) is not a function

I want to do something like

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

UPDATE SOLUTION

Observable.pipe(...pipes);

Upvotes: 0

Views: 66

Answers (1)

Federico Galfione
Federico Galfione

Reputation: 1109

I think this should work, but the error you're getting is not clear.

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);
}

Upvotes: 1

Related Questions