Rxjs 并行订阅者

Rxjs parallel subscribers

我有以下代码

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);
}