@jcbuisson/express-x-client 2.1.0 → 2.1.6
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 +18 -33
package/package.json
CHANGED
package/src/index.mjs
CHANGED
|
@@ -17,58 +17,42 @@ export default function expressXClient(socket, options={}) {
|
|
|
17
17
|
const waitingPromisesByUid = {}
|
|
18
18
|
const action2service2handlers = {}
|
|
19
19
|
const type2appHandler = {}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
let disconnectHandler = null
|
|
20
|
+
const connectHandlers = []
|
|
21
|
+
const connectErrorHandlers = []
|
|
22
|
+
const disconnectHandlers = []
|
|
24
23
|
|
|
25
24
|
socket.on("connect", async () => {
|
|
26
25
|
console.log("socket connected", socket.id)
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
// }
|
|
31
|
-
if (connectHandler) connectHandler(socket)
|
|
26
|
+
for (const handler of connectHandlers) {
|
|
27
|
+
handler(socket)
|
|
28
|
+
}
|
|
32
29
|
})
|
|
33
30
|
|
|
34
|
-
socket.on("connect_error", async () => {
|
|
31
|
+
socket.on("connect_error", async (err) => {
|
|
35
32
|
console.log("socket connection error", socket.id)
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
// }
|
|
40
|
-
if (connectErrorHandler) connectErrorHandler(socket)
|
|
33
|
+
for (const handler of connectErrorHandlers) {
|
|
34
|
+
handler(socket, err)
|
|
35
|
+
}
|
|
41
36
|
})
|
|
42
37
|
|
|
43
38
|
socket.on("disconnect", async () => {
|
|
44
|
-
|
|
39
|
+
for (const handler of disconnectHandlers) {
|
|
40
|
+
handler(socket)
|
|
41
|
+
}
|
|
45
42
|
})
|
|
46
43
|
|
|
47
44
|
function onConnect(func) {
|
|
48
|
-
|
|
45
|
+
connectHandlers.push(func)
|
|
49
46
|
}
|
|
50
47
|
|
|
51
48
|
function onConnectError(func) {
|
|
52
|
-
|
|
49
|
+
connectErrorHandlers.push(func)
|
|
53
50
|
}
|
|
54
51
|
|
|
55
52
|
function onDisconnect(func) {
|
|
56
|
-
|
|
53
|
+
disconnectHandlers.push(func)
|
|
57
54
|
}
|
|
58
55
|
|
|
59
|
-
|
|
60
|
-
// async function socketConnection() {
|
|
61
|
-
// const promise = new Promise((resolve, reject) => {
|
|
62
|
-
// socketConnectionState.resolve = resolve
|
|
63
|
-
// socketConnectionState.reject = reject
|
|
64
|
-
// })
|
|
65
|
-
// return promise
|
|
66
|
-
// }
|
|
67
|
-
|
|
68
|
-
// function socketStatus() {
|
|
69
|
-
// return socketConnectionState.status
|
|
70
|
-
// }
|
|
71
|
-
|
|
72
56
|
// on receiving response from service request
|
|
73
57
|
socket.on('client-response', ({ uid, error, result }) => {
|
|
74
58
|
if (options.debug) console.log('client-response', uid, error, result)
|
|
@@ -143,12 +127,14 @@ export default function expressXClient(socket, options={}) {
|
|
|
143
127
|
if (options.debug) console.log('app-event', type, value)
|
|
144
128
|
if (!type2appHandler[type]) type2appHandler[type] = {}
|
|
145
129
|
const handler = type2appHandler[type]
|
|
130
|
+
console.log('handler', handler)
|
|
146
131
|
if (handler) handler(value)
|
|
147
132
|
})
|
|
148
133
|
|
|
149
134
|
// add a handler for application-wide events
|
|
150
135
|
function on(type, handler) {
|
|
151
136
|
type2appHandler[type] = handler
|
|
137
|
+
console.log('type2appHandler[type]', type2appHandler[type])
|
|
152
138
|
}
|
|
153
139
|
|
|
154
140
|
return {
|
|
@@ -160,4 +146,3 @@ export default function expressXClient(socket, options={}) {
|
|
|
160
146
|
on,
|
|
161
147
|
}
|
|
162
148
|
}
|
|
163
|
-
|