我有以下代码
x = new Rx.Subject()
z = new Rx.Subject()
loop = 900000000;
x
.do(() => console.log('x start'))
.do(() => { let i = loop; while(i--) continue })
.subscribe(() => console.log('x end'))
z
.do(() => console.log('z start'))
.do(() => { let i = loop; while(i--) continue })
.subscribe(() => console.log('z end'))
x.subscribe(z)
如果我将新事件发送到
x
,则将是输出:z start
z end
x start
x end
但是我需要同时进行订阅,并将其记录为
x start
z start
x end
z end
我不在乎订单。
我尝试添加
.subscribeOn(Rx.Scheduler.async)
,但结果是相同的。查看示例here
最佳答案
看起来您需要.observeOn()
console.clear()
const x = new Rx.Subject();
const z = new Rx.Subject();
const loop = 900000000;
x
.do(() => log('x start'))
.do(() => { let i = loop; while(i--) continue })
.observeOn(Rx.Scheduler.async)
.subscribe(() => log('x end'));
z
.do(() => log('z start'))
.do(() => { let i = loop; while(i--) continue })
.observeOn(Rx.Scheduler.async)
.subscribe(() => log('z end'));
x.subscribe(z);
document
.querySelector('#next')
.addEventListener('click', () => x.next());
function log(data) {
const logs = document.querySelector('#logs');
const log = document.createElement('p');
log.innerText = data;
logs.appendChild(log);
}