@jcbuisson/express-x-client 2.1.7 → 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 +10 -22
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x-client",
3
- "version": "2.1.7",
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,7 +17,6 @@ export default function expressXClient(socket, options={}) {
17
17
  const waitingPromisesByUid = {}
18
18
  const action2service2handlers = {}
19
19
  const type2appHandler = {}
20
- // const socketConnectionState = {}
21
20
  let connectHandler = null
22
21
  let connectErrorHandler = null
23
22
  let disconnectHandler = null
@@ -48,19 +47,6 @@ export default function expressXClient(socket, options={}) {
48
47
  disconnectHandler = func
49
48
  }
50
49
 
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
-
64
50
  // on receiving response from service request
65
51
  socket.on('client-response', ({ uid, error, result }) => {
66
52
  if (options.debug) console.log('client-response', uid, error, result)
@@ -96,16 +82,18 @@ export default function expressXClient(socket, options={}) {
96
82
  })
97
83
  // send request to server through websocket
98
84
  if (options.debug) console.log('client-request', uid, name, action, args)
99
- socket.emit('client-request', {
100
- uid,
101
- name,
102
- action,
103
- args,
104
- })
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
+ }
105
92
  return promise
106
93
  }
107
94
 
108
- function service(name, serviceOptions={ timeout: 20000 }) {
95
+ function service(name, serviceOptions={}) {
96
+ if (serviceOptions.timeout === undefined) serviceOptions.timeout = 20000
109
97
  const service = {
110
98
  // associate a handler to a pub/sub event for this service
111
99
  on: (action, handler) => {