-
Notifications
You must be signed in to change notification settings - Fork 23
/
09_rx_complicated.js
57 lines (46 loc) · 1.39 KB
/
09_rx_complicated.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
var Promise = require("bluebird"),
Rx = require("rx"),
aBootTime = 1000,
bBootTime = 1000,
observableB;
serverA();
observableB = serverB();
function serverA() {
console.log("A: Booting up system...");
return Rx.Observable.timer(aBootTime)
.do(checkNetwork)
.delay(500)
.flatMap(sendRequest)
.subscribe(observer);
setTimeout(checkNetwork, aBootTime);
function checkNetwork() {
console.log("A: Checking network connection");
}
function sendRequest() {
console.log("A: Request complex computation");
return Rx.Observable.fromCallback(sendNetworkRequest)();
}
function observer(value) {
console.log("A: Computation returned " + value);
}
}
function serverB() {
console.log("B: Booting up system...")
var subject = new Rx.AsyncSubject();
Rx.Observable.timer(bBootTime).map(listenRequests).subscribe(subject);
return subject;
function listenRequests() {
console.log("B: Server up and running");
return serverHandler;
}
function serverHandler(callback) {
console.log("B: Starting heavy computation");
Rx.Observable.timer(2000).subscribe(answerRequest);
function answerRequest() {
callback(42);
}
}
}
function sendNetworkRequest(callback) {
observableB.subscribe(serverHandler => serverHandler(callback));
}