-
Notifications
You must be signed in to change notification settings - Fork 9
/
05-sources.js
51 lines (44 loc) · 1.08 KB
/
05-sources.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
const Rx = require('rx');
function main(sources) {
return {
HTTP: sources.HTTP.tap(e => console.log('request to', e.req.url))
}
}
function makeHttpEffect() {
const requests_ = new Rx.Subject();
return {
writeEffect: function (model_) {
model_.subscribe(e => {
console.log('sending hello')
e.res.writeHead(200, { 'Content-Type': 'text/plain' })
e.res.end('Hello World\n')
})
return requests_
},
serverCallback: (req, res) => {
requests_.onNext({ req: req, res: res })
},
readEffect: requests_
}
}
const httpEffect = makeHttpEffect()
const drivers = {
HTTP: httpEffect
}
function run(main, drivers) {
const sources = {
HTTP: drivers.HTTP.readEffect
}
const sinks = main(sources)
Object.keys(drivers).forEach(key => {
drivers[key].writeEffect(sinks[key])
})
}
run(main, drivers)
const http = require('http')
const hostname = '127.0.0.1'
const port = 1337
http.createServer(httpEffect.serverCallback)
.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`)
});