2017-09-19 80 views
1

我正在轉換this應用程序以支持RxJS版本5.但我不確定如何轉換以下代碼。如何將RxJS v4應用程序轉換爲RxJS v5版本

losslessClick.subscribe(function (checked) { 
    if (checked) { 
    mousemove.resume(); 
    } else { 
    mousemove.pause(); 
    } 
}) 

我嘗試了here的建議,但沒有爲我工作。

var pauser = new Subject(); 
var pausable = pauser.switchMap(paused => paused ? Observable.never() : mousemove); 
pausable.subscription(x => console.log(x)); 
pauser.next(true); 

任何幫助,將不勝感激。提前致謝。

回答

1

這可能對你有所幫助。

var losslessResults = document.getElementById('losslessResults'); 
var losslessToggle = document.getElementById('losslessToggle'); 

function logInput(text) { 
    var li = document.createElement('li'); 
    li.innerHTML = text; 
    losslessResults.appendChild(li); 
} 

var mousemove = Rx.Observable.fromEvent(document, 'mousemove') 
    .map(function (e) { 
     return 'clientX: ' + e.clientX + ', clientY: ' + e.clientY; 
    }); 

// Lossless 
var losslessClick = Rx.Observable.fromEvent(losslessToggle, 'click') 
    .map(function (e) { 
     return e.target.checked; 
    }) 

var pauser = new Rx.Subject(); 
var pausable = pauser.switchMap(function (paused) { 
    return paused ? mousemove : Rx.Observable.never(); 
}); 
pausable.subscribe(function (x) { 
    return logInput(x); 
}); 
pauser.next(true); 
losslessClick.subscribe(pausable) 

工作示例可以找到here

+0

謝謝。它工作正常:) – stafan