-
Notifications
You must be signed in to change notification settings - Fork 0
/
RxExampleSimpleClicks.html
66 lines (57 loc) · 2.18 KB
/
RxExampleSimpleClicks.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<div class="container">
<div class="header">
<a href="#" class="this">BUTTON</a>
<h2>Double click it</h2>
</div>
</div>
<div>
<button>BTN</button>
</div>
<script src="http://cdnjs.cloudflare.com/ajax/libs/rxjs/2.2.26/rx.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/rxjs/2.2.26/rx.async.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/rxjs/2.2.26/rx.coincidence.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/rxjs/2.2.26/rx.binding.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/rxjs/2.2.26/rx.time.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/rxjs-dom/2.0.7/rx.dom.js"></script>
<script type="text/javascript">
// Make the raw clicks stream
var button = document.querySelector('.this');
var clickStream = Rx.Observable.fromEvent(button, 'click');
// HERE
// The 4 lines of code that make the multi-click logic
var multiClickStream = clickStream
.buffer(function () { return clickStream.throttle(250); })
.map(function (list) { return list.length; })
.filter(function (x) { return x >= 2; });
// Same as above, but detects single clicks
var singleClickStream = clickStream
.buffer(function () { return clickStream.throttle(250); })
.map(function (list) { return list.length; })
.filter(function (x) { return x === 1; });
// Listen to both streams and render the text label accordingly
singleClickStream.subscribe(function (event) {
document.querySelector('h2').textContent = event + ' click';
});
multiClickStream.subscribe(function (numclicks) {
document.querySelector('h2').textContent = '' + numclicks + 'x click';
});
var obs = Rx.Observable.merge(singleClickStream, multiClickStream);
obs.throttle(1000).subscribe(function (suggestion) {
document.querySelector('h2').textContent = '';
});
</script>
<style>
body {
font-family: sans-serif;
padding: 10px;
}
h2 {
font-weight: bold;
display: inline-block;
margin-left: 20px;
}
.header {
background: #ECECEC;
padding: 5px;
}
</style>