您可以合併所有可觀察到的阻塞物(obsStop $),映射第一個停止事件以將其與mousemove區分開來,僅採用一個此類事件並用僞'close - takeUntil'事件進行連接。 takeWhile將採取所有的鼠標移動和關閉事件,直到虛擬'close - takeUntil'事件來臨。
let start = document.getElementById('start');
let stop1 = document.getElementById('stop1');
let stop2 = document.getElementById('stop2');
let stop3 = document.getElementById('stop3');
let start$ = Rx.Observable.fromEvent(start, 'click');
let stop1$ = Rx.Observable.fromEvent(stop1, 'click');
let stop2$ = Rx.Observable.fromEvent(stop2, 'click');
let stop3$ = Rx.Observable.fromEvent(stop3, 'click');
let obsStop$ = Rx.Observable.merge(stop1$, stop2$, stop3$);
start$.switchMap(x => {
return Rx.Observable.fromEvent(document, 'mousemove')
.merge(obsStop$
.map(event => {
return {stopClick: true, event: event};
})
.take(1)
.concat(Rx.Observable.of('takeUntil')))
.takeWhile(x => x !== 'takeUntil')
}).subscribe(x => console.log(x));