Exercise 8 solution
Not ideal solution. We have two stream, the clicks and the timer and let's compose them together.
$(document).ready(function(){
var $btn = $("#btn"),
$list = $("#list");
clicks = ASQ.react.of(),
timer = ASQ.react.of()
$btn.click(function(evt){
clicks.push(evt);
});
setInterval(function(){
timer.push();
},1000)
// no merge the two streams
var msgs = ASQ.rect.all(clicks, timer);
msgs.val(function(){
$list.append($("<div>clicked</div>"))
});
});
The problem is that the click events queue will stack up, since nothing is throwing away these click events between 1000msSo after every 1000ms you will get a new output from the queue
Talks a bit about "hot" and "cold" observablesSo instead he keeps track of the latest click.
$(document).ready(function(){
var $btn = $("#btn"),
$list = $("#list"),
clicks = ASQ.react.of(),
msgs = ASQ.react.of(),
latest;
$btn.click(function(evt){
// push click event messages into stream
clicks.push(evt);
});
// sample clicks stream
setInterval(function(){
if (latest) {
msgs.push("clicked!");
latest = null;
}
},1000);
// subscribe to click stream
clicks.val(function(evt){
// separete the concerns (from the click handling)
latest = evt;
});
// subscribe to sampled message stream
msgs.val(function(msg){
$list.append($("<div>" + msg + "</div>"));
});
});