-
Notifications
You must be signed in to change notification settings - Fork 7
/
dataCollectorDemo.js
executable file
·78 lines (66 loc) · 1.66 KB
/
dataCollectorDemo.js
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
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env node
console.log('Hello, World!');
var startCollecting = function() {
// for purpose of demonstration assume three parts
var part1, part2, part3;
// function which in the end processes all data
var finish = function() {
if(part1 !== undefined && part2 !== undefined && part3 !== undefined) {
console.log('All done!');
console.log('1: ' + part1);
console.log('2: ' + part2);
console.log('3: ' + part3);
} else {
console.log('Still waiting for data');
}
};
// functions via which the data can be set
// this functions could be directly used as callbacks
var setPart1 = function(data) {
part1 = data;
finish();
}
var setPart2 = function(data) {
part2 = data;
finish();
}
var setPart3 = function(data) {
part3 = data;
finish();
}
// the object which can be used to set the data, e.g. as a callback
// this is only required because in this example so we can pass the
// setter functions as a return value
var state = {
setPart1: setPart1,
setPart2: setPart2,
setPart3: setPart3
}
return state;
}
// show it works
state = startCollecting();
state.setPart1('bla');
state.setPart2('blu');
state.setPart3('blop');
console.log();
// resorted input
state = startCollecting();
state.setPart3('blop');
state.setPart2('blu');
state.setPart1('bla');
console.log();
// yet another input order
state = startCollecting();
state.setPart2('blu');
state.setPart1('bla');
state.setPart3('blop');
// two data collections running interleaved
state = startCollecting();
state2 = startCollecting();
state.setPart2('1.2');
state2.setPart1('2.1');
state2.setPart3('2.3');
state.setPart1('1.1');
state2.setPart2('2.2');
state.setPart3('1.3');