@trippler/tr_lib 2.0.3 → 2.0.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/dist/client/async/listen/index.d.ts +1 -0
- package/dist/client/async/listen/index.js +60 -0
- package/dist/client/async/promise/index.d.ts +2 -0
- package/dist/client/async/promise/index.js +1 -0
- package/dist/client/console/index.d.ts +3 -0
- package/dist/client/console/index.js +9 -0
- package/dist/client/index.d.ts +3 -0
- package/dist/client/index.js +3 -0
- package/dist/server/async/listen/index.d.ts +2 -0
- package/dist/server/async/listen/index.js +1 -0
- package/dist/server/async/promise/index.d.ts +1 -0
- package/dist/server/async/promise/index.js +26 -0
- package/dist/server/console/index.d.ts +3 -0
- package/dist/server/console/index.js +9 -0
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.js +3 -0
- package/dist/shared/async/index.d.ts +5 -0
- package/dist/shared/async/index.js +4 -0
- package/package.json +8 -3
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const listen: (options: [number, boolean] | number | boolean, endpoint: string, ...parameters: any[]) => Promise<any>;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import promises from '../../../shared/async';
|
|
2
|
+
import { trace, fatal } from '../../console';
|
|
3
|
+
const pendingPromises = {};
|
|
4
|
+
const definedAddress = [];
|
|
5
|
+
let promiseId = 0;
|
|
6
|
+
const awaitLimit = 5000;
|
|
7
|
+
export const listen = async (options, endpoint, ...parameters) => {
|
|
8
|
+
if (typeof options === 'string') {
|
|
9
|
+
return listen([awaitLimit, false], options, endpoint, ...parameters);
|
|
10
|
+
}
|
|
11
|
+
if (typeof options === 'number') {
|
|
12
|
+
return listen([options, false], endpoint, ...parameters);
|
|
13
|
+
}
|
|
14
|
+
if (typeof options === 'boolean') {
|
|
15
|
+
return listen([awaitLimit, options], endpoint, ...parameters);
|
|
16
|
+
}
|
|
17
|
+
const timeout = options[0] ?? awaitLimit;
|
|
18
|
+
const debug = options[1] ?? false;
|
|
19
|
+
const promise = () => {
|
|
20
|
+
return new Promise(resolve => {
|
|
21
|
+
promiseId = promiseId + 1;
|
|
22
|
+
const currentPromiseId = promiseId;
|
|
23
|
+
pendingPromises[currentPromiseId] = { resolve };
|
|
24
|
+
const responseEvent = `__tr_async_await:${endpoint}`;
|
|
25
|
+
if (!definedAddress.includes(responseEvent)) {
|
|
26
|
+
definedAddress.push(responseEvent);
|
|
27
|
+
onNet(responseEvent, (selfpromiseId, response) => {
|
|
28
|
+
if (pendingPromises[selfpromiseId]) {
|
|
29
|
+
pendingPromises[selfpromiseId].resolve({ success: true, returned: response });
|
|
30
|
+
delete pendingPromises[selfpromiseId];
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
if (!promises.server.includes(endpoint)) {
|
|
35
|
+
fatal(`async ${endpoint} is not defined`);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
emitNet(`__tr_async_define:${endpoint}`, currentPromiseId, ...parameters);
|
|
39
|
+
setTimeout(() => {
|
|
40
|
+
if (pendingPromises[currentPromiseId]) {
|
|
41
|
+
pendingPromises[currentPromiseId].resolve({ success: false });
|
|
42
|
+
delete pendingPromises[currentPromiseId];
|
|
43
|
+
}
|
|
44
|
+
}, timeout);
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
const response = await promise();
|
|
48
|
+
if (response.success) {
|
|
49
|
+
if (debug) {
|
|
50
|
+
trace(`server async ${endpoint} returned ${Object.keys(response.returned).length} values`);
|
|
51
|
+
}
|
|
52
|
+
return response.returned;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
if (debug) {
|
|
56
|
+
trace(`server async ${endpoint} timed out after ${timeout}ms`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default true;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default true;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const promise: (endpoint: string, FUNCTION: Function) => boolean;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import promises from '../../../shared/async';
|
|
2
|
+
import { fatal } from '../../console';
|
|
3
|
+
export const promise = (endpoint, FUNCTION) => {
|
|
4
|
+
if (typeof endpoint !== 'string')
|
|
5
|
+
return false;
|
|
6
|
+
if (typeof FUNCTION !== 'function')
|
|
7
|
+
return false;
|
|
8
|
+
if (promises.server.includes(endpoint)) {
|
|
9
|
+
fatal(`server async '${endpoint}' is already defined`);
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
promises.server.push(endpoint);
|
|
13
|
+
onNet(`__tr_async_define:${endpoint}`, (promiseId, ...parameters) => {
|
|
14
|
+
const clientSource = source;
|
|
15
|
+
const invocation = FUNCTION(clientSource, ...parameters);
|
|
16
|
+
console.log(invocation);
|
|
17
|
+
if (invocation) {
|
|
18
|
+
emitNet(`__tr_async_await:${endpoint}`, source, promiseId, invocation);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
emitNet(`__tr_async_await:${endpoint}`, source, promiseId);
|
|
22
|
+
console.trace(`server async '${endpoint}' (client ${source}) threw error: ${invocation}`);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
return true;
|
|
26
|
+
};
|
package/package.json
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trippler/tr_lib",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.5",
|
|
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
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"author": "Trippler",
|
|
8
8
|
"license": "GPL-3.0",
|
|
9
|
-
"keywords": [
|
|
9
|
+
"keywords": [
|
|
10
|
+
"fivem",
|
|
11
|
+
"library",
|
|
12
|
+
"trippler",
|
|
13
|
+
"tr_lib"
|
|
14
|
+
],
|
|
10
15
|
"repository": {
|
|
11
16
|
"type": "git",
|
|
12
17
|
"url": "https://github.com/tripplerscripts/tr_lib"
|
|
@@ -25,4 +30,4 @@
|
|
|
25
30
|
"@citizenfx/server": "^2.0.23683-1",
|
|
26
31
|
"typescript": "^5.0.0"
|
|
27
32
|
}
|
|
28
|
-
}
|
|
33
|
+
}
|