-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceHandler.js
More file actions
40 lines (39 loc) · 1.7 KB
/
ServiceHandler.js
File metadata and controls
40 lines (39 loc) · 1.7 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
const util = require("util");
const { DatabaseService } = require("./DatabaseService");
class NodeServiceHandler {
async init(service_handler_config, serviceLog, databaseServices = null) {
//make sure init of the same instance will be called only once
if (this.isInited === true) {
return;
}
else {
const init = true;
Object.defineProperty(this, 'isInited', { get: () => init, enumerable: true });
Object.defineProperty(this, 'config', { get: () => service_handler_config, enumerable: true });
}
if (Array.isArray(databaseServices)) {
for (const dbService of databaseServices) {
if (!(dbService instanceof DatabaseService)) continue;
Object.defineProperty(this, dbService.type, { get: () => dbService, enumerable: true });
}
}
// init handler last so it can use others services that have been initialized.
if (this.initHandler != null) {
const initResult = util.types.isAsyncFunction(this.initHandler) ? await this.initHandler(serviceLog) : this.initHandler(serviceLog);
}
}
async close(serviceLog) {
//make sure close of the same instance will be called only once
if (this.isClosed === true) {
return;
}
else {
const close = true;
Object.defineProperty(this, 'isClosed', { get: () => close, enumerable: true });
}
if (this.closeHandler != null) {
const closeResult = util.types.isAsyncFunction(this.closeHandler) ? await this.closeHandler(serviceLog) : this.closeHandler(serviceLog);
}
}
}
module.exports = NodeServiceHandler