Skip to content

Commit 9a398c2

Browse files
committed
sync with yodaos-project/yodart
1 parent 497582f commit 9a398c2

7 files changed

+834
-242
lines changed

CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ target_include_directories(shadow-flora-cli PRIVATE
6565
target_link_libraries(shadow-flora-cli iotjs flora-cli caps)
6666

6767
install(TARGETS shadow-flora-cli DESTINATION ${CMAKE_INSTALL_DIR})
68-
install(FILES index.js comp.js DESTINATION ${CMAKE_INSTALL_DIR})
68+
69+
file(GLOB YODA_FLORA_SRC *.js)
70+
install(FILES ${YODA_FLORA_SRC} DESTINATION ${CMAKE_INSTALL_DIR})
6971
endif(BUILD_INDEPENDENT)
7072

7173
target_compile_options(shadow-flora-cli PRIVATE

comp.js

+36-13
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,64 @@ var defaultConfig = {
88
'reconnInterval': 10000
99
}
1010

11-
function FloraComp () {
12-
}
13-
14-
FloraComp.prototype.init = function (fid, config) {
15-
if (typeof fid !== 'string') {
16-
fid = ''
17-
}
11+
function FloraComp (fid, config) {
12+
var furi
1813
if (typeof config !== 'object') {
1914
config = defaultConfig
2015
}
21-
this.agent = new flora.Agent(config.uri + '#' + fid,
22-
config.reconnInterval, config.bufsize)
16+
if (typeof fid !== 'string') {
17+
furi = config.uri
18+
} else {
19+
furi = config.uri + '#' + fid
20+
}
21+
this.agent = new flora.Agent(furi, config)
22+
}
2323

24+
FloraComp.prototype.init = function () {
2425
if (typeof this.handlers === 'object') {
2526
Object.keys(this.handlers).forEach((key) => {
2627
var cb = this.handlers[key]
2728
if (typeof cb === 'function') {
2829
this.agent.subscribe(key, cb.bind(this))
2930
}
3031
})
31-
this.agent.start()
3232
}
33+
34+
if (typeof this.remoteMethods === 'object') {
35+
Object.keys(this.remoteMethods).forEach((key) => {
36+
var cb = this.remoteMethods[key]
37+
if (typeof cb === 'function') {
38+
this.agent.declareMethod(key, cb.bind(this))
39+
}
40+
})
41+
}
42+
43+
this.agent.start()
3344
}
3445

35-
FloraComp.prototype.destruct = function () {
46+
FloraComp.prototype.deinit = function () {
3647
if (this.agent instanceof flora.Agent) {
3748
this.agent.close()
3849
}
3950
}
4051

41-
FloraComp.prototype.post = function (name, msg, type) {
52+
FloraComp.prototype.post = function (name, msg, type, opts) {
4253
if (this.agent instanceof flora.Agent) {
43-
return this.agent.post(name, msg, type)
54+
return this.agent.post(name, msg, type, opts)
4455
}
4556
return flora.ERROR_NOT_CONNECTED
4657
}
4758

59+
FloraComp.prototype.subscribe = function subscribe (name, handler) {
60+
return this.agent.subscribe(name, handler)
61+
}
62+
63+
FloraComp.prototype.declareMethod = function declareMethod (name, handler) {
64+
return this.agent.declareMethod(name, handler)
65+
}
66+
67+
FloraComp.prototype.call = function call () {
68+
return this.agent.call.apply(this.agent, arguments)
69+
}
70+
4871
module.exports = FloraComp

disposable.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
var flora = require('@yoda/flora')
2+
3+
var defaultUrl = 'unix:/var/run/flora.sock'
4+
5+
module.exports.once = once
6+
/**
7+
*
8+
* @param {string} name
9+
* @param {object} [options]
10+
* @param {string} [options.url='unix:/var/run/flora.sock']
11+
* @param {number} [options.timeout=15000]
12+
*/
13+
function once (name, options) {
14+
if (options == null) {
15+
options = {}
16+
}
17+
var url = options.url || defaultUrl
18+
var timeout = options.timeout || 15000
19+
var agent = new flora.Agent(url)
20+
return new Promise((resolve, reject) => {
21+
var timer = setTimeout(() => {
22+
agent.close()
23+
reject(new Error(`flora.once timeount for ${timeout}`))
24+
}, timeout)
25+
agent.subscribe(name, msg => {
26+
clearTimeout(timer)
27+
agent.close()
28+
resolve(msg)
29+
})
30+
agent.start()
31+
})
32+
}
33+
34+
module.exports.post = post
35+
/**
36+
*
37+
* @param {string} name
38+
* @param {any[]} msg
39+
* @param {number} [type]
40+
* @param {object} [options]
41+
* @param {string} [options.url='unix:/var/run/flora.sock']
42+
* @returns {number} status code, 0 if post succeeded.
43+
*/
44+
function post (name, msg, type, options) {
45+
if (typeof name !== 'string') {
46+
throw new TypeError('Expect a string on first argument of flora.disposable.post')
47+
}
48+
if (!Array.isArray(msg)) {
49+
throw new TypeError('Expect an array on second argument of flora.disposable.post')
50+
}
51+
if (typeof type !== 'number') {
52+
options = type
53+
type = flora.MSGTYPE_INSTANT
54+
}
55+
if (options == null) {
56+
options = {}
57+
}
58+
var url = options.url || defaultUrl
59+
var agent = new flora.Agent(url)
60+
agent.start()
61+
62+
var ret = agent.post(name, msg, type)
63+
agent.close()
64+
65+
return ret
66+
}

0 commit comments

Comments
 (0)