forked from gpittarelli/ineedthis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
89 lines (81 loc) · 2.26 KB
/
example.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
79
80
81
82
83
84
85
86
87
88
89
const {createService, start} = require('.');
const db = {
connect: () => ({close: () => void 0})
};
// Static config
var A = createService(
'package/Configuration', {
start: (config = {url: '', key: ''}) => () => config
}
);
// Connect to a remote server, eg a DB, based on the configuration
// service
var B = createService(
'package/ConnectionService', {
dependencies: ['package/Configuration'],
start: () => ({'package/Configuration': config}) => {
return db.connect(config.url, config.key);
},
stop: (dbConnection) => dbConnection.close()
}
);
var C = createService(
'package/OverlayService', {
dependencies: [
'package/Configuration',
'package/ConnectionService'
],
start: () => ({
'package/Configuration': config,
'package/ConnectionService': underlyingConn
}) => {
return {
_config: config,
doSomething: function (msg) {
console.log('Hello, ' + msg + '!');
}
};
}
}
);
var App = createService(
'package/AppServer', {
dependencies: [
'package/Configuration',
'package/OverlayService'
],
start: () => ({
'package/Configuration': config,
'package/OverlayService': overlay
}) => {
console.log('hi', config, overlay);
return new Promise((resolve, reject) => {
setTimeout(() => resolve('APP SERVER!'), 2000);
});
},
stop: server => new Promise(resolve => server.close(resolve))
}
);
// If the service names all follow the intended nameing pattern, a
// simple start() suffices.
start(App)
.then(system => {
console.log('started', system);
// setTimeout(() => stop(system), 1000);
})
.catch(err => console.error('ERR!', err));
// Starts A then B then C and finally App; then 1 second later shuts
// them down in reverse order (more complex dependency graphs will be
// handled via topological sort with maximal concurrency at every
// step). Cyclic dependencies trigger an error.
/*
// Starting multiple services that can dynamically share common
// dependencies
start([App, App2]);
// Manually specifying some (or all) dependency services:
const FakeB = MockB();
start(App, {
'package/Configuration': A({key: 'ABC'}),
'package/ConnectionService': FakeB,
}).catch(err => { /* });
*/