forked from thlorenz/kebab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.html
50 lines (42 loc) · 1.37 KB
/
example.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
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Kebab Producer/Consumer Example</title>
<script type="text/javascript" src="./kebab.js"></script>
<script type="text/javascript" charset="utf-8">
var kb = kebab()
, items = 10
, consumers = 4
;
function produce () {
var id = --items;
if (id) {
setTimeout(function () {
document.writeln('<p>Producer enqueueing item ' + id +'</p>');
kb.enqueue({ now: new Date() }, id);
produce();
}, 50);
}
}
function consume (num) {
kb.once(function (info, id) {
document.writeln(
'<p>Consumer ' + num + ' handling item ' + id + ' produced at (' +
info.now.getSeconds() + 's:' + info.now.getMilliseconds() + 'ms).</p>'
);
// younger consumers are lazy and therefore only consume once
if (num > 1) {
// heavy work going on
setTimeout(function () { consume(num); }, 180);
}
});
}
for (var i = 0; i < consumers; i++)
consume(i);
produce();
</script>
</head>
<body>
</body>
</html>