@jcbuisson/express-x-client 2.1.5 → 2.1.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 +17 -4
package/package.json
CHANGED
package/src/index.mjs
CHANGED
|
@@ -48,6 +48,19 @@ export default function expressXClient(socket, options={}) {
|
|
|
48
48
|
disconnectHandler = func
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
|
|
52
|
+
// async function socketConnection() {
|
|
53
|
+
// const promise = new Promise((resolve, reject) => {
|
|
54
|
+
// socketConnectionState.resolve = resolve
|
|
55
|
+
// socketConnectionState.reject = reject
|
|
56
|
+
// })
|
|
57
|
+
// return promise
|
|
58
|
+
// }
|
|
59
|
+
|
|
60
|
+
// function socketStatus() {
|
|
61
|
+
// return socketConnectionState.status
|
|
62
|
+
// }
|
|
63
|
+
|
|
51
64
|
// on receiving response from service request
|
|
52
65
|
socket.on('client-response', ({ uid, error, result }) => {
|
|
53
66
|
if (options.debug) console.log('client-response', uid, error, result)
|
|
@@ -70,7 +83,7 @@ export default function expressXClient(socket, options={}) {
|
|
|
70
83
|
if (handler) handler(result)
|
|
71
84
|
})
|
|
72
85
|
|
|
73
|
-
async function serviceMethodRequest(name, action,
|
|
86
|
+
async function serviceMethodRequest(name, action, serviceOptions, ...args) {
|
|
74
87
|
// create a promise which will resolve or reject by an event 'client-response'
|
|
75
88
|
const uid = generateUID(20)
|
|
76
89
|
const promise = new Promise((resolve, reject) => {
|
|
@@ -79,7 +92,7 @@ export default function expressXClient(socket, options={}) {
|
|
|
79
92
|
setTimeout(() => {
|
|
80
93
|
delete waitingPromisesByUid[uid]
|
|
81
94
|
reject(`Error: timeout on service '${name}', action '${action}', args: ${JSON.stringify(args)}`)
|
|
82
|
-
},
|
|
95
|
+
}, serviceOptions.timeout)
|
|
83
96
|
})
|
|
84
97
|
// send request to server through websocket
|
|
85
98
|
if (options.debug) console.log('client-request', uid, name, action, args)
|
|
@@ -92,7 +105,7 @@ export default function expressXClient(socket, options={}) {
|
|
|
92
105
|
return promise
|
|
93
106
|
}
|
|
94
107
|
|
|
95
|
-
function service(name,
|
|
108
|
+
function service(name, serviceOptions={ timeout: 20000 }) {
|
|
96
109
|
const service = {
|
|
97
110
|
// associate a handler to a pub/sub event for this service
|
|
98
111
|
on: (action, handler) => {
|
|
@@ -106,7 +119,7 @@ export default function expressXClient(socket, options={}) {
|
|
|
106
119
|
get(service, action) {
|
|
107
120
|
if (!(action in service)) {
|
|
108
121
|
// newly used property `action`: define it as a service method request function
|
|
109
|
-
service[action] = (...args) => serviceMethodRequest(name, action,
|
|
122
|
+
service[action] = (...args) => serviceMethodRequest(name, action, serviceOptions, ...args)
|
|
110
123
|
}
|
|
111
124
|
return service[action]
|
|
112
125
|
}
|