-
Notifications
You must be signed in to change notification settings - Fork 0
/
goblin-registry.js
60 lines (54 loc) · 1.34 KB
/
goblin-registry.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
'use strict';
const Goblin = require('./lib/index.js');
let routingKey = '$';
try {
const xHost = require('xcraft-core-host');
routingKey = xHost.getRoutingKey();
} catch (ex) {
if (ex.code !== 'MODULE_NOT_FOUND') {
throw ex;
}
}
const cmd = {};
const getState = `${routingKey}.getState`;
cmd[getState] = function (msg, resp) {
let state = null;
const goblinId = msg.data.goblinId;
const Goblins = Goblin.getGoblinsRegistry();
try {
const namespace = Goblin.getGoblinName(goblinId);
if (Goblins.has(namespace) && Goblins.get(namespace).has(goblinId)) {
const goblin = Goblins.get(namespace).get(goblinId);
state = goblin.isCreated() ? goblin.getState() : null;
}
} catch (ex) {
resp.events.send(`goblin-registry.${getState}.${msg.id}.error`, {
code: ex.code,
message: ex.message,
stack: ex.stack,
});
} finally {
resp.events.send(`goblin-registry.${getState}.${msg.id}.finished`, state);
}
};
/**
* Retrieve the list of available commands.
*
* @returns {Object} The list and definitions of commands.
*/
exports.xcraftCommands = function () {
return {
handlers: cmd,
rc: {
[getState]: {
parallel: true,
desc: "get goblin's state",
options: {
params: {
required: ['goblinId'],
},
},
},
},
};
};