@jcbuisson/express-x-client 1.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/.adminjs/.entry.js +1 -0
- package/.env +2 -0
- package/package.json +19 -0
- package/src/expressx-client.js +83 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
AdminJS.UserComponents = {}
|
package/.env
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jcbuisson/express-x-client",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "src/express-x-client.js",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git@gitlab.com:buisson31/express-x.git"
|
|
9
|
+
},
|
|
10
|
+
"author": "Jean-Christophe Buisson <buisson@enseeiht.fr> (jcbuisson.dev)",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"private": false,
|
|
13
|
+
"keywords": [],
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"socket.io-client": "^4.6.0"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
|
|
2
|
+
import { io } from 'socket.io-client'
|
|
3
|
+
import { v4 } from 'uuid'
|
|
4
|
+
|
|
5
|
+
function expressxClient() {
|
|
6
|
+
|
|
7
|
+
const socket = io()
|
|
8
|
+
|
|
9
|
+
const waitingPromises = {}
|
|
10
|
+
const action2service2handlers = {}
|
|
11
|
+
|
|
12
|
+
// on connection
|
|
13
|
+
socket.on("connected", async (connectionId) => {
|
|
14
|
+
console.log('connected', connectionId)
|
|
15
|
+
const token = window.sessionStorage.getItem('feathers-jwt')
|
|
16
|
+
if (token) {
|
|
17
|
+
// disconnect/reconnect: reauthenticate
|
|
18
|
+
console.log('reauthenticate with', token)
|
|
19
|
+
serviceMethodRequest('authenticate', 'patch', [token])
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
// on receiving response from service request
|
|
24
|
+
socket.on('client-response', ({ uid, error, result }) => {
|
|
25
|
+
console.log('client-response', uid, error, result)
|
|
26
|
+
if (!uid in waitingPromises) return // ???
|
|
27
|
+
const [resolve, reject] = waitingPromises[uid]
|
|
28
|
+
if (error) {
|
|
29
|
+
reject(error)
|
|
30
|
+
} else {
|
|
31
|
+
resolve(result)
|
|
32
|
+
}
|
|
33
|
+
delete waitingPromises[uid]
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
// on receiving events from pub/sub
|
|
37
|
+
socket.on('service-event', ({ name, action, result }) => {
|
|
38
|
+
if (!action2service2handlers[action]) action2service2handlers[action] = {}
|
|
39
|
+
const serviceHandlers = action2service2handlers[action]
|
|
40
|
+
const handler = serviceHandlers[name]
|
|
41
|
+
if (handler) handler(result)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
async function serviceMethodRequest(name, action, argList) {
|
|
45
|
+
const uid = v4()
|
|
46
|
+
const promise = new Promise((resolve, reject) => {
|
|
47
|
+
waitingPromises[uid] = [resolve, reject]
|
|
48
|
+
})
|
|
49
|
+
socket.emit('client-request', {
|
|
50
|
+
uid,
|
|
51
|
+
name,
|
|
52
|
+
action,
|
|
53
|
+
argList,
|
|
54
|
+
})
|
|
55
|
+
return promise
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function service(name) {
|
|
59
|
+
return {
|
|
60
|
+
create: (data) => serviceMethodRequest(name, 'create', [data]),
|
|
61
|
+
get: (id) => serviceMethodRequest(name, 'create', [id]),
|
|
62
|
+
patch: (id, data) => serviceMethodRequest(name, 'create', [id, data]),
|
|
63
|
+
update: (id, data) => serviceMethodRequest(name, 'update', [id, data]),
|
|
64
|
+
remove: (id) => serviceMethodRequest(name, 'remove', [id]),
|
|
65
|
+
find: (data) => serviceMethodRequest(name, 'find', [data]),
|
|
66
|
+
|
|
67
|
+
// associate a handler to a pub/sub event for this service
|
|
68
|
+
on: (action, handler) => {
|
|
69
|
+
if (!action2service2handlers[action]) action2service2handlers[action] = {}
|
|
70
|
+
const serviceHandlers = action2service2handlers[action]
|
|
71
|
+
serviceHandlers[name] = handler
|
|
72
|
+
console.log('action2service2handlers', action2service2handlers)
|
|
73
|
+
},
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
service,
|
|
79
|
+
socket,
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export default expressxClient
|