RxJ组合不同并根据流

时间:2018-05-09 16:22:20

标签: javascript functional-programming rxjs

const f1$ = Observable.create(ob => this.eventService.on('firstName', data => ob.next(data)));
const f2$ = Observable.create(ob => this.eventService.on('lastName', data => ob.next(data)));
const f3$ = Observable.create(ob => this.eventService.on('middleName', data => ob.next(data));

const mainObj$ = of({ address: 'abc street' });

combineLatest([f1$, f2$, f3$, mainObj$], (f, l, m, dataBlock) => {

    return R.compose(addFirstName(f), addLastName(l), addMiddleName(m))(dataBlock);

}).pipe(

    flatMap(data => this.someHttpService.save(data)),
    pluck('id')
).subscribe(id => console.log(`user ${id} saved successfully`));

如何删除compose并使用rxjs来实现相同的功能。

此外,我希望f1 $,f2 $,f3 $表现得像行为主题一样,因为我只想保存只有地址的用户。

我的主要目标是基于f1 $,f2 $,f3 $来操纵mainObj对象。

Witch combine最新如何找出触发了哪个observable,以便我只能应用该操作。 例如,如果触发 f1 $ ,我只需将addFirstNameFn应用于mainObj,而不是全部调用它们。 (addFirstName,addLastName,addMiddleName)。

1 个答案:

答案 0 :(得分:0)

您可以接下来()数据及其处理程序,并使用附加的处理程序执行,如下所示



const f1$ = Observable.create(ob => this.eventService.on('firstName', data => ob.next({data,func:(data)=>{})));
const f2$ = Observable.create(ob => this.eventService.on('lastName', data => ob.next({data,func:(data)=>{})));
const f3$ = Observable.create(ob => this.eventService.on('middleName', data =>ob.next({data,func:(data)=>{})));

const mainObj$ = of({ address: 'abc street' });

combineLatest([f1$, f2$, f3$, mainObj$], (f, l, m, dataBlock) => {

f1$.func(f1$.data) ...
m$.func(m$.data)... 
l$.func(l$.data)...

}).pipe(

    flatMap(data => this.someHttpService.save(data)),
    pluck('id')
).subscribe(id => console.log(`user ${id} saved successfully`));