@jcbuisson/express-x-client 1.5.9 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.mjs +17 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x-client",
3
- "version": "1.5.9",
3
+ "version": "1.6.0",
4
4
  "type": "module",
5
5
  "description": "Client library for ExpressX framework",
6
6
  "main": "src/index.mjs",
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,7 +104,7 @@ 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 }) => {
108
109
  if (options.debug) console.log('service-event', name, action, result)
109
110
  if (!action2service2handlers[action]) action2service2handlers[action] = {}
@@ -112,6 +113,14 @@ export default function expressXClient(socket, options={}) {
112
113
  if (handler) handler(result)
113
114
  })
114
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
+
115
124
  function wait(ms) {
116
125
  return new Promise(resolve => setTimeout(resolve, ms));
117
126
  }
@@ -147,6 +156,12 @@ export default function expressXClient(socket, options={}) {
147
156
  return promise
148
157
  }
149
158
 
159
+ // define application events handlers
160
+ function on(type, handler) {
161
+ if (!type2appHandlers[type]) type2appHandlers[type] = {}
162
+ type2appHandlers[type] = handler
163
+ }
164
+
150
165
  function service(name) {
151
166
  const service = {
152
167
  // associate a handler to a pub/sub event for this service
@@ -173,6 +188,7 @@ export default function expressXClient(socket, options={}) {
173
188
  setConnectionCallback,
174
189
  setDisconnectionCallback,
175
190
  service,
191
+ on,
176
192
  }
177
193
  }
178
194