Skip to content

Commit 4a8cb16

Browse files
committed
Fixed bug generating id for local functions
1 parent 9b4dd8b commit 4a8cb16

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

src/api/createNodeFactory.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ export default function createNodeFactory({ encode, decode }) {
1111
errorInstances = [Error],
1212
} = {}) {
1313
const requests = {}
14+
let request_id_index = 0
1415
const local_functions_id = {}
1516
const local_functions = new Map()
16-
let local_function_index = 0
17-
let request_id_index = 0
17+
let local_function_index = 1 // 0 is reserved for the entry function used in open()
1818

1919
const encode_params = {
2020
local_functions,
@@ -29,15 +29,14 @@ export default function createNodeFactory({ encode, decode }) {
2929

3030
function createRemoteFunction({
3131
function_id,
32-
target,
33-
path,
34-
caller,
3532
function_creator,
33+
caller,
34+
path,
3635
}) {
3736
const makeCall = (request_id, args) => {
3837
const data = [request_id, function_id]
3938
if (args.length > 0) data.push(args)
40-
api.send(encode(data, encode_params))
39+
node.send(encode(data, encode_params))
4140
return data
4241
}
4342

@@ -51,7 +50,7 @@ export default function createNodeFactory({ encode, decode }) {
5150
return req
5251
}
5352
req.data = makeCall(request_id, args)
54-
req.node = api
53+
req.node = node
5554
req.createdAt = new Date().getTime()
5655
req.resolve = (value) => resolveOrReject(resolve, value)
5756
req.reject = (error) => resolveOrReject(reject, error)
@@ -65,20 +64,27 @@ export default function createNodeFactory({ encode, decode }) {
6564

6665
return rpcFilter({
6766
rpc,
68-
node: api,
67+
node,
6968
function_id,
7069
function_creator,
7170
caller,
7271
path,
7372
})
7473
}
7574

75+
function getNextLocalFunctionId() {
76+
while (local_functions_id.hasOwnProperty(local_function_index)) {
77+
local_function_index += 1
78+
}
79+
return local_function_index
80+
}
81+
7682
function open(send, fn) {
77-
const local_function_id = local_function_index++
78-
if (isFunction(fn)) registerLocalFunction(local_function_id, fn)
79-
api.send = (msg) => send(serialize(msg))
83+
const function_id = 0
84+
if (isFunction(fn)) registerLocalFunction(function_id, fn)
85+
node.send = (msg) => send(serialize(msg))
8086
return createRemoteFunction({
81-
function_id: 0,
87+
function_id,
8288
function_creator: FUNCTION_CREATOR.ENTRY,
8389
})
8490
}
@@ -109,7 +115,7 @@ export default function createNodeFactory({ encode, decode }) {
109115

110116
// Request without response
111117
if (id === 0) {
112-
const req = { node: api }
118+
const req = { node }
113119
args.push(req)
114120
fn.apply(req, args)
115121
}
@@ -118,14 +124,14 @@ export default function createNodeFactory({ encode, decode }) {
118124
else {
119125
const req = createRequest()
120126
const response = [response_id]
121-
req.node = api
127+
req.node = node
122128
req.then((value) => {
123129
response.push(0) // no errors
124130
if (value !== undefined) response.push(value)
125-
api.send(encode(response, encode_params))
131+
node.send(encode(response, encode_params))
126132
}).catch((error) => {
127133
response.push(error === 0 ? null : error)
128-
api.send(encode(response, encode_params))
134+
node.send(encode(response, encode_params))
129135
})
130136
args.push(req)
131137
localProcedureCall(fn, req, args, errorInstances)
@@ -147,15 +153,15 @@ export default function createNodeFactory({ encode, decode }) {
147153
}
148154

149155
function registerLocalFunctionFromEncode(fn) {
150-
return registerLocalFunction(local_function_index++, fn)
156+
return registerLocalFunction(getNextLocalFunctionId(), fn)
151157
}
152158

153-
const api = {
159+
const node = {
154160
open,
155161
message,
156162
requests,
157163
}
158164

159-
return api
165+
return node
160166
}
161167
}

0 commit comments

Comments
 (0)