@jcbuisson/express-x-client 2.0.1 → 2.1.0

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 +62 -28
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x-client",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "type": "module",
5
5
  "description": "Client library for ExpressX framework",
6
6
  "main": "src/index.mjs",
package/src/index.mjs CHANGED
@@ -16,36 +16,59 @@ export default function expressXClient(socket, options={}) {
16
16
 
17
17
  const waitingPromisesByUid = {}
18
18
  const action2service2handlers = {}
19
- const socketConnectionState = {}
19
+ const type2appHandler = {}
20
+ // const socketConnectionState = {}
21
+ let connectHandler = null
22
+ let connectErrorHandler = null
23
+ let disconnectHandler = null
20
24
 
21
25
  socket.on("connect", async () => {
22
26
  console.log("socket connected", socket.id)
23
- if (socketConnectionState.resolve) {
24
- socketConnectionState.resolve('ok')
25
- socketConnectionState.status = 'connected'
26
- }
27
+ // if (socketConnectionState.resolve) {
28
+ // socketConnectionState.resolve('ok')
29
+ // socketConnectionState.status = 'connected'
30
+ // }
31
+ if (connectHandler) connectHandler(socket)
27
32
  })
28
33
 
29
- socket.on("error", async () => {
34
+ socket.on("connect_error", async () => {
30
35
  console.log("socket connection error", socket.id)
31
- if (socketConnectionState.reject) {
32
- socketConnectionState.reject(err)
33
- socketConnectionState.status = 'error'
34
- }
36
+ // if (socketConnectionState.reject) {
37
+ // socketConnectionState.reject(err)
38
+ // socketConnectionState.status = 'error'
39
+ // }
40
+ if (connectErrorHandler) connectErrorHandler(socket)
35
41
  })
36
42
 
37
- async function socketConnection() {
38
- const promise = new Promise((resolve, reject) => {
39
- socketConnectionState.resolve = resolve
40
- socketConnectionState.reject = reject
41
- })
42
- return promise
43
+ socket.on("disconnect", async () => {
44
+ if (disconnectHandler) disconnectHandler(socket)
45
+ })
46
+
47
+ function onConnect(func) {
48
+ connectHandler = func
43
49
  }
44
50
 
45
- function socketStatus() {
46
- return socketConnectionState.status
51
+ function onConnectError(func) {
52
+ connectErrorHandler = func
47
53
  }
48
54
 
55
+ function onDisconnect(func) {
56
+ disconnectHandler = func
57
+ }
58
+
59
+
60
+ // async function socketConnection() {
61
+ // const promise = new Promise((resolve, reject) => {
62
+ // socketConnectionState.resolve = resolve
63
+ // socketConnectionState.reject = reject
64
+ // })
65
+ // return promise
66
+ // }
67
+
68
+ // function socketStatus() {
69
+ // return socketConnectionState.status
70
+ // }
71
+
49
72
  // on receiving response from service request
50
73
  socket.on('client-response', ({ uid, error, result }) => {
51
74
  if (options.debug) console.log('client-response', uid, error, result)
@@ -67,14 +90,6 @@ export default function expressXClient(socket, options={}) {
67
90
  const handler = serviceHandlers[name]
68
91
  if (handler) handler(result)
69
92
  })
70
-
71
- // on receiving application events from pub/sub
72
- socket.on('app-event', ({ type, value }) => {
73
- if (options.debug) console.log('app-event', type, value)
74
- if (!type2appHandlers[type]) type2appHandlers[type] = {}
75
- const handler = type2appHandlers[type]
76
- if (handler) handler(value)
77
- })
78
93
 
79
94
  async function serviceMethodRequest(name, action, options, ...args) {
80
95
  // create a promise which will resolve or reject by an event 'client-response'
@@ -120,10 +135,29 @@ export default function expressXClient(socket, options={}) {
120
135
  return new Proxy(service, handler)
121
136
  }
122
137
 
138
+ /////////////// APPLICATION-LEVEL EVENTS /////////////////
139
+
140
+ // There is a need for application-wide events sent outside any service method call, for example when backend state changes
141
+ // without front-end interactions
142
+ socket.on('app-event', ({ type, value }) => {
143
+ if (options.debug) console.log('app-event', type, value)
144
+ if (!type2appHandler[type]) type2appHandler[type] = {}
145
+ const handler = type2appHandler[type]
146
+ if (handler) handler(value)
147
+ })
148
+
149
+ // add a handler for application-wide events
150
+ function on(type, handler) {
151
+ type2appHandler[type] = handler
152
+ }
153
+
123
154
  return {
124
- socketConnection,
125
- socketStatus,
155
+ onConnect,
156
+ onConnectError,
157
+ onDisconnect,
158
+
126
159
  service,
160
+ on,
127
161
  }
128
162
  }
129
163