@jcbuisson/express-x-client 1.5.5 → 1.5.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.mjs +14 -5
package/package.json
CHANGED
package/src/index.mjs
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
|
|
2
|
-
|
|
2
|
+
function generateUID(length) {
|
|
3
|
+
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
|
4
|
+
let uid = '';
|
|
3
5
|
|
|
6
|
+
for (let i = 0; i < length; i++) {
|
|
7
|
+
const randomIndex = Math.floor(Math.random() * characters.length)
|
|
8
|
+
uid += characters.charAt(randomIndex)
|
|
9
|
+
}
|
|
10
|
+
return uid
|
|
11
|
+
}
|
|
12
|
+
|
|
4
13
|
export default function expressXClient(socket, options={}) {
|
|
5
14
|
if (options.debug === undefined) options.debug = false
|
|
6
15
|
|
|
@@ -19,12 +28,12 @@ export default function expressXClient(socket, options={}) {
|
|
|
19
28
|
}
|
|
20
29
|
|
|
21
30
|
function _getCnxId() {
|
|
22
|
-
if (typeof
|
|
31
|
+
if (typeof sessionStorage !== 'undefined') return sessionStorage.getItem('expressx-cnx-id')
|
|
23
32
|
return nodeCnxId
|
|
24
33
|
}
|
|
25
34
|
|
|
26
35
|
function _setCnxId(id) {
|
|
27
|
-
if (typeof
|
|
36
|
+
if (typeof sessionStorage !== 'undefined') {
|
|
28
37
|
sessionStorage.setItem('expressx-cnx-id', id)
|
|
29
38
|
} else {
|
|
30
39
|
nodeCnxId = id
|
|
@@ -79,7 +88,7 @@ export default function expressXClient(socket, options={}) {
|
|
|
79
88
|
})
|
|
80
89
|
|
|
81
90
|
// disconnection due to a page reload
|
|
82
|
-
if (typeof window !== 'undefined') {
|
|
91
|
+
if (typeof window !== 'undefined' && 'addEventListener' in window) {
|
|
83
92
|
window.addEventListener('unload', () => {
|
|
84
93
|
const id = _getCnxId()
|
|
85
94
|
if (id > 0) {
|
|
@@ -127,7 +136,7 @@ export default function expressXClient(socket, options={}) {
|
|
|
127
136
|
if (retries === 0) throw new Error(`Timeout waiting for reconnection`)
|
|
128
137
|
|
|
129
138
|
// create a promise which will resolve or reject by an event 'client-response'
|
|
130
|
-
const uid =
|
|
139
|
+
const uid = generateUID(20)
|
|
131
140
|
const promise = new Promise((resolve, reject) => {
|
|
132
141
|
waitingPromisesByUid[uid] = [resolve, reject]
|
|
133
142
|
// a 5s timeout may also reject the promise
|