@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.
- package/package.json +1 -1
- package/src/index.mjs +28 -23
package/package.json
CHANGED
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 (
|
|
25
|
-
socketConnectionState.resolve('ok')
|
|
26
|
-
socketConnectionState.status = 'connected'
|
|
27
|
-
}
|
|
27
|
+
if (connectHandler) connectHandler(socket)
|
|
28
28
|
})
|
|
29
29
|
|
|
30
|
-
socket.on("
|
|
30
|
+
socket.on("connect_error", async (err) => {
|
|
31
31
|
console.log("socket connection error", socket.id)
|
|
32
|
-
if (
|
|
33
|
-
socketConnectionState.reject(err)
|
|
34
|
-
socketConnectionState.status = 'error'
|
|
35
|
-
}
|
|
32
|
+
if (connectErrorHandler) connectErrorHandler(socket, err)
|
|
36
33
|
})
|
|
37
34
|
|
|
38
|
-
async
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
|
47
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
133
|
-
|
|
136
|
+
onConnect,
|
|
137
|
+
onConnectError,
|
|
138
|
+
onDisconnect,
|
|
139
|
+
|
|
134
140
|
service,
|
|
135
141
|
on,
|
|
136
142
|
}
|
|
137
143
|
}
|
|
138
|
-
|