@trippler/tr_lib 2.0.10 → 2.0.11

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.
@@ -1 +1 @@
1
- export declare const triggerPromise: (options: [number, boolean] | number | boolean, endpoint: string, ...parameters: any[]) => Promise<any>;
1
+ export declare const triggerPromise: <T = unknown>(options: [number, boolean] | number | boolean | string, endpoint: string | any, ...parameters: any[]) => Promise<T | null>;
@@ -1,10 +1,9 @@
1
1
  import { trace, fatal } from '../../../shared/console';
2
2
  const pendingPromises = {};
3
- const definedAddress = [];
4
3
  const patienceLimit = 5000;
5
4
  const averageServerResponseTime = 1000;
6
- let promiseId = 0;
7
5
  const promises = [];
6
+ let promiseId = 0;
8
7
  export const triggerPromise = async (options, endpoint, ...parameters) => {
9
8
  if (typeof options === 'string') {
10
9
  return triggerPromise([patienceLimit, false], options, endpoint, ...parameters);
@@ -22,10 +21,10 @@ export const triggerPromise = async (options, endpoint, ...parameters) => {
22
21
  if (!defined) {
23
22
  fatal(`promise ${endpoint} is not defined`);
24
23
  resolve(false);
24
+ return;
25
25
  }
26
26
  promises.push(endpoint);
27
27
  resolve(true);
28
- return;
29
28
  };
30
29
  onNet('__tr_promise_self_server_response', handler);
31
30
  emitNet('__tr_promise_self_request_server', endpoint);
@@ -44,9 +43,9 @@ export const triggerPromise = async (options, endpoint, ...parameters) => {
44
43
  promiseId = promiseId + 1;
45
44
  const currentPromiseId = promiseId;
46
45
  pendingPromises[currentPromiseId] = { resolve };
47
- const responseEvent = `__tr_promise_on:${endpoint}`;
48
- if (!definedAddress.includes(responseEvent)) {
49
- definedAddress.push(responseEvent);
46
+ const responseEvent = `__tr_promise_await:${endpoint}`;
47
+ if (!promises.includes(endpoint)) {
48
+ promises.push(endpoint);
50
49
  onNet(responseEvent, (selfpromiseId, response) => {
51
50
  if (pendingPromises[selfpromiseId]) {
52
51
  pendingPromises[selfpromiseId].resolve({ success: true, returned: response });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trippler/tr_lib",
3
- "version": "2.0.10",
3
+ "version": "2.0.11",
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",
@@ -1 +1 @@
1
- export declare const onPromise: (endpoint: string, FUNCTION: Function) => boolean;
1
+ export declare const onPromise: <T extends (...args: any) => ReturnType<T>>(endpoint: string, Function: T) => boolean;
@@ -6,27 +6,26 @@ onNet('__tr_promise_self_request_server', (endpoint) => {
6
6
  onNet('__tr_promise_on_self_server_lua_backward_compatibility', (endpoint) => {
7
7
  promises.push(endpoint);
8
8
  });
9
- export const onPromise = (endpoint, FUNCTION) => {
9
+ export const onPromise = (endpoint, Function) => {
10
10
  if (typeof endpoint !== 'string')
11
11
  return false;
12
12
  if (promises.includes(endpoint)) {
13
13
  fatal(`server promise '${endpoint}' is already defined`);
14
14
  return false;
15
15
  }
16
- if (typeof FUNCTION !== 'function')
16
+ if (typeof Function !== 'function')
17
17
  return false;
18
18
  promises.push(endpoint);
19
19
  emitNet('__tr_promise_on_self_server_ts_backward_compatibility', source, endpoint);
20
20
  onNet(`__tr_promise_on:${endpoint}`, (promiseId, ...parameters) => {
21
21
  const clientSource = source;
22
- const invocation = FUNCTION(clientSource, ...parameters);
23
- console.log(invocation);
24
- if (invocation) {
25
- emitNet(`__tr_promise_on:${endpoint}`, source, promiseId, invocation);
22
+ try {
23
+ const result = Function(clientSource, ...parameters);
24
+ emitNet(`__tr_promise_on:${endpoint}`, source, promiseId, result);
26
25
  }
27
- else {
26
+ catch (error) {
28
27
  emitNet(`__tr_promise_on:${endpoint}`, source, promiseId);
29
- console.trace(`server promise '${endpoint}' (client ${source}) threw error: ${invocation}`);
28
+ console.trace(`server promise '${endpoint}' (client ${source}) threw error: ${error}`);
30
29
  }
31
30
  });
32
31
  return true;