@jcbuisson/express-x-client 2.1.6 → 2.1.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.mjs +27 -19
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x-client",
3
- "version": "2.1.6",
3
+ "version": "2.1.7",
4
4
  "type": "module",
5
5
  "description": "Client library for ExpressX framework",
6
6
  "main": "src/index.mjs",
package/src/index.mjs CHANGED
@@ -17,42 +17,50 @@ export default function expressXClient(socket, options={}) {
17
17
  const waitingPromisesByUid = {}
18
18
  const action2service2handlers = {}
19
19
  const type2appHandler = {}
20
- const connectHandlers = []
21
- const connectErrorHandlers = []
22
- const disconnectHandlers = []
20
+ // const socketConnectionState = {}
21
+ let connectHandler = null
22
+ let connectErrorHandler = null
23
+ let disconnectHandler = null
23
24
 
24
25
  socket.on("connect", async () => {
25
26
  console.log("socket connected", socket.id)
26
- for (const handler of connectHandlers) {
27
- handler(socket)
28
- }
27
+ if (connectHandler) connectHandler(socket)
29
28
  })
30
29
 
31
30
  socket.on("connect_error", async (err) => {
32
31
  console.log("socket connection error", socket.id)
33
- for (const handler of connectErrorHandlers) {
34
- handler(socket, err)
35
- }
32
+ if (connectErrorHandler) connectErrorHandler(socket, err)
36
33
  })
37
34
 
38
35
  socket.on("disconnect", async () => {
39
- for (const handler of disconnectHandlers) {
40
- handler(socket)
41
- }
36
+ if (disconnectHandler) disconnectHandler(socket)
42
37
  })
43
38
 
44
39
  function onConnect(func) {
45
- connectHandlers.push(func)
40
+ connectHandler = func
46
41
  }
47
42
 
48
43
  function onConnectError(func) {
49
- connectErrorHandlers.push(func)
44
+ connectErrorHandler = func
50
45
  }
51
46
 
52
47
  function onDisconnect(func) {
53
- disconnectHandlers.push(func)
48
+ disconnectHandler = func
54
49
  }
55
50
 
51
+
52
+ // async function socketConnection() {
53
+ // const promise = new Promise((resolve, reject) => {
54
+ // socketConnectionState.resolve = resolve
55
+ // socketConnectionState.reject = reject
56
+ // })
57
+ // return promise
58
+ // }
59
+
60
+ // function socketStatus() {
61
+ // return socketConnectionState.status
62
+ // }
63
+
56
64
  // on receiving response from service request
57
65
  socket.on('client-response', ({ uid, error, result }) => {
58
66
  if (options.debug) console.log('client-response', uid, error, result)
@@ -75,7 +83,7 @@ export default function expressXClient(socket, options={}) {
75
83
  if (handler) handler(result)
76
84
  })
77
85
 
78
- async function serviceMethodRequest(name, action, options, ...args) {
86
+ async function serviceMethodRequest(name, action, serviceOptions, ...args) {
79
87
  // create a promise which will resolve or reject by an event 'client-response'
80
88
  const uid = generateUID(20)
81
89
  const promise = new Promise((resolve, reject) => {
@@ -84,7 +92,7 @@ export default function expressXClient(socket, options={}) {
84
92
  setTimeout(() => {
85
93
  delete waitingPromisesByUid[uid]
86
94
  reject(`Error: timeout on service '${name}', action '${action}', args: ${JSON.stringify(args)}`)
87
- }, options.timeout)
95
+ }, serviceOptions.timeout)
88
96
  })
89
97
  // send request to server through websocket
90
98
  if (options.debug) console.log('client-request', uid, name, action, args)
@@ -97,7 +105,7 @@ export default function expressXClient(socket, options={}) {
97
105
  return promise
98
106
  }
99
107
 
100
- function service(name, options={ timeout: 5000 }) {
108
+ function service(name, serviceOptions={ timeout: 20000 }) {
101
109
  const service = {
102
110
  // associate a handler to a pub/sub event for this service
103
111
  on: (action, handler) => {
@@ -111,7 +119,7 @@ export default function expressXClient(socket, options={}) {
111
119
  get(service, action) {
112
120
  if (!(action in service)) {
113
121
  // newly used property `action`: define it as a service method request function
114
- service[action] = (...args) => serviceMethodRequest(name, action, options, ...args)
122
+ service[action] = (...args) => serviceMethodRequest(name, action, serviceOptions, ...args)
115
123
  }
116
124
  return service[action]
117
125
  }