0

在 rxjs6 中,我们可以从操作符管道中创建一个操作符。

import { pipe } from 'rxjs';

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

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

有没有办法在 IxJS 中创建这样的运算符?

4

1 回答 1

0

您可以手动组合运算符:

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

或者编写自己的pipe实现:

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

function customOperator() {
  return pipe(
    filter(x => x % 2 === 0),
    map(x => x * x)
  )
}
于 2019-03-17T11:26:35.053 回答