@trippler/tr_lib 2.0.3 → 2.0.4

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.
@@ -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,9 @@
1
+ export const trace = (...parameters) => {
2
+ console.trace(...parameters);
3
+ };
4
+ export const info = (...parameters) => {
5
+ console.info(...parameters);
6
+ };
7
+ export const fatal = (...parameters) => {
8
+ console.error(...parameters);
9
+ };
@@ -0,0 +1,3 @@
1
+ export * from './async/promise';
2
+ export * from './async/listen';
3
+ export * from './console';
@@ -0,0 +1 @@
1
+ export default true;
@@ -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
+ };
@@ -0,0 +1,9 @@
1
+ export const trace = (...parameters) => {
2
+ console.trace(...parameters);
3
+ };
4
+ export const info = (...parameters) => {
5
+ console.info(...parameters);
6
+ };
7
+ export const fatal = (...parameters) => {
8
+ console.error(...parameters);
9
+ };
@@ -0,0 +1,3 @@
1
+ export * from './async/promise';
2
+ export * from './async/listen';
3
+ export * from './console';
@@ -0,0 +1,4 @@
1
+ export default {
2
+ client: {},
3
+ server: {}
4
+ };
package/package.json CHANGED
@@ -1,12 +1,17 @@
1
1
  {
2
2
  "name": "@trippler/tr_lib",
3
- "version": "2.0.3",
3
+ "version": "2.0.4",
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": ["fivem", "library", "trippler", "tr_lib"],
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
+ }