我有以下代码
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
答案 0 :(得分:4)
您似乎需要.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);
}