@jcbuisson/express-x-client 1.5.7 → 1.5.8
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 +15 -13
package/package.json
CHANGED
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 = {}
|
|
@@ -139,11 +129,11 @@ export default function expressXClient(socket, options={}) {
|
|
|
139
129
|
const uid = generateUID(20)
|
|
140
130
|
const promise = new Promise((resolve, reject) => {
|
|
141
131
|
waitingPromisesByUid[uid] = [resolve, reject]
|
|
142
|
-
// a
|
|
132
|
+
// a timeout may also reject the promise
|
|
143
133
|
setTimeout(() => {
|
|
144
134
|
delete waitingPromisesByUid[uid]
|
|
145
135
|
reject(`Error: timeout on service '${name}', action '${action}', args: ${JSON.stringify(args)}`)
|
|
146
|
-
},
|
|
136
|
+
}, options.timeout)
|
|
147
137
|
})
|
|
148
138
|
// send request to server through websocket
|
|
149
139
|
if (options.debug) console.log('client-request', uid, name, action, args)
|
|
@@ -184,3 +174,15 @@ export default function expressXClient(socket, options={}) {
|
|
|
184
174
|
service,
|
|
185
175
|
}
|
|
186
176
|
}
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
function generateUID(length) {
|
|
180
|
+
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
|
181
|
+
let uid = '';
|
|
182
|
+
|
|
183
|
+
for (let i = 0; i < length; i++) {
|
|
184
|
+
const randomIndex = Math.floor(Math.random() * characters.length)
|
|
185
|
+
uid += characters.charAt(randomIndex)
|
|
186
|
+
}
|
|
187
|
+
return uid
|
|
188
|
+
}
|