我們有以下流。rxjs在處理數據之前檢查流是否爲空
const recorders = imongo.listCollections('recorders')
.flatMapConcat(names => {
const recorders = names
.map(entry => entry.name)
.filter(entry => !_.contains(
['recorders.starts',
'recorders.sources',
'system.indexes',
'system.users'],
entry));
console.log(recorders);
return Rx.Observable.fromArray(recorders);
});
recorders.isEmpty()
.subscribe(
empty => {
if(empty) {
logger.warn('No recorders found.');
}
},
() => {}
);
recorders.flatMapConcat(createRecorderIntervals)
.finally(() => process.exit(0))
.subscribe(
() => {},
e => logger.error('Error while updating: %s', e, {}),
() => logger.info('Finished syncing all recorders')
);
如果流爲空,那麼我們不想createRecorderIntervals
。上面這段代碼正在工作。但是,檢查流是否爲空,導致console.log
被執行兩次。這是爲什麼發生?我能以某種方式修復它嗎?
編輯:所以,我去下面的方式,重新思考它感謝@馬丁的回答後
const recorders = imongo.listCollections('recorders')
.flatMapConcat(names => {
const recorders = names
.map(entry => entry.name)
.filter(entry => !_.contains(
['recorders.starts',
'recorders.sources',
'system.indexes',
'system.users'],
entry));
if(!recorders.length) {
logger.warn('No recorders found.');
return Rx.Observable.empty();
}
return Rx.Observable.fromArray(recorders);
})
.flatMapConcat(createRecorderIntervals)
.finally(() => scheduleNextRun())
.subscribe(
() => {},
e => logger.error('Error while updating: %s', e, {}),
() => logger.info('Finished syncing all recorders')
);
在這種情況下,我不能檢查整個流是否爲空,因爲我沒有得到整個數組,但單獨的值,對不對? – XeniaSis
我只是想在沒有錄像機的情況下顯示信息 – XeniaSis
我更新了我的答案。 tl; dr查看['do()'](http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-do)運算符。 – martin