@jcbuisson/express-x-client 1.5.3 → 1.5.5
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 -19
- 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 window !== 'undefined') return sessionStorage.getItem('expressx-cnx-id')
|
|
23
|
+
return nodeCnxId
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function _setCnxId(id) {
|
|
27
|
+
if (typeof window !== '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,13 +45,15 @@ export default function expressXClient(socket, options={}) {
|
|
|
31
45
|
from: -prevConnectionId,
|
|
32
46
|
to: connectionId,
|
|
33
47
|
})
|
|
48
|
+
// set/update connection id
|
|
49
|
+
_setCnxId(connectionId)
|
|
34
50
|
} else {
|
|
35
51
|
if (options.debug) console.log('Error, previous connection id should be negative', prevConnectionId)
|
|
36
52
|
}
|
|
37
53
|
|
|
38
54
|
} else {
|
|
39
|
-
// set/update connection id
|
|
40
|
-
|
|
55
|
+
// set/update connection id
|
|
56
|
+
_setCnxId(connectionId)
|
|
41
57
|
}
|
|
42
58
|
// call user-defined connection callback
|
|
43
59
|
if (onConnectionCallback) onConnectionCallback(connectionId)
|
|
@@ -45,33 +61,34 @@ export default function expressXClient(socket, options={}) {
|
|
|
45
61
|
|
|
46
62
|
socket.on("cnx-transfer-ack", async (connectionId) => {
|
|
47
63
|
if (options.debug) console.log('cnx-transfer-ack', connectionId)
|
|
48
|
-
|
|
64
|
+
_setCnxId(connectionId)
|
|
49
65
|
})
|
|
50
66
|
|
|
51
67
|
|
|
52
|
-
// A negative value for
|
|
68
|
+
// A negative value for the connexion id means that the connection with the server has been lost
|
|
53
69
|
// Requests must wait until it goes positive again
|
|
54
70
|
|
|
55
71
|
// disconnection due to network issues
|
|
56
72
|
socket.on("disconnect", async (cause) => {
|
|
57
|
-
|
|
58
|
-
const id = sessionStorage.getItem('expressx-cnx-id')
|
|
73
|
+
const id = _getCnxId()
|
|
59
74
|
if (id > 0) {
|
|
60
|
-
|
|
75
|
+
_setCnxId(-id)
|
|
61
76
|
} else {
|
|
62
77
|
if (options.debug) console.log('Error (disconnect), connection id should be negative', id)
|
|
63
78
|
}
|
|
64
79
|
})
|
|
65
80
|
|
|
66
81
|
// disconnection due to a page reload
|
|
67
|
-
window
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
82
|
+
if (typeof window !== 'undefined') {
|
|
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
|
+
}
|
|
75
92
|
|
|
76
93
|
|
|
77
94
|
// on receiving response from service request
|
|
@@ -100,10 +117,10 @@ export default function expressXClient(socket, options={}) {
|
|
|
100
117
|
}
|
|
101
118
|
|
|
102
119
|
async function serviceMethodRequest(name, action, ...args) {
|
|
103
|
-
// wait while
|
|
120
|
+
// wait while stored connexion id is negative (= connection is lost with server)
|
|
104
121
|
let retries = 10
|
|
105
122
|
while (retries-- > 0) {
|
|
106
|
-
const id =
|
|
123
|
+
const id = _getCnxId()
|
|
107
124
|
if (id > 0) break
|
|
108
125
|
await wait(200)
|
|
109
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)
|