工作日常记录 2017-09-21
等待联调,没什么重要的任务。继续看各种文档
- RxJS的zip和昨天描述的merge类似,是对多个Observable进行合并的操作。不过不同点在于, zip是把内部绑定的多个Observable进行同步,当内部多个Observable都返回了结果的时候,把各个Obseravable的返回结果合成一个结果打到zip生成的Observable主线上面,例子如下:
import Rx from 'rxjs';
import Events from 'events';
// Default two Observable created by timer
const timer1 = Rx.Observable.timer(0, 8000)
.mapTo({ value: 'value from timer1' });
const timer2 = Rx.Observable.timer(0, 10000)
.mapTo({ value: 'value from timer2' });
// Input trigger value
const inputE = new Events.EventEmitter();
process.stdin.addListener(
'data', value => inputE.emit('input',`${input}`)
);
const input = Rx.Observable.fromEvent(inputE, 'input');
// Create a Observable that zip all the Observable above
const zipppedObservable = Rx.Observable.zip(
timer1, timer2, input
).subscribe(
param =>(console.log(
`result:${JSON.stringify(param)}`
))
);
// In console, when you run the entire project
// Nothing will happen, after 10 second, you enter
this is a test \n
// Output will be
result:[{ value: 'value from timer1' }, { value: 'value from timer2' }, 'this is a test']