@jcbuisson/express-x-client 1.0.5 → 1.0.7
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.js +28 -21
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -3,33 +3,37 @@ import { v4 } from 'uuid'
|
|
|
3
3
|
|
|
4
4
|
function expressxClient(socket) {
|
|
5
5
|
|
|
6
|
-
const
|
|
6
|
+
const waitingPromisesByUid = {}
|
|
7
|
+
const timersByUid = {}
|
|
7
8
|
const action2service2handlers = {}
|
|
9
|
+
let onConnectionCallback = null
|
|
10
|
+
let onDisconnectionCallback = null
|
|
11
|
+
|
|
12
|
+
const setConnectionCallback = (callback) => {
|
|
13
|
+
onConnectionCallback = callback
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const setDisconnectionCallback = (callback) => {
|
|
17
|
+
onDisconnectionCallback = callback
|
|
18
|
+
}
|
|
8
19
|
|
|
9
20
|
// on connection
|
|
10
21
|
socket.on("connected", async (connectionId) => {
|
|
11
22
|
console.log('connected', connectionId)
|
|
12
|
-
|
|
13
|
-
// TODO: configurable
|
|
14
|
-
const accessToken = window.sessionStorage.getItem('feathers-jwt')
|
|
15
|
-
if (accessToken) {
|
|
16
|
-
// disconnect/reconnect: reauthenticate
|
|
17
|
-
console.log('reauthenticate with', accessToken)
|
|
18
|
-
serviceMethodRequest('authenticate', 'reauthenticate', accessToken)
|
|
19
|
-
}
|
|
23
|
+
if (onConnectionCallback) onConnectionCallback(connectionId)
|
|
20
24
|
})
|
|
21
25
|
|
|
22
26
|
// on receiving response from service request
|
|
23
27
|
socket.on('client-response', ({ uid, error, result }) => {
|
|
24
28
|
console.log('client-response', uid, error, result)
|
|
25
|
-
if (!uid
|
|
26
|
-
const [resolve, reject] =
|
|
29
|
+
if (!waitingPromisesByUid[uid]) return // may not exist because of timeout
|
|
30
|
+
const [resolve, reject] = waitingPromisesByUid[uid]
|
|
27
31
|
if (error) {
|
|
28
32
|
reject(error)
|
|
29
33
|
} else {
|
|
30
34
|
resolve(result)
|
|
31
35
|
}
|
|
32
|
-
delete
|
|
36
|
+
delete waitingPromisesByUid[uid]
|
|
33
37
|
})
|
|
34
38
|
|
|
35
39
|
// on receiving events from pub/sub
|
|
@@ -42,9 +46,16 @@ function expressxClient(socket) {
|
|
|
42
46
|
|
|
43
47
|
async function serviceMethodRequest(name, action, ...args) {
|
|
44
48
|
const uid = v4()
|
|
49
|
+
// create a promise which will resolve or reject by an event 'client-response'
|
|
45
50
|
const promise = new Promise((resolve, reject) => {
|
|
46
|
-
|
|
51
|
+
waitingPromisesByUid[uid] = [resolve, reject]
|
|
52
|
+
// a 5s timeout may also reject the promise
|
|
53
|
+
setTimeout(() => {
|
|
54
|
+
delete waitingPromisesByUid[uid]
|
|
55
|
+
reject(`Error: timeout on service '${name}', action '${action}', args: ${args}`)
|
|
56
|
+
}, 5000)
|
|
47
57
|
})
|
|
58
|
+
// send request to server through websocket
|
|
48
59
|
socket.emit('client-request', {
|
|
49
60
|
uid,
|
|
50
61
|
name,
|
|
@@ -63,11 +74,11 @@ function expressxClient(socket) {
|
|
|
63
74
|
serviceHandlers[name] = handler
|
|
64
75
|
},
|
|
65
76
|
}
|
|
66
|
-
// use a
|
|
77
|
+
// use a Proxy to allow for any method name for a service
|
|
67
78
|
const handler = {
|
|
68
79
|
get(service, action) {
|
|
69
80
|
if (!(action in service)) {
|
|
70
|
-
// newly used property
|
|
81
|
+
// newly used property `action`: define it as a service method request function
|
|
71
82
|
service[action] = (...args) => serviceMethodRequest(name, action, ...args)
|
|
72
83
|
}
|
|
73
84
|
return service[action]
|
|
@@ -76,14 +87,10 @@ function expressxClient(socket) {
|
|
|
76
87
|
return new Proxy(service, handler)
|
|
77
88
|
}
|
|
78
89
|
|
|
79
|
-
function logout() {
|
|
80
|
-
// TODO: configurable
|
|
81
|
-
window.sessionStorage.removeItem('feathers-jwt')
|
|
82
|
-
}
|
|
83
|
-
|
|
84
90
|
return {
|
|
91
|
+
setConnectionCallback,
|
|
92
|
+
setDisconnectionCallback,
|
|
85
93
|
service,
|
|
86
|
-
logout,
|
|
87
94
|
}
|
|
88
95
|
}
|
|
89
96
|
|