-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomp.js
71 lines (59 loc) · 1.62 KB
/
comp.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
'use strict'
var flora = require('@yoda/flora')
var defaultConfig = {
'uri': 'unix:/var/run/flora.sock',
'bufsize': 0,
'reconnInterval': 10000
}
function FloraComp (fid, config) {
var furi
if (typeof config !== 'object') {
config = defaultConfig
}
if (typeof fid !== 'string') {
furi = config.uri
} else {
furi = config.uri + '#' + fid
}
this.agent = new flora.Agent(furi, config)
}
FloraComp.prototype.init = function () {
if (typeof this.handlers === 'object') {
Object.keys(this.handlers).forEach((key) => {
var cb = this.handlers[key]
if (typeof cb === 'function') {
this.agent.subscribe(key, cb.bind(this))
}
})
}
if (typeof this.remoteMethods === 'object') {
Object.keys(this.remoteMethods).forEach((key) => {
var cb = this.remoteMethods[key]
if (typeof cb === 'function') {
this.agent.declareMethod(key, cb.bind(this))
}
})
}
this.agent.start()
}
FloraComp.prototype.deinit = function () {
if (this.agent instanceof flora.Agent) {
this.agent.close()
}
}
FloraComp.prototype.post = function (name, msg, type, opts) {
if (this.agent instanceof flora.Agent) {
return this.agent.post(name, msg, type, opts)
}
return flora.ERROR_NOT_CONNECTED
}
FloraComp.prototype.subscribe = function subscribe (name, handler) {
return this.agent.subscribe(name, handler)
}
FloraComp.prototype.declareMethod = function declareMethod (name, handler) {
return this.agent.declareMethod(name, handler)
}
FloraComp.prototype.call = function call () {
return this.agent.call.apply(this.agent, arguments)
}
module.exports = FloraComp