@jcbuisson/express-x-client 1.5.9 → 1.6.1
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 +28 -4
package/package.json
CHANGED
package/src/index.mjs
CHANGED
|
@@ -5,7 +5,9 @@ export default function expressXClient(socket, options={}) {
|
|
|
5
5
|
|
|
6
6
|
const waitingPromisesByUid = {}
|
|
7
7
|
const action2service2handlers = {}
|
|
8
|
+
const type2appHandlers = {}
|
|
8
9
|
let onConnectionCallback = null
|
|
10
|
+
let onReconnectionCallback = null
|
|
9
11
|
let onDisconnectionCallback = null
|
|
10
12
|
let nodeCnxId
|
|
11
13
|
|
|
@@ -13,6 +15,10 @@ export default function expressXClient(socket, options={}) {
|
|
|
13
15
|
onConnectionCallback = callback
|
|
14
16
|
}
|
|
15
17
|
|
|
18
|
+
const setReconnectionCallback = (callback) => {
|
|
19
|
+
onReconnectionCallback = callback
|
|
20
|
+
}
|
|
21
|
+
|
|
16
22
|
const setDisconnectionCallback = (callback) => {
|
|
17
23
|
onDisconnectionCallback = callback
|
|
18
24
|
}
|
|
@@ -58,9 +64,11 @@ export default function expressXClient(socket, options={}) {
|
|
|
58
64
|
if (onConnectionCallback) onConnectionCallback(connectionId)
|
|
59
65
|
})
|
|
60
66
|
|
|
61
|
-
socket.on("cnx-transfer-ack", async (
|
|
62
|
-
if (options.debug) console.log('cnx-transfer-ack',
|
|
63
|
-
_setCnxId(
|
|
67
|
+
socket.on("cnx-transfer-ack", async (connection) => {
|
|
68
|
+
if (options.debug) console.log('cnx-transfer-ack', connection)
|
|
69
|
+
_setCnxId(connection.id)
|
|
70
|
+
// call user-defined reconnection callback
|
|
71
|
+
if (onReconnectionCallback) onReconnectionCallback(connection)
|
|
64
72
|
})
|
|
65
73
|
|
|
66
74
|
|
|
@@ -103,7 +111,7 @@ export default function expressXClient(socket, options={}) {
|
|
|
103
111
|
delete waitingPromisesByUid[uid]
|
|
104
112
|
})
|
|
105
113
|
|
|
106
|
-
// on receiving events from pub/sub
|
|
114
|
+
// on receiving service events from pub/sub
|
|
107
115
|
socket.on('service-event', ({ name, action, result }) => {
|
|
108
116
|
if (options.debug) console.log('service-event', name, action, result)
|
|
109
117
|
if (!action2service2handlers[action]) action2service2handlers[action] = {}
|
|
@@ -112,6 +120,14 @@ export default function expressXClient(socket, options={}) {
|
|
|
112
120
|
if (handler) handler(result)
|
|
113
121
|
})
|
|
114
122
|
|
|
123
|
+
// on receiving application events from pub/sub
|
|
124
|
+
socket.on('app-event', ({ type, value }) => {
|
|
125
|
+
if (options.debug) console.log('app-event', type, value)
|
|
126
|
+
if (!type2appHandlers[type]) type2appHandlers[type] = {}
|
|
127
|
+
const handler = type2appHandlers[type]
|
|
128
|
+
if (handler) handler(value)
|
|
129
|
+
})
|
|
130
|
+
|
|
115
131
|
function wait(ms) {
|
|
116
132
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
117
133
|
}
|
|
@@ -147,6 +163,12 @@ export default function expressXClient(socket, options={}) {
|
|
|
147
163
|
return promise
|
|
148
164
|
}
|
|
149
165
|
|
|
166
|
+
// define application events handlers
|
|
167
|
+
function on(type, handler) {
|
|
168
|
+
if (!type2appHandlers[type]) type2appHandlers[type] = {}
|
|
169
|
+
type2appHandlers[type] = handler
|
|
170
|
+
}
|
|
171
|
+
|
|
150
172
|
function service(name) {
|
|
151
173
|
const service = {
|
|
152
174
|
// associate a handler to a pub/sub event for this service
|
|
@@ -171,8 +193,10 @@ export default function expressXClient(socket, options={}) {
|
|
|
171
193
|
|
|
172
194
|
return {
|
|
173
195
|
setConnectionCallback,
|
|
196
|
+
setReconnectionCallback,
|
|
174
197
|
setDisconnectionCallback,
|
|
175
198
|
service,
|
|
199
|
+
on,
|
|
176
200
|
}
|
|
177
201
|
}
|
|
178
202
|
|