【问题标题】:How to create custom operator from a pipe of operators in IxJS?如何从 IxJS 中的运算符管道创建自定义运算符?
【发布时间】:2019-07-10 05:18:54
【问题描述】:

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

import { pipe } from 'rxjs';

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

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

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

【问题讨论】:

    标签: ixjs


    【解决方案1】:

    您可以手动组合运算符:

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

    【讨论】:

      猜你喜欢
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-26
      • 1970-01-01
      • 2022-06-29
      相关资源
      最近更新 更多