@jcbuisson/express-x-client 1.5.8 → 1.6.0
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 -1
package/package.json
CHANGED
package/src/index.mjs
CHANGED
|
@@ -5,6 +5,7 @@ export default function expressXClient(socket, options={}) {
|
|
|
5
5
|
|
|
6
6
|
const waitingPromisesByUid = {}
|
|
7
7
|
const action2service2handlers = {}
|
|
8
|
+
const type2appHandlers = {}
|
|
8
9
|
let onConnectionCallback = null
|
|
9
10
|
let onDisconnectionCallback = null
|
|
10
11
|
let nodeCnxId
|
|
@@ -103,14 +104,23 @@ export default function expressXClient(socket, options={}) {
|
|
|
103
104
|
delete waitingPromisesByUid[uid]
|
|
104
105
|
})
|
|
105
106
|
|
|
106
|
-
// on receiving events from pub/sub
|
|
107
|
+
// on receiving service events from pub/sub
|
|
107
108
|
socket.on('service-event', ({ name, action, result }) => {
|
|
109
|
+
if (options.debug) console.log('service-event', name, action, result)
|
|
108
110
|
if (!action2service2handlers[action]) action2service2handlers[action] = {}
|
|
109
111
|
const serviceHandlers = action2service2handlers[action]
|
|
110
112
|
const handler = serviceHandlers[name]
|
|
111
113
|
if (handler) handler(result)
|
|
112
114
|
})
|
|
113
115
|
|
|
116
|
+
// on receiving application events from pub/sub
|
|
117
|
+
socket.on('app-event', ({ type, value }) => {
|
|
118
|
+
if (options.debug) console.log('app-event', type, value)
|
|
119
|
+
if (!type2appHandlers[type]) type2appHandlers[type] = {}
|
|
120
|
+
const handler = type2appHandlers[type]
|
|
121
|
+
if (handler) handler(value)
|
|
122
|
+
})
|
|
123
|
+
|
|
114
124
|
function wait(ms) {
|
|
115
125
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
116
126
|
}
|
|
@@ -146,6 +156,12 @@ export default function expressXClient(socket, options={}) {
|
|
|
146
156
|
return promise
|
|
147
157
|
}
|
|
148
158
|
|
|
159
|
+
// define application events handlers
|
|
160
|
+
function on(type, handler) {
|
|
161
|
+
if (!type2appHandlers[type]) type2appHandlers[type] = {}
|
|
162
|
+
type2appHandlers[type] = handler
|
|
163
|
+
}
|
|
164
|
+
|
|
149
165
|
function service(name) {
|
|
150
166
|
const service = {
|
|
151
167
|
// associate a handler to a pub/sub event for this service
|
|
@@ -172,6 +188,7 @@ export default function expressXClient(socket, options={}) {
|
|
|
172
188
|
setConnectionCallback,
|
|
173
189
|
setDisconnectionCallback,
|
|
174
190
|
service,
|
|
191
|
+
on,
|
|
175
192
|
}
|
|
176
193
|
}
|
|
177
194
|
|