RxJS .zip and .switchLatest
HTML
<script src="https://rawgit.com/Reactive-Extensions/RxJS/master/dist/rx.all.js"></script>
<button id="message">message</button>
<textarea id='ta_message'></textarea>
<textarea id='ta_result'></textarea>
CSS
textarea {
font-family:"Courier New", monospace;
font-size: 12px;
width: 100%;
height: 120px;
}
JavaScript
// Every second
var source = Rx.Observable.interval(1000);
var subject = new Rx.Subject();
var subSource = source.subscribe(subject);
var disposable1 = subject.subscribe(
function (x) { console.log('Value published to observer #1: ' + x); },
function (e) { console.log('onError: ' + e.message); },
function () { console.log('onCompleted'); });
var disposable2 = subject.subscribe(
function (x) { console.log('Value published to observer #2: ' + x); },
function (e) { console.log('onError: ' + e.message); },
function () { console.log('onCompleted'); });
setTimeout(function () {
// Clean up
subject.onCompleted();
disposable1.dispose();
disposable2.dispose();
}, 5000);
// => Value published to observer #1: 0
// => Value published to observer #2: 0
// => Value published to observer #1: 1
// => Value published to observer #2: 1
// => Value published to observer #1: 2
// => Value published to observer #2: 2
// => Value published to observer #1: 3
// => Value published to observer #2: 3
// => Value published to observer #1: 4
// => Value published to observer #2: 4
// => onCompleted
// => onCompleted