@jcbuisson/express-x-client 2.0.2 → 2.1.5

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 +28 -23
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x-client",
3
- "version": "2.0.2",
3
+ "version": "2.1.5",
4
4
  "type": "module",
5
5
  "description": "Client library for ExpressX framework",
6
6
  "main": "src/index.mjs",
package/src/index.mjs CHANGED
@@ -17,34 +17,35 @@ export default function expressXClient(socket, options={}) {
17
17
  const waitingPromisesByUid = {}
18
18
  const action2service2handlers = {}
19
19
  const type2appHandler = {}
20
- const socketConnectionState = {}
20
+ // const socketConnectionState = {}
21
+ let connectHandler = null
22
+ let connectErrorHandler = null
23
+ let disconnectHandler = null
21
24
 
22
25
  socket.on("connect", async () => {
23
26
  console.log("socket connected", socket.id)
24
- if (socketConnectionState.resolve) {
25
- socketConnectionState.resolve('ok')
26
- socketConnectionState.status = 'connected'
27
- }
27
+ if (connectHandler) connectHandler(socket)
28
28
  })
29
29
 
30
- socket.on("error", async () => {
30
+ socket.on("connect_error", async (err) => {
31
31
  console.log("socket connection error", socket.id)
32
- if (socketConnectionState.reject) {
33
- socketConnectionState.reject(err)
34
- socketConnectionState.status = 'error'
35
- }
32
+ if (connectErrorHandler) connectErrorHandler(socket, err)
36
33
  })
37
34
 
38
- async function socketConnection() {
39
- const promise = new Promise((resolve, reject) => {
40
- socketConnectionState.resolve = resolve
41
- socketConnectionState.reject = reject
42
- })
43
- return promise
35
+ socket.on("disconnect", async () => {
36
+ if (disconnectHandler) disconnectHandler(socket)
37
+ })
38
+
39
+ function onConnect(func) {
40
+ connectHandler = func
41
+ }
42
+
43
+ function onConnectError(func) {
44
+ connectErrorHandler = func
44
45
  }
45
46
 
46
- function socketStatus() {
47
- return socketConnectionState.status
47
+ function onDisconnect(func) {
48
+ disconnectHandler = func
48
49
  }
49
50
 
50
51
  // on receiving response from service request
@@ -115,24 +116,28 @@ export default function expressXClient(socket, options={}) {
115
116
 
116
117
  /////////////// APPLICATION-LEVEL EVENTS /////////////////
117
118
 
118
- // There is a need for events sent outside any service method call, for example on unsolicited-by-frontend backend state change
119
+ // There is a need for application-wide events sent outside any service method call, for example when backend state changes
120
+ // without front-end interactions
119
121
  socket.on('app-event', ({ type, value }) => {
120
122
  if (options.debug) console.log('app-event', type, value)
121
123
  if (!type2appHandler[type]) type2appHandler[type] = {}
122
124
  const handler = type2appHandler[type]
125
+ console.log('handler', handler)
123
126
  if (handler) handler(value)
124
127
  })
125
128
 
126
- // add application event handler
129
+ // add a handler for application-wide events
127
130
  function on(type, handler) {
128
131
  type2appHandler[type] = handler
132
+ console.log('type2appHandler[type]', type2appHandler[type])
129
133
  }
130
134
 
131
135
  return {
132
- socketConnection,
133
- socketStatus,
136
+ onConnect,
137
+ onConnectError,
138
+ onDisconnect,
139
+
134
140
  service,
135
141
  on,
136
142
  }
137
143
  }
138
-