@trippler/tr_lib 2.0.12 → 2.0.13
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/client/index.d.ts +1 -1
- package/client/index.js +1 -1
- package/client/promise/onPromise/index.d.ts +1 -1
- package/client/promise/onPromise/index.js +31 -1
- package/package.json +1 -1
- package/server/index.d.ts +1 -1
- package/server/index.js +1 -1
- package/server/promise/onPromise/index.d.ts +1 -1
- package/server/promise/triggerPromise/index.d.ts +1 -2
- package/server/promise/triggerPromise/index.js +78 -1
package/client/index.d.ts
CHANGED
package/client/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default:
|
|
1
|
+
declare const _default: <T extends (...args: any) => ReturnType<T>>(endpoint: string, Function: T) => boolean;
|
|
2
2
|
export default _default;
|
|
@@ -1 +1,31 @@
|
|
|
1
|
-
|
|
1
|
+
import { fatal } from '../../../shared/console';
|
|
2
|
+
const promises = [];
|
|
3
|
+
onNet('__tr_promise_self_request_client', (endpoint) => {
|
|
4
|
+
emitNet('__tr_promise_self_client_response', source, promises.includes(endpoint));
|
|
5
|
+
});
|
|
6
|
+
onNet('__tr_promise_on_self_client_lua_backward_compatibility', (endpoint) => {
|
|
7
|
+
promises.push(endpoint);
|
|
8
|
+
});
|
|
9
|
+
export default (endpoint, Function) => {
|
|
10
|
+
if (typeof endpoint !== 'string')
|
|
11
|
+
return false;
|
|
12
|
+
if (promises.includes(endpoint)) {
|
|
13
|
+
fatal(`client promise '${endpoint}' is already defined`);
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
if (typeof Function !== 'function')
|
|
17
|
+
return false;
|
|
18
|
+
promises.push(endpoint);
|
|
19
|
+
emitNet('__tr_promise_on_self_client_ts_backward_compatibility', endpoint);
|
|
20
|
+
onNet(`__tr_promise_on:${endpoint}`, (promiseId, ...parameters) => {
|
|
21
|
+
try {
|
|
22
|
+
const result = Function(...parameters);
|
|
23
|
+
emitNet(`__tr_promise_on:${endpoint}`, promiseId, result);
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
emitNet(`__tr_promise_on:${endpoint}`, promiseId);
|
|
27
|
+
console.trace(`client promise '${endpoint}' (server (promise: ${promiseId}) threw error: ${error}`);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
return true;
|
|
31
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trippler/tr_lib",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.13",
|
|
4
4
|
"description": "A lightweight utility & helper library for FiveM developers. Designed to improve code reusability, consistency, and performance across and outcross Trippler resources.",
|
|
5
5
|
"author": "Trippler",
|
|
6
6
|
"license": "GPL-3.0",
|
package/server/index.d.ts
CHANGED
package/server/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: <T extends (...args: any) => ReturnType<T>>(endpoint: string, Function: T) => boolean;
|
|
1
|
+
declare const _default: <T extends (source: number, ...args: any) => ReturnType<T>>(endpoint: string, Function: T) => boolean;
|
|
2
2
|
export default _default;
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
declare const
|
|
2
|
-
export default _default;
|
|
1
|
+
export declare const triggerPromise: <T = unknown>(options: [number, boolean] | number | boolean | string, endpoint?: string | any, ...parameters: any[]) => Promise<T | null>;
|
|
@@ -1 +1,78 @@
|
|
|
1
|
-
|
|
1
|
+
import { trace, fatal } from '../../../shared/console';
|
|
2
|
+
const pendingPromises = {};
|
|
3
|
+
const patienceLimit = 5000;
|
|
4
|
+
const averageServerResponseTime = 1000;
|
|
5
|
+
const promises = [];
|
|
6
|
+
let promiseId = 0;
|
|
7
|
+
export const triggerPromise = async (options, endpoint, ...parameters) => {
|
|
8
|
+
if (typeof options === 'string') {
|
|
9
|
+
return triggerPromise([patienceLimit, false], options, endpoint, ...parameters);
|
|
10
|
+
}
|
|
11
|
+
if (typeof options === 'number') {
|
|
12
|
+
return triggerPromise([options, false], endpoint, ...parameters);
|
|
13
|
+
}
|
|
14
|
+
if (typeof options === 'boolean') {
|
|
15
|
+
return triggerPromise([patienceLimit, options], endpoint, ...parameters);
|
|
16
|
+
}
|
|
17
|
+
await new Promise(resolve => {
|
|
18
|
+
if (!promises.includes(endpoint)) {
|
|
19
|
+
const handler = (defined) => {
|
|
20
|
+
removeEventListener('__tr_promise_self_client_response', handler);
|
|
21
|
+
if (!defined) {
|
|
22
|
+
fatal(`promise ${endpoint} is not defined`);
|
|
23
|
+
resolve(false);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
promises.push(endpoint);
|
|
27
|
+
resolve(true);
|
|
28
|
+
};
|
|
29
|
+
onNet('__tr_promise_self_client_response', handler);
|
|
30
|
+
emitNet('__tr_promise_self_request_client', endpoint);
|
|
31
|
+
setTimeout(() => {
|
|
32
|
+
removeEventListener('__tr_promise_self_client_response', handler);
|
|
33
|
+
resolve(false);
|
|
34
|
+
}, averageServerResponseTime);
|
|
35
|
+
}
|
|
36
|
+
else
|
|
37
|
+
resolve(true);
|
|
38
|
+
});
|
|
39
|
+
const timeout = options?.[0] ?? patienceLimit;
|
|
40
|
+
const debug = options?.[1] ?? false;
|
|
41
|
+
const promise = () => {
|
|
42
|
+
return new Promise(resolve => {
|
|
43
|
+
promiseId = promiseId + 1;
|
|
44
|
+
const currentPromiseId = promiseId;
|
|
45
|
+
pendingPromises[currentPromiseId] = { resolve };
|
|
46
|
+
const responseEvent = `__tr_promise_await:${endpoint}`;
|
|
47
|
+
if (!promises.includes(endpoint)) {
|
|
48
|
+
promises.push(endpoint);
|
|
49
|
+
onNet(responseEvent, (selfpromiseId, response) => {
|
|
50
|
+
if (pendingPromises[selfpromiseId]) {
|
|
51
|
+
pendingPromises[selfpromiseId].resolve({ success: true, returned: response });
|
|
52
|
+
delete pendingPromises[selfpromiseId];
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
emitNet(`__tr_promise_on:${endpoint}`, currentPromiseId, ...parameters);
|
|
57
|
+
setTimeout(() => {
|
|
58
|
+
if (pendingPromises[currentPromiseId]) {
|
|
59
|
+
pendingPromises[currentPromiseId].resolve({ success: false });
|
|
60
|
+
delete pendingPromises[currentPromiseId];
|
|
61
|
+
}
|
|
62
|
+
}, timeout);
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
const response = await promise();
|
|
66
|
+
if (response.success) {
|
|
67
|
+
if (debug) {
|
|
68
|
+
trace(`client promise ${endpoint} returned ${Object.keys(response.returned).length} values`);
|
|
69
|
+
}
|
|
70
|
+
return response.returned;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
if (debug) {
|
|
74
|
+
trace(`client promise ${endpoint} timed out after ${timeout}ms`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return null;
|
|
78
|
+
};
|