Anthony Lau
Anthony Lau

Reputation: 17

How to create custom operator from a pipe of operators in IxJS?

In rxjs6, we can create an operator from a pipe of operators.

import { pipe } from 'rxjs';

function doSomething() {
   return pipe(
       map(...),
       flatMap(...),
   );
}

$.pipe(
   map(...),
   doSomething(),
   flatMap(...),
)

Is there a way to create an operator like this in IxJS?

Upvotes: 0

Views: 127

Answers (1)

Oles Savluk
Oles Savluk

Reputation: 4345

You can combine operators manually:

import { IterableX as Iterable } from 'ix/iterable';
import { map, filter } from 'ix/iterable/pipe/index';

function customOperator() {
  return source$ => map(x => x * x)(
    filter(x => x % 2 === 0)
    (source$)
  );
}

const results = Iterable.of(1, 2, 3, 4).pipe(
  customOperator()
).forEach(x => console.log(`Next ${x}`));

Or write your own pipe implementation:

const pipe = (...fns) =>
  source$ => fns.reduce(
    (acc, fn) => fn(acc),
    source$
  );

function customOperator() {
  return pipe(
    filter(x => x % 2 === 0),
    map(x => x * x)
  )
}

Upvotes: 0

Related Questions