这是场景:
我有多个连接到不同的数据库,我想确保代码在所有连接都处于活动状态时运行。
我正在使用 Rxjs 来处理这个问题(欢迎使用另一种解决方案),但我面临着如果我在其中一个事件处于活动状态后组合连接事件,我永远不会运行订阅,因为 combineLatest 希望发出所有可观察的,但它们是!
const a = new Rx.Subject();
const b = new Rx.Subject();
var bool = false;
setInterval(()=>{
bool = !bool
a.next(bool ? ' i am connected' : 'im NOT connected');
},1000)
setTimeout(()=>{
b.next('i am always connected!')
},400)
// this variable will be exported to all js that run queries
var obs = new Rx.Observable.combineLatest(a,b);
setTimeout(()=>{
obs.subscribe((andb)=>{
console.log( andb )
// i can check all connections at once and run the code
})
},399)
// problem is here, i want to subscribe later than the connections
//emit, if you edit 399 to 401 you will see that nothing is going on
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.5.6/Rx.js"></script>