@jcbuisson/express-x-client 1.6.2 → 2.0.1
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 +37 -121
package/package.json
CHANGED
package/src/index.mjs
CHANGED
|
@@ -1,102 +1,50 @@
|
|
|
1
|
-
|
|
2
|
-
export default function expressXClient(socket, options={}) {
|
|
3
|
-
if (options.debug === undefined) options.debug = false
|
|
4
|
-
if (options.timeout === undefined) options.timeout = 5000
|
|
5
|
-
|
|
6
|
-
const waitingPromisesByUid = {}
|
|
7
|
-
const action2service2handlers = {}
|
|
8
|
-
const type2appHandlers = {}
|
|
9
|
-
let onConnectionCallback = null
|
|
10
|
-
let onReconnectionCallback = null
|
|
11
|
-
let onDisconnectionCallback = null
|
|
12
|
-
let nodeCnxId
|
|
13
|
-
|
|
14
|
-
const setConnectionCallback = (callback) => {
|
|
15
|
-
onConnectionCallback = callback
|
|
16
|
-
}
|
|
17
1
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
2
|
+
function generateUID(length) {
|
|
3
|
+
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
|
4
|
+
let uid = '';
|
|
21
5
|
|
|
22
|
-
|
|
23
|
-
|
|
6
|
+
for (let i = 0; i < length; i++) {
|
|
7
|
+
const randomIndex = Math.floor(Math.random() * characters.length)
|
|
8
|
+
uid += characters.charAt(randomIndex)
|
|
24
9
|
}
|
|
10
|
+
return uid
|
|
11
|
+
}
|
|
25
12
|
|
|
26
|
-
function _getCnxId() {
|
|
27
|
-
if (typeof sessionStorage !== 'undefined') return sessionStorage.getItem('expressx-cnx-id')
|
|
28
|
-
return nodeCnxId
|
|
29
|
-
}
|
|
30
13
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
sessionStorage.setItem('expressx-cnx-id', id)
|
|
34
|
-
} else {
|
|
35
|
-
nodeCnxId = id
|
|
36
|
-
}
|
|
37
|
-
}
|
|
14
|
+
export default function expressXClient(socket, options={}) {
|
|
15
|
+
if (options.debug === undefined) options.debug = false
|
|
38
16
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
// look for a previously stored connection id
|
|
43
|
-
const prevConnectionId = _getCnxId()
|
|
44
|
-
if (prevConnectionId) {
|
|
45
|
-
// it's a reconnection
|
|
46
|
-
if (prevConnectionId < 0) {
|
|
47
|
-
// ask server to transfer all data from connection `prevConnectionId` to connection `connectionId`
|
|
48
|
-
if (options.debug) console.log('cnx-transfer', -prevConnectionId, 'to', connectionId)
|
|
49
|
-
socket.emit('cnx-transfer', {
|
|
50
|
-
from: -prevConnectionId,
|
|
51
|
-
to: connectionId,
|
|
52
|
-
})
|
|
53
|
-
// set/update connection id
|
|
54
|
-
_setCnxId(connectionId)
|
|
55
|
-
} else {
|
|
56
|
-
if (options.debug) console.log('Error, previous connection id should be negative', prevConnectionId)
|
|
57
|
-
}
|
|
17
|
+
const waitingPromisesByUid = {}
|
|
18
|
+
const action2service2handlers = {}
|
|
19
|
+
const socketConnectionState = {}
|
|
58
20
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
21
|
+
socket.on("connect", async () => {
|
|
22
|
+
console.log("socket connected", socket.id)
|
|
23
|
+
if (socketConnectionState.resolve) {
|
|
24
|
+
socketConnectionState.resolve('ok')
|
|
25
|
+
socketConnectionState.status = 'connected'
|
|
62
26
|
}
|
|
63
|
-
// call user-defined connection callback
|
|
64
|
-
if (onConnectionCallback) onConnectionCallback(connectionId)
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
socket.on("cnx-transfer-ack", async (connection) => {
|
|
68
|
-
if (options.debug) console.log('cnx-transfer-ack', connection)
|
|
69
|
-
_setCnxId(connection.id)
|
|
70
|
-
// call user-defined reconnection callback
|
|
71
|
-
if (onReconnectionCallback) onReconnectionCallback(connection)
|
|
72
27
|
})
|
|
73
28
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
socket.on("disconnect", async (cause) => {
|
|
80
|
-
const id = _getCnxId()
|
|
81
|
-
if (id > 0) {
|
|
82
|
-
_setCnxId(-id)
|
|
83
|
-
} else {
|
|
84
|
-
if (options.debug) console.log('Error (disconnect), connection id should be negative', id)
|
|
29
|
+
socket.on("error", async () => {
|
|
30
|
+
console.log("socket connection error", socket.id)
|
|
31
|
+
if (socketConnectionState.reject) {
|
|
32
|
+
socketConnectionState.reject(err)
|
|
33
|
+
socketConnectionState.status = 'error'
|
|
85
34
|
}
|
|
86
35
|
})
|
|
87
36
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (id > 0) {
|
|
93
|
-
_setCnxId(-id)
|
|
94
|
-
} else {
|
|
95
|
-
if (options.debug) console.log('Error (unload), connection id should be negative', id)
|
|
96
|
-
}
|
|
37
|
+
async function socketConnection() {
|
|
38
|
+
const promise = new Promise((resolve, reject) => {
|
|
39
|
+
socketConnectionState.resolve = resolve
|
|
40
|
+
socketConnectionState.reject = reject
|
|
97
41
|
})
|
|
42
|
+
return promise
|
|
98
43
|
}
|
|
99
44
|
|
|
45
|
+
function socketStatus() {
|
|
46
|
+
return socketConnectionState.status
|
|
47
|
+
}
|
|
100
48
|
|
|
101
49
|
// on receiving response from service request
|
|
102
50
|
socket.on('client-response', ({ uid, error, result }) => {
|
|
@@ -127,21 +75,8 @@ export default function expressXClient(socket, options={}) {
|
|
|
127
75
|
const handler = type2appHandlers[type]
|
|
128
76
|
if (handler) handler(value)
|
|
129
77
|
})
|
|
130
|
-
|
|
131
|
-
function
|
|
132
|
-
return new Promise(resolve => setTimeout(resolve, ms));
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
async function serviceMethodRequest(name, action, ...args) {
|
|
136
|
-
// wait while stored connexion id is negative (= connection is lost with server)
|
|
137
|
-
let retries = 10
|
|
138
|
-
while (retries-- > 0) {
|
|
139
|
-
const id = _getCnxId()
|
|
140
|
-
if (id > 0) break
|
|
141
|
-
await wait(200)
|
|
142
|
-
}
|
|
143
|
-
if (retries === 0) throw new Error(`Timeout waiting for reconnection`)
|
|
144
|
-
|
|
78
|
+
|
|
79
|
+
async function serviceMethodRequest(name, action, options, ...args) {
|
|
145
80
|
// create a promise which will resolve or reject by an event 'client-response'
|
|
146
81
|
const uid = generateUID(20)
|
|
147
82
|
const promise = new Promise((resolve, reject) => {
|
|
@@ -163,13 +98,7 @@ export default function expressXClient(socket, options={}) {
|
|
|
163
98
|
return promise
|
|
164
99
|
}
|
|
165
100
|
|
|
166
|
-
|
|
167
|
-
function on(type, handler) {
|
|
168
|
-
if (!type2appHandlers[type]) type2appHandlers[type] = {}
|
|
169
|
-
type2appHandlers[type] = handler
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
function service(name) {
|
|
101
|
+
function service(name, options={ timeout: 5000 }) {
|
|
173
102
|
const service = {
|
|
174
103
|
// associate a handler to a pub/sub event for this service
|
|
175
104
|
on: (action, handler) => {
|
|
@@ -183,7 +112,7 @@ export default function expressXClient(socket, options={}) {
|
|
|
183
112
|
get(service, action) {
|
|
184
113
|
if (!(action in service)) {
|
|
185
114
|
// newly used property `action`: define it as a service method request function
|
|
186
|
-
service[action] = (...args) => serviceMethodRequest(name, action, ...args)
|
|
115
|
+
service[action] = (...args) => serviceMethodRequest(name, action, options, ...args)
|
|
187
116
|
}
|
|
188
117
|
return service[action]
|
|
189
118
|
}
|
|
@@ -192,22 +121,9 @@ export default function expressXClient(socket, options={}) {
|
|
|
192
121
|
}
|
|
193
122
|
|
|
194
123
|
return {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
setDisconnectionCallback,
|
|
124
|
+
socketConnection,
|
|
125
|
+
socketStatus,
|
|
198
126
|
service,
|
|
199
|
-
on,
|
|
200
127
|
}
|
|
201
128
|
}
|
|
202
129
|
|
|
203
|
-
|
|
204
|
-
function generateUID(length) {
|
|
205
|
-
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
|
206
|
-
let uid = '';
|
|
207
|
-
|
|
208
|
-
for (let i = 0; i < length; i++) {
|
|
209
|
-
const randomIndex = Math.floor(Math.random() * characters.length)
|
|
210
|
-
uid += characters.charAt(randomIndex)
|
|
211
|
-
}
|
|
212
|
-
return uid
|
|
213
|
-
}
|