@jcbuisson/express-x-client 2.1.6 → 2.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.mjs +27 -19
package/package.json
CHANGED
package/src/index.mjs
CHANGED
|
@@ -17,42 +17,50 @@ export default function expressXClient(socket, options={}) {
|
|
|
17
17
|
const waitingPromisesByUid = {}
|
|
18
18
|
const action2service2handlers = {}
|
|
19
19
|
const type2appHandler = {}
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
// const socketConnectionState = {}
|
|
21
|
+
let connectHandler = null
|
|
22
|
+
let connectErrorHandler = null
|
|
23
|
+
let disconnectHandler = null
|
|
23
24
|
|
|
24
25
|
socket.on("connect", async () => {
|
|
25
26
|
console.log("socket connected", socket.id)
|
|
26
|
-
|
|
27
|
-
handler(socket)
|
|
28
|
-
}
|
|
27
|
+
if (connectHandler) connectHandler(socket)
|
|
29
28
|
})
|
|
30
29
|
|
|
31
30
|
socket.on("connect_error", async (err) => {
|
|
32
31
|
console.log("socket connection error", socket.id)
|
|
33
|
-
|
|
34
|
-
handler(socket, err)
|
|
35
|
-
}
|
|
32
|
+
if (connectErrorHandler) connectErrorHandler(socket, err)
|
|
36
33
|
})
|
|
37
34
|
|
|
38
35
|
socket.on("disconnect", async () => {
|
|
39
|
-
|
|
40
|
-
handler(socket)
|
|
41
|
-
}
|
|
36
|
+
if (disconnectHandler) disconnectHandler(socket)
|
|
42
37
|
})
|
|
43
38
|
|
|
44
39
|
function onConnect(func) {
|
|
45
|
-
|
|
40
|
+
connectHandler = func
|
|
46
41
|
}
|
|
47
42
|
|
|
48
43
|
function onConnectError(func) {
|
|
49
|
-
|
|
44
|
+
connectErrorHandler = func
|
|
50
45
|
}
|
|
51
46
|
|
|
52
47
|
function onDisconnect(func) {
|
|
53
|
-
|
|
48
|
+
disconnectHandler = func
|
|
54
49
|
}
|
|
55
50
|
|
|
51
|
+
|
|
52
|
+
// async function socketConnection() {
|
|
53
|
+
// const promise = new Promise((resolve, reject) => {
|
|
54
|
+
// socketConnectionState.resolve = resolve
|
|
55
|
+
// socketConnectionState.reject = reject
|
|
56
|
+
// })
|
|
57
|
+
// return promise
|
|
58
|
+
// }
|
|
59
|
+
|
|
60
|
+
// function socketStatus() {
|
|
61
|
+
// return socketConnectionState.status
|
|
62
|
+
// }
|
|
63
|
+
|
|
56
64
|
// on receiving response from service request
|
|
57
65
|
socket.on('client-response', ({ uid, error, result }) => {
|
|
58
66
|
if (options.debug) console.log('client-response', uid, error, result)
|
|
@@ -75,7 +83,7 @@ export default function expressXClient(socket, options={}) {
|
|
|
75
83
|
if (handler) handler(result)
|
|
76
84
|
})
|
|
77
85
|
|
|
78
|
-
async function serviceMethodRequest(name, action,
|
|
86
|
+
async function serviceMethodRequest(name, action, serviceOptions, ...args) {
|
|
79
87
|
// create a promise which will resolve or reject by an event 'client-response'
|
|
80
88
|
const uid = generateUID(20)
|
|
81
89
|
const promise = new Promise((resolve, reject) => {
|
|
@@ -84,7 +92,7 @@ export default function expressXClient(socket, options={}) {
|
|
|
84
92
|
setTimeout(() => {
|
|
85
93
|
delete waitingPromisesByUid[uid]
|
|
86
94
|
reject(`Error: timeout on service '${name}', action '${action}', args: ${JSON.stringify(args)}`)
|
|
87
|
-
},
|
|
95
|
+
}, serviceOptions.timeout)
|
|
88
96
|
})
|
|
89
97
|
// send request to server through websocket
|
|
90
98
|
if (options.debug) console.log('client-request', uid, name, action, args)
|
|
@@ -97,7 +105,7 @@ export default function expressXClient(socket, options={}) {
|
|
|
97
105
|
return promise
|
|
98
106
|
}
|
|
99
107
|
|
|
100
|
-
function service(name,
|
|
108
|
+
function service(name, serviceOptions={ timeout: 20000 }) {
|
|
101
109
|
const service = {
|
|
102
110
|
// associate a handler to a pub/sub event for this service
|
|
103
111
|
on: (action, handler) => {
|
|
@@ -111,7 +119,7 @@ export default function expressXClient(socket, options={}) {
|
|
|
111
119
|
get(service, action) {
|
|
112
120
|
if (!(action in service)) {
|
|
113
121
|
// newly used property `action`: define it as a service method request function
|
|
114
|
-
service[action] = (...args) => serviceMethodRequest(name, action,
|
|
122
|
+
service[action] = (...args) => serviceMethodRequest(name, action, serviceOptions, ...args)
|
|
115
123
|
}
|
|
116
124
|
return service[action]
|
|
117
125
|
}
|