@jcbuisson/express-x-client 2.1.6 → 2.1.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.mjs +22 -26
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.8",
4
4
  "type": "module",
5
5
  "description": "Client library for ExpressX framework",
6
6
  "main": "src/index.mjs",
package/src/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  function generateUID(length) {
3
3
  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
4
- let uid = '';
4
+ let uid = ''
5
5
 
6
6
  for (let i = 0; i < length; i++) {
7
7
  const randomIndex = Math.floor(Math.random() * characters.length)
@@ -17,40 +17,34 @@ 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
+ let connectHandler = null
21
+ let connectErrorHandler = null
22
+ let disconnectHandler = null
23
23
 
24
24
  socket.on("connect", async () => {
25
25
  console.log("socket connected", socket.id)
26
- for (const handler of connectHandlers) {
27
- handler(socket)
28
- }
26
+ if (connectHandler) connectHandler(socket)
29
27
  })
30
28
 
31
29
  socket.on("connect_error", async (err) => {
32
30
  console.log("socket connection error", socket.id)
33
- for (const handler of connectErrorHandlers) {
34
- handler(socket, err)
35
- }
31
+ if (connectErrorHandler) connectErrorHandler(socket, err)
36
32
  })
37
33
 
38
34
  socket.on("disconnect", async () => {
39
- for (const handler of disconnectHandlers) {
40
- handler(socket)
41
- }
35
+ if (disconnectHandler) disconnectHandler(socket)
42
36
  })
43
37
 
44
38
  function onConnect(func) {
45
- connectHandlers.push(func)
39
+ connectHandler = func
46
40
  }
47
41
 
48
42
  function onConnectError(func) {
49
- connectErrorHandlers.push(func)
43
+ connectErrorHandler = func
50
44
  }
51
45
 
52
46
  function onDisconnect(func) {
53
- disconnectHandlers.push(func)
47
+ disconnectHandler = func
54
48
  }
55
49
 
56
50
  // on receiving response from service request
@@ -75,7 +69,7 @@ export default function expressXClient(socket, options={}) {
75
69
  if (handler) handler(result)
76
70
  })
77
71
 
78
- async function serviceMethodRequest(name, action, options, ...args) {
72
+ async function serviceMethodRequest(name, action, serviceOptions, ...args) {
79
73
  // create a promise which will resolve or reject by an event 'client-response'
80
74
  const uid = generateUID(20)
81
75
  const promise = new Promise((resolve, reject) => {
@@ -84,20 +78,22 @@ export default function expressXClient(socket, options={}) {
84
78
  setTimeout(() => {
85
79
  delete waitingPromisesByUid[uid]
86
80
  reject(`Error: timeout on service '${name}', action '${action}', args: ${JSON.stringify(args)}`)
87
- }, options.timeout)
81
+ }, serviceOptions.timeout)
88
82
  })
89
83
  // send request to server through websocket
90
84
  if (options.debug) console.log('client-request', uid, name, action, args)
91
- socket.emit('client-request', {
92
- uid,
93
- name,
94
- action,
95
- args,
96
- })
85
+ if (serviceOptions.volatile) {
86
+ // event is not sent if connection is not active
87
+ socket.volatile.emit('client-request', { uid, name, action, args, })
88
+ } else {
89
+ // event is buffered if connection is not active (default)
90
+ socket.emit('client-request', { uid, name, action, args, })
91
+ }
97
92
  return promise
98
93
  }
99
94
 
100
- function service(name, options={ timeout: 5000 }) {
95
+ function service(name, serviceOptions={}) {
96
+ if (serviceOptions.timeout === undefined) serviceOptions.timeout = 20000
101
97
  const service = {
102
98
  // associate a handler to a pub/sub event for this service
103
99
  on: (action, handler) => {
@@ -111,7 +107,7 @@ export default function expressXClient(socket, options={}) {
111
107
  get(service, action) {
112
108
  if (!(action in service)) {
113
109
  // newly used property `action`: define it as a service method request function
114
- service[action] = (...args) => serviceMethodRequest(name, action, options, ...args)
110
+ service[action] = (...args) => serviceMethodRequest(name, action, serviceOptions, ...args)
115
111
  }
116
112
  return service[action]
117
113
  }