@jcbuisson/express-x-client 1.0.6 → 1.0.7

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.js +14 -12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x-client",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "repository": {
package/src/index.js CHANGED
@@ -3,7 +3,8 @@ import { v4 } from 'uuid'
3
3
 
4
4
  function expressxClient(socket) {
5
5
 
6
- const waitingPromises = {}
6
+ const waitingPromisesByUid = {}
7
+ const timersByUid = {}
7
8
  const action2service2handlers = {}
8
9
  let onConnectionCallback = null
9
10
  let onDisconnectionCallback = null
@@ -24,18 +25,15 @@ function expressxClient(socket) {
24
25
 
25
26
  // on receiving response from service request
26
27
  socket.on('client-response', ({ uid, error, result }) => {
27
- // console.log('client-response', uid, error, result)
28
- if (!uid in waitingPromises) return // ???
29
-
30
- // ANNULER TIMER
31
-
32
- const [resolve, reject] = waitingPromises[uid]
28
+ console.log('client-response', uid, error, result)
29
+ if (!waitingPromisesByUid[uid]) return // may not exist because of timeout
30
+ const [resolve, reject] = waitingPromisesByUid[uid]
33
31
  if (error) {
34
32
  reject(error)
35
33
  } else {
36
34
  resolve(result)
37
35
  }
38
- delete waitingPromises[uid]
36
+ delete waitingPromisesByUid[uid]
39
37
  })
40
38
 
41
39
  // on receiving events from pub/sub
@@ -48,12 +46,16 @@ function expressxClient(socket) {
48
46
 
49
47
  async function serviceMethodRequest(name, action, ...args) {
50
48
  const uid = v4()
49
+ // create a promise which will resolve or reject by an event 'client-response'
51
50
  const promise = new Promise((resolve, reject) => {
52
- waitingPromises[uid] = [resolve, reject]
53
-
54
- // CREER TIMER
55
-
51
+ waitingPromisesByUid[uid] = [resolve, reject]
52
+ // a 5s timeout may also reject the promise
53
+ setTimeout(() => {
54
+ delete waitingPromisesByUid[uid]
55
+ reject(`Error: timeout on service '${name}', action '${action}', args: ${args}`)
56
+ }, 5000)
56
57
  })
58
+ // send request to server through websocket
57
59
  socket.emit('client-request', {
58
60
  uid,
59
61
  name,