-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
90 lines (80 loc) · 2.19 KB
/
index.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
90
const crypto = require('crypto');
const os = require('os');
const objectAssignDeep = require('object-assign-deep');
class LeaseholdApp {
constructor({processStream}) {
this.channel = null;
this.options = {};
this.appState = {
modules: {}
};
this.nonce = crypto.randomBytes(8).toString('hex');
this.os = os.platform() + os.release();
this.processStream = processStream;
}
get alias() {
return 'leasehold_app';
}
get dependencies() {
return [];
}
get events() {
return ['ready', 'state:updated'];
}
get actions() {
return {
getApplicationState: {
handler: async (action) => ({...this.appState})
},
updateApplicationState: {
handler: async (action) => {
this.updateAppState(action.params);
}
},
getComponentConfig: {
handler: (action) => this.options.components[action.params]
},
getModuleState: {
handler: (action) => this.appState.modules[action.params.moduleName]
},
updateModuleState: {
handler: (action) => {
this.updateAppState({
modules: {
...action.params
}
});
}
}
};
}
updateAppState(newAppState) {
objectAssignDeep(this.appState, newAppState);
this.channel.publish('leasehold_app:state:updated', this.appState);
}
async load(channel, options) {
let mainNetworkConfig = this.appConfig.modules[options.moduleRedirects.network] || {};
let mainHTTPAPIConfig = this.appConfig.modules[options.moduleRedirects.http_api] || {};
this.channel = channel;
this.options = options;
this.appState = {
...options.nodeInfo,
os: this.os,
nonce: this.nonce,
height: 1,
wsPort: mainNetworkConfig.wsPort || options.defaultWSPort,
httpPort: mainHTTPAPIConfig.httpPort || options.defaultHTTPPort,
modules: {}
};
(async () => {
for await (let [data] of this.processStream.listener('message')) {
if (data && data.event === 'appReady') {
this.channel.publish('leasehold_app:ready');
break;
}
}
})();
}
async unload() {}
};
module.exports = LeaseholdApp;