@jcbuisson/express-x-client 2.0.0 → 2.0.2
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 +20 -12
package/package.json
CHANGED
package/src/index.mjs
CHANGED
|
@@ -13,10 +13,10 @@ function generateUID(length) {
|
|
|
13
13
|
|
|
14
14
|
export default function expressXClient(socket, options={}) {
|
|
15
15
|
if (options.debug === undefined) options.debug = false
|
|
16
|
-
if (options.timeout === undefined) options.timeout = 5000
|
|
17
16
|
|
|
18
17
|
const waitingPromisesByUid = {}
|
|
19
18
|
const action2service2handlers = {}
|
|
19
|
+
const type2appHandler = {}
|
|
20
20
|
const socketConnectionState = {}
|
|
21
21
|
|
|
22
22
|
socket.on("connect", async () => {
|
|
@@ -68,16 +68,8 @@ export default function expressXClient(socket, options={}) {
|
|
|
68
68
|
const handler = serviceHandlers[name]
|
|
69
69
|
if (handler) handler(result)
|
|
70
70
|
})
|
|
71
|
-
|
|
72
|
-
// on receiving application events from pub/sub
|
|
73
|
-
socket.on('app-event', ({ type, value }) => {
|
|
74
|
-
if (options.debug) console.log('app-event', type, value)
|
|
75
|
-
if (!type2appHandlers[type]) type2appHandlers[type] = {}
|
|
76
|
-
const handler = type2appHandlers[type]
|
|
77
|
-
if (handler) handler(value)
|
|
78
|
-
})
|
|
79
71
|
|
|
80
|
-
async function serviceMethodRequest(name, action, ...args) {
|
|
72
|
+
async function serviceMethodRequest(name, action, options, ...args) {
|
|
81
73
|
// create a promise which will resolve or reject by an event 'client-response'
|
|
82
74
|
const uid = generateUID(20)
|
|
83
75
|
const promise = new Promise((resolve, reject) => {
|
|
@@ -99,7 +91,7 @@ export default function expressXClient(socket, options={}) {
|
|
|
99
91
|
return promise
|
|
100
92
|
}
|
|
101
93
|
|
|
102
|
-
function service(name) {
|
|
94
|
+
function service(name, options={ timeout: 5000 }) {
|
|
103
95
|
const service = {
|
|
104
96
|
// associate a handler to a pub/sub event for this service
|
|
105
97
|
on: (action, handler) => {
|
|
@@ -113,7 +105,7 @@ export default function expressXClient(socket, options={}) {
|
|
|
113
105
|
get(service, action) {
|
|
114
106
|
if (!(action in service)) {
|
|
115
107
|
// newly used property `action`: define it as a service method request function
|
|
116
|
-
service[action] = (...args) => serviceMethodRequest(name, action, ...args)
|
|
108
|
+
service[action] = (...args) => serviceMethodRequest(name, action, options, ...args)
|
|
117
109
|
}
|
|
118
110
|
return service[action]
|
|
119
111
|
}
|
|
@@ -121,10 +113,26 @@ export default function expressXClient(socket, options={}) {
|
|
|
121
113
|
return new Proxy(service, handler)
|
|
122
114
|
}
|
|
123
115
|
|
|
116
|
+
/////////////// APPLICATION-LEVEL EVENTS /////////////////
|
|
117
|
+
|
|
118
|
+
// There is a need for events sent outside any service method call, for example on unsolicited-by-frontend backend state change
|
|
119
|
+
socket.on('app-event', ({ type, value }) => {
|
|
120
|
+
if (options.debug) console.log('app-event', type, value)
|
|
121
|
+
if (!type2appHandler[type]) type2appHandler[type] = {}
|
|
122
|
+
const handler = type2appHandler[type]
|
|
123
|
+
if (handler) handler(value)
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
// add application event handler
|
|
127
|
+
function on(type, handler) {
|
|
128
|
+
type2appHandler[type] = handler
|
|
129
|
+
}
|
|
130
|
+
|
|
124
131
|
return {
|
|
125
132
|
socketConnection,
|
|
126
133
|
socketStatus,
|
|
127
134
|
service,
|
|
135
|
+
on,
|
|
128
136
|
}
|
|
129
137
|
}
|
|
130
138
|
|