-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment.js
More file actions
58 lines (46 loc) · 1.17 KB
/
environment.js
File metadata and controls
58 lines (46 loc) · 1.17 KB
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
const config = require('./config')
const Base = require('./base')
const Network = require('./network')
class Environment extends Base {
constructor(options = {}) {
super(options)
this.network = this.createNetwork()
this.network.ready().then(() => this.emit('ready'))
this.modulesByLabel = {}
}
handleModule(module) {
this.addModuleToLabelMap(module)
this.emit('module', module)
this.emit(`module.${module.name}`, module)
}
addModuleToLabelMap(module) {
let list = this.modulesByLabel[module.label]
if (list) {
list.push(module)
} else {
this.modulesByLabel[module.label] = [module]
}
}
handleReady() {
super.handleReady()
this.loadPlugins()
}
createNetwork() {
const { transport } = config.network
return new Network({ transport })
}
loadPlugins() {
const options = {}
config.plugins.forEach(PluginConstructor => new PluginConstructor(options))
}
}
let env = null
module.exports = (options = {}) => {
if (env) {
return env
}
const Constructor = options.Constructor || Environment
env = new Constructor(options)
return env
}
module.exports.Environment = Environment