Skip to content

Commit c4a4b52

Browse files
committed
Naming changes, Function is now Rpc
1 parent 4a8cb16 commit c4a4b52

File tree

5 files changed

+75
-31
lines changed

5 files changed

+75
-31
lines changed

src/api/createNodeFactory.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,22 @@ export default function createNodeFactory({ encode, decode }) {
1212
} = {}) {
1313
const requests = {}
1414
let request_id_index = 0
15-
const local_functions_id = {}
16-
const local_functions = new Map()
15+
const local_rpcs_id = {}
16+
const local_rpcs = new Map()
1717
let local_function_index = 1 // 0 is reserved for the entry function used in open()
1818

1919
const encode_params = {
20-
local_functions,
21-
registerLocalFunctionFromEncode,
20+
local_rpcs,
21+
registerLocalRpcFromEncode,
2222
}
2323

24-
function registerLocalFunction(function_id, fn) {
25-
local_functions_id[function_id] = fn
26-
local_functions.set(fn, function_id)
24+
function registerLocalRpc(function_id, fn) {
25+
local_rpcs_id[function_id] = fn
26+
local_rpcs.set(fn, function_id)
2727
return function_id
2828
}
2929

30-
function createRemoteFunction({
31-
function_id,
32-
function_creator,
33-
caller,
34-
path,
35-
}) {
30+
function createRpc(function_id) {
3631
const makeCall = (request_id, args) => {
3732
const data = [request_id, function_id]
3833
if (args.length > 0) data.push(args)
@@ -62,8 +57,17 @@ export default function createNodeFactory({ encode, decode }) {
6257
makeCall(0, args)
6358
}
6459

60+
return rpc
61+
}
62+
63+
function createRemoteFunction({
64+
function_id,
65+
function_creator,
66+
caller,
67+
path,
68+
}) {
6569
return rpcFilter({
66-
rpc,
70+
rpc: createRpc(function_id),
6771
node,
6872
function_id,
6973
function_creator,
@@ -73,15 +77,15 @@ export default function createNodeFactory({ encode, decode }) {
7377
}
7478

7579
function getNextLocalFunctionId() {
76-
while (local_functions_id.hasOwnProperty(local_function_index)) {
80+
while (local_rpcs_id.hasOwnProperty(local_function_index)) {
7781
local_function_index += 1
7882
}
7983
return local_function_index
8084
}
8185

8286
function open(send, fn) {
8387
const function_id = 0
84-
if (isFunction(fn)) registerLocalFunction(function_id, fn)
88+
if (isFunction(fn)) registerLocalRpc(function_id, fn)
8589
node.send = (msg) => send(serialize(msg))
8690
return createRemoteFunction({
8791
function_id,
@@ -97,7 +101,7 @@ export default function createNodeFactory({ encode, decode }) {
97101

98102
const [id, function_id] = msg
99103
const is_request = id > -1
100-
const fn = is_request ? local_functions_id[function_id] : undefined
104+
const fn = is_request ? local_rpcs_id[function_id] : undefined
101105

102106
msg = decode(msg, {
103107
createRemoteFunction,
@@ -152,8 +156,8 @@ export default function createNodeFactory({ encode, decode }) {
152156
return false
153157
}
154158

155-
function registerLocalFunctionFromEncode(fn) {
156-
return registerLocalFunction(getNextLocalFunctionId(), fn)
159+
function registerLocalRpcFromEncode(fn) {
160+
return registerLocalRpc(getNextLocalFunctionId(), fn)
157161
}
158162

159163
const node = {

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import createNodeFactory from './api/createNodeFactory'
66
import createStoreFactory from './api/createStoreFactory'
77
import applyPatchFactory from './api/applyPatchFactory'
88

9-
import Function from './types/Function'
9+
import Rpc from './types/Rpc'
1010
import Primitives from './types/Primitives'
1111
import Delete from './types/Delete'
1212
import Replace from './types/Replace'
@@ -31,7 +31,7 @@ function factory() {
3131
if (isFunction(decode)) decoders.push(decode)
3232
}
3333

34-
addType(Function)
34+
addType(Rpc)
3535
addType(Primitives)
3636
addType(Delete)
3737
addType(Replace)

src/types/Function.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,21 @@ import { isValidToEscape, isValidToDecode } from '../util/isValid'
33
import { getUniqueKey } from '../util/get'
44
import { isInteger, isFunction } from '../util/is'
55

6-
export default function Function() {}
6+
export default function Rpc() {}
77

8-
Function.encode = function ({
9-
value,
10-
local_functions,
11-
registerLocalFunctionFromEncode,
12-
}) {
8+
Rpc.encode = function ({ value, local_rpcs, registerLocalRpcFromEncode }) {
139
if (isFunction(value)) {
14-
const function_id = local_functions.has(value)
15-
? local_functions.get(value)
16-
: registerLocalFunctionFromEncode(value)
10+
const function_id = local_rpcs.has(value)
11+
? local_rpcs.get(value)
12+
: registerLocalRpcFromEncode(value)
1713
return { [FUNCTION_KEY]: function_id }
1814
} else if (isValidToDecode({ value, key: FUNCTION_KEY })) {
1915
return { [ESCAPE_KEY]: value }
2016
}
2117
return value
2218
}
2319

24-
Function.decode = function ({
20+
Rpc.decode = function ({
2521
value,
2622
createRemoteFunction,
2723
path,

src/types/Rpc.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { ESCAPE_KEY, FUNCTION_KEY } from '../const'
2+
import { isValidToEscape, isValidToDecode } from '../util/isValid'
3+
import { getUniqueKey } from '../util/get'
4+
import { isInteger, isFunction } from '../util/is'
5+
6+
export default function Rpc() {}
7+
8+
Rpc.encode = function ({ value, local_rpcs, registerLocalRpcFromEncode }) {
9+
if (isFunction(value)) {
10+
const function_id = local_rpcs.has(value)
11+
? local_rpcs.get(value)
12+
: registerLocalRpcFromEncode(value)
13+
return { [FUNCTION_KEY]: function_id }
14+
} else if (isValidToDecode({ value, key: FUNCTION_KEY })) {
15+
return { [ESCAPE_KEY]: value }
16+
}
17+
return value
18+
}
19+
20+
Rpc.decode = function ({
21+
value,
22+
createRemoteFunction,
23+
path,
24+
caller,
25+
function_creator,
26+
}) {
27+
if (
28+
getUniqueKey(value) === FUNCTION_KEY &&
29+
isInteger(value[FUNCTION_KEY])
30+
) {
31+
return createRemoteFunction({
32+
function_id: value[FUNCTION_KEY],
33+
function_creator,
34+
caller,
35+
path: path.slice(2),
36+
})
37+
} else if (
38+
isValidToEscape({ value }) &&
39+
isValidToDecode({ value: value[ESCAPE_KEY], key: FUNCTION_KEY })
40+
) {
41+
return value[ESCAPE_KEY]
42+
}
43+
return value
44+
}
File renamed without changes.

0 commit comments

Comments
 (0)