@jcbuisson/express-x-client 1.5.4 → 1.5.6
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/.vscode/launch.json +18 -0
- package/package.json +1 -1
- package/prisma/dev.db +0 -0
- package/src/index.mjs +36 -21
- package/test/index.test.js +1 -7
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
// Utilisez IntelliSense pour en savoir plus sur les attributs possibles.
|
|
3
|
+
// Pointez pour afficher la description des attributs existants.
|
|
4
|
+
// Pour plus d'informations, visitez : https://go.microsoft.com/fwlink/?linkid=830387
|
|
5
|
+
"version": "0.2.0",
|
|
6
|
+
"configurations": [
|
|
7
|
+
{
|
|
8
|
+
"type": "node",
|
|
9
|
+
"request": "launch",
|
|
10
|
+
"name": "test",
|
|
11
|
+
"skipFiles": [
|
|
12
|
+
"<node_internals>/**"
|
|
13
|
+
],
|
|
14
|
+
"cwd": "${workspaceFolder}",
|
|
15
|
+
"program": "${workspaceFolder}/test/index.test.js"
|
|
16
|
+
}
|
|
17
|
+
]
|
|
18
|
+
}
|
package/package.json
CHANGED
package/prisma/dev.db
CHANGED
|
Binary file
|
package/src/index.mjs
CHANGED
|
@@ -8,6 +8,7 @@ export default function expressXClient(socket, options={}) {
|
|
|
8
8
|
const action2service2handlers = {}
|
|
9
9
|
let onConnectionCallback = null
|
|
10
10
|
let onDisconnectionCallback = null
|
|
11
|
+
let nodeCnxId
|
|
11
12
|
|
|
12
13
|
const setConnectionCallback = (callback) => {
|
|
13
14
|
onConnectionCallback = callback
|
|
@@ -17,11 +18,24 @@ export default function expressXClient(socket, options={}) {
|
|
|
17
18
|
onDisconnectionCallback = callback
|
|
18
19
|
}
|
|
19
20
|
|
|
21
|
+
function _getCnxId() {
|
|
22
|
+
if (typeof sessionStorage !== 'undefined') return sessionStorage.getItem('expressx-cnx-id')
|
|
23
|
+
return nodeCnxId
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function _setCnxId(id) {
|
|
27
|
+
if (typeof sessionStorage !== 'undefined') {
|
|
28
|
+
sessionStorage.setItem('expressx-cnx-id', id)
|
|
29
|
+
} else {
|
|
30
|
+
nodeCnxId = id
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
20
34
|
// on connection
|
|
21
35
|
socket.on("connected", async (connectionId) => {
|
|
22
36
|
if (options.debug) console.log('connected', connectionId)
|
|
23
|
-
// look
|
|
24
|
-
const prevConnectionId =
|
|
37
|
+
// look for a previously stored connection id
|
|
38
|
+
const prevConnectionId = _getCnxId()
|
|
25
39
|
if (prevConnectionId) {
|
|
26
40
|
// it's a reconnection
|
|
27
41
|
if (prevConnectionId < 0) {
|
|
@@ -31,15 +45,15 @@ export default function expressXClient(socket, options={}) {
|
|
|
31
45
|
from: -prevConnectionId,
|
|
32
46
|
to: connectionId,
|
|
33
47
|
})
|
|
34
|
-
// set/update connection id
|
|
35
|
-
|
|
48
|
+
// set/update connection id
|
|
49
|
+
_setCnxId(connectionId)
|
|
36
50
|
} else {
|
|
37
51
|
if (options.debug) console.log('Error, previous connection id should be negative', prevConnectionId)
|
|
38
52
|
}
|
|
39
53
|
|
|
40
54
|
} else {
|
|
41
|
-
// set/update connection id
|
|
42
|
-
|
|
55
|
+
// set/update connection id
|
|
56
|
+
_setCnxId(connectionId)
|
|
43
57
|
}
|
|
44
58
|
// call user-defined connection callback
|
|
45
59
|
if (onConnectionCallback) onConnectionCallback(connectionId)
|
|
@@ -47,33 +61,34 @@ export default function expressXClient(socket, options={}) {
|
|
|
47
61
|
|
|
48
62
|
socket.on("cnx-transfer-ack", async (connectionId) => {
|
|
49
63
|
if (options.debug) console.log('cnx-transfer-ack', connectionId)
|
|
50
|
-
|
|
64
|
+
_setCnxId(connectionId)
|
|
51
65
|
})
|
|
52
66
|
|
|
53
67
|
|
|
54
|
-
// A negative value for
|
|
68
|
+
// A negative value for the connexion id means that the connection with the server has been lost
|
|
55
69
|
// Requests must wait until it goes positive again
|
|
56
70
|
|
|
57
71
|
// disconnection due to network issues
|
|
58
72
|
socket.on("disconnect", async (cause) => {
|
|
59
|
-
|
|
60
|
-
const id = sessionStorage.getItem('expressx-cnx-id')
|
|
73
|
+
const id = _getCnxId()
|
|
61
74
|
if (id > 0) {
|
|
62
|
-
|
|
75
|
+
_setCnxId(-id)
|
|
63
76
|
} else {
|
|
64
77
|
if (options.debug) console.log('Error (disconnect), connection id should be negative', id)
|
|
65
78
|
}
|
|
66
79
|
})
|
|
67
80
|
|
|
68
81
|
// disconnection due to a page reload
|
|
69
|
-
window
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
82
|
+
if (typeof window !== 'undefined' && 'addEventListener' in window) {
|
|
83
|
+
window.addEventListener('unload', () => {
|
|
84
|
+
const id = _getCnxId()
|
|
85
|
+
if (id > 0) {
|
|
86
|
+
_setCnxId(-id)
|
|
87
|
+
} else {
|
|
88
|
+
if (options.debug) console.log('Error (unload), connection id should be negative', id)
|
|
89
|
+
}
|
|
90
|
+
})
|
|
91
|
+
}
|
|
77
92
|
|
|
78
93
|
|
|
79
94
|
// on receiving response from service request
|
|
@@ -102,10 +117,10 @@ export default function expressXClient(socket, options={}) {
|
|
|
102
117
|
}
|
|
103
118
|
|
|
104
119
|
async function serviceMethodRequest(name, action, ...args) {
|
|
105
|
-
// wait while
|
|
120
|
+
// wait while stored connexion id is negative (= connection is lost with server)
|
|
106
121
|
let retries = 10
|
|
107
122
|
while (retries-- > 0) {
|
|
108
|
-
const id =
|
|
123
|
+
const id = _getCnxId()
|
|
109
124
|
if (id > 0) break
|
|
110
125
|
await wait(200)
|
|
111
126
|
}
|
package/test/index.test.js
CHANGED
|
@@ -11,13 +11,7 @@ import { strict as assert } from 'node:assert'
|
|
|
11
11
|
import { expressX } from '@jcbuisson/express-x'
|
|
12
12
|
import expressXClient from '../src/index.mjs'
|
|
13
13
|
|
|
14
|
-
const prisma = new PrismaClient(
|
|
15
|
-
datasources: {
|
|
16
|
-
db: {
|
|
17
|
-
url: "file:./dev.db",
|
|
18
|
-
},
|
|
19
|
-
},
|
|
20
|
-
})
|
|
14
|
+
const prisma = new PrismaClient()
|
|
21
15
|
|
|
22
16
|
// `app` is a regular express application, enhanced with services and real-time features
|
|
23
17
|
const app = expressX(prisma)
|