rxjs5 - rxjs, How can I merge multiple subjects in to one Observable, but process them with different method -
i have 3 subject
. this:
const s1$ = new subject()
const s2$ = new subject()
const s3$ = new subject()
these 3 subjects call next()
emit same value: const fruit = {id: 1, name: apple}
;
and, have 3 methods handle logic one 1 correspondence of subjects
call next(fruit)
method.
method1(): { //called when s1$.next(fruit) }
method2(): { //called when s2$.next(fruit) }
method3(): { //called when s3$.next(fruit) }
i want implement this:
// here maybe not observable.merge, it's thinking. observable.merge( s1$, s2$, s3$ ) .dosomeoperator() .subscribe(val => { //val s1$ emit, s2$ emit or s3$ emit //but val same, fruit. //do map s1->method1, s2->method2, s3->method3, can omit if...else statement. const method = this.method1 | this.method2 | this.method3. method(); })
how can implement this, thanks.
use map operator add distinguish sources.
export class appcomponent { s1(val) { console.log('s1', val); } s2(val) { console.log('s2', val); } constructor() { const s1= new subject(); const s2= new subject(); const m1= s1.map(val=> ({val, source:'s1'})); const m2 = s2.map(val=> ({val, source:'s2'})); observable.merge(m1, m2) .subscribe(({val, source}) => { this[source](val); }); s1.next('apple'); s2.next('apple'); } }
Comments
Post a Comment