Following is an HTML snippet I'm using as an example:
<html>
<head>
<script src="rx.all.js"></script>
</head>
<body>
<script>
var source = new Rx.BehaviorSubject(function() {return 2;});
var stuff = source.scan([], function(val, operation) {
return operation(val);
});
stuff.subscribe(function(v) {
console.log("first subscriber");
console.log(v);
});
source.onNext(function(val) {
return val * 2;
});
stuff.subscribe(function(v) {
console.log("second subscriber");
console.log(v);
});
</script>
</body>
The output in the JS console is:
first subscriber
2
first subscriber
4
second subscriber
0
Now, "stuff" does some processing (basically applying a function to the current value), an idea which I've taken from TodoMVC example for ReactJs + RxJS (https://github.com/fdecampredon/react-rxjs-todomvc).
The output I'm trying to achieve is to have the second subscriber also see "4" the moment it subscribes. I'm using RxJS in conjunction with ReactJS, so components unsubscribe when they're unmounted (due to a route change), and subscribe again when they're mounted again.
replay(1)
does not work in RxJS. You will have to usereplay(null, 1)
. – Schizont