Skip to content

Instantly share code, notes, and snippets.

@thetrav
Created October 19, 2014 02:17
Show Gist options
  • Save thetrav/b1d27db3e8820146204c to your computer and use it in GitHub Desktop.
Save thetrav/b1d27db3e8820146204c to your computer and use it in GitHub Desktop.
Bacon Dispatch?
<html>
<head>
<title>test eventTicks</title>
<script src="js/lib/zepto-1.1.4.js"></script>
<script src="js/lib/bacon-0.7.12.js"></script>
<script>
$(function() {
var ticks = new Bacon.Bus();
ticks.plug($(".ticker").asEventStream("click", function() {
return {tick: 1};
}));
var tocks = new Bacon.Bus();
tocks.plug($(".tocker").asEventStream("click", function() {
return {tock: 1};
}));
var tickProp = ticks.scan(0, function(m, e) {
return m + e.tick;
});
tickProp.assign($('.ticks'), "text");
var tockProp = tocks.scan(0, function(m, e) {
return m + e.tock;
});
tockProp.assign($('.tocks'), "text");
var tickTock = Bacon.combineWith(function(tickCount, tockCount) {
if(tickCount == 2) {
ticks.push({tick: -2});
tocks.push({tock: 1});
}
return {ticks: tickCount, tocks: tockCount};
}, tickProp, tockProp);
tickTock.onValue(function(e) {
$(".tickTock").text(JSON.stringify(e));
});
});
</script>
</head>
<body>
<div class="ticks"></div>
<button class="ticker">tick</button>
<div class="tocks"></div>
<button class="tocker">tock</button>
<div class="tickTock"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment