@jcbuisson/express-x-client 2.0.2 → 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.
- package/package.json +1 -1
- package/src/index.mjs +47 -22
package/package.json
CHANGED
package/src/index.mjs
CHANGED
|
@@ -17,36 +17,58 @@ 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
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
27
|
+
// if (socketConnectionState.resolve) {
|
|
28
|
+
// socketConnectionState.resolve('ok')
|
|
29
|
+
// socketConnectionState.status = 'connected'
|
|
30
|
+
// }
|
|
31
|
+
if (connectHandler) connectHandler(socket)
|
|
28
32
|
})
|
|
29
33
|
|
|
30
|
-
socket.on("
|
|
34
|
+
socket.on("connect_error", async () => {
|
|
31
35
|
console.log("socket connection error", socket.id)
|
|
32
|
-
if (socketConnectionState.reject) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
+
// if (socketConnectionState.reject) {
|
|
37
|
+
// socketConnectionState.reject(err)
|
|
38
|
+
// socketConnectionState.status = 'error'
|
|
39
|
+
// }
|
|
40
|
+
if (connectErrorHandler) connectErrorHandler(socket)
|
|
36
41
|
})
|
|
37
42
|
|
|
38
|
-
async
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
43
|
+
socket.on("disconnect", async () => {
|
|
44
|
+
if (disconnectHandler) disconnectHandler(socket)
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
function onConnect(func) {
|
|
48
|
+
connectHandler = func
|
|
44
49
|
}
|
|
45
50
|
|
|
46
|
-
function
|
|
47
|
-
|
|
51
|
+
function onConnectError(func) {
|
|
52
|
+
connectErrorHandler = func
|
|
48
53
|
}
|
|
49
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
|
+
|
|
50
72
|
// on receiving response from service request
|
|
51
73
|
socket.on('client-response', ({ uid, error, result }) => {
|
|
52
74
|
if (options.debug) console.log('client-response', uid, error, result)
|
|
@@ -115,7 +137,8 @@ export default function expressXClient(socket, options={}) {
|
|
|
115
137
|
|
|
116
138
|
/////////////// APPLICATION-LEVEL EVENTS /////////////////
|
|
117
139
|
|
|
118
|
-
// There is a need for events sent outside any service method call, for example
|
|
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
|
|
119
142
|
socket.on('app-event', ({ type, value }) => {
|
|
120
143
|
if (options.debug) console.log('app-event', type, value)
|
|
121
144
|
if (!type2appHandler[type]) type2appHandler[type] = {}
|
|
@@ -123,14 +146,16 @@ export default function expressXClient(socket, options={}) {
|
|
|
123
146
|
if (handler) handler(value)
|
|
124
147
|
})
|
|
125
148
|
|
|
126
|
-
// add application
|
|
149
|
+
// add a handler for application-wide events
|
|
127
150
|
function on(type, handler) {
|
|
128
151
|
type2appHandler[type] = handler
|
|
129
152
|
}
|
|
130
153
|
|
|
131
154
|
return {
|
|
132
|
-
|
|
133
|
-
|
|
155
|
+
onConnect,
|
|
156
|
+
onConnectError,
|
|
157
|
+
onDisconnect,
|
|
158
|
+
|
|
134
159
|
service,
|
|
135
160
|
on,
|
|
136
161
|
}
|