@jcbuisson/express-x-client 1.5.7 → 1.5.9

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 +16 -13
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x-client",
3
- "version": "1.5.7",
3
+ "version": "1.5.9",
4
4
  "type": "module",
5
5
  "description": "Client library for ExpressX framework",
6
6
  "main": "src/index.mjs",
package/src/index.mjs CHANGED
@@ -1,17 +1,7 @@
1
-
2
- function generateUID(length) {
3
- const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
4
- let uid = '';
5
-
6
- for (let i = 0; i < length; i++) {
7
- const randomIndex = Math.floor(Math.random() * characters.length)
8
- uid += characters.charAt(randomIndex)
9
- }
10
- return uid
11
- }
12
1
 
13
2
  export default function expressXClient(socket, options={}) {
14
3
  if (options.debug === undefined) options.debug = false
4
+ if (options.timeout === undefined) options.timeout = 5000
15
5
 
16
6
  const waitingPromisesByUid = {}
17
7
  const action2service2handlers = {}
@@ -115,6 +105,7 @@ export default function expressXClient(socket, options={}) {
115
105
 
116
106
  // on receiving events from pub/sub
117
107
  socket.on('service-event', ({ name, action, result }) => {
108
+ if (options.debug) console.log('service-event', name, action, result)
118
109
  if (!action2service2handlers[action]) action2service2handlers[action] = {}
119
110
  const serviceHandlers = action2service2handlers[action]
120
111
  const handler = serviceHandlers[name]
@@ -139,11 +130,11 @@ export default function expressXClient(socket, options={}) {
139
130
  const uid = generateUID(20)
140
131
  const promise = new Promise((resolve, reject) => {
141
132
  waitingPromisesByUid[uid] = [resolve, reject]
142
- // a 5s timeout may also reject the promise
133
+ // a timeout may also reject the promise
143
134
  setTimeout(() => {
144
135
  delete waitingPromisesByUid[uid]
145
136
  reject(`Error: timeout on service '${name}', action '${action}', args: ${JSON.stringify(args)}`)
146
- }, 5000)
137
+ }, options.timeout)
147
138
  })
148
139
  // send request to server through websocket
149
140
  if (options.debug) console.log('client-request', uid, name, action, args)
@@ -184,3 +175,15 @@ export default function expressXClient(socket, options={}) {
184
175
  service,
185
176
  }
186
177
  }
178
+
179
+
180
+ function generateUID(length) {
181
+ const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
182
+ let uid = '';
183
+
184
+ for (let i = 0; i < length; i++) {
185
+ const randomIndex = Math.floor(Math.random() * characters.length)
186
+ uid += characters.charAt(randomIndex)
187
+ }
188
+ return uid
189
+ }