在rxjs6中,我们可以从一个操作符管道创建一个操作符。
import { pipe } from 'rxjs';
function doSomething() {
return pipe(
map(...),
flatMap(...),
);
}
$.pipe(
map(...),
doSomething(),
flatMap(...),
)
有没有办法在IxJS中创建这样的运算符?
发布于 2019-03-17 19:26:35
您可以手动组合运算符:
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)
)
}
https://stackoverflow.com/questions/54719907
复制相似问题