lenix 1.7.25 → 1.8.1

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/_init.ts CHANGED
@@ -1,6 +1,58 @@
1
+ import { asserts, type JsonValue } from '@lenix/lenix'
1
2
  import type { Request } from '../shared/types.ts'
2
3
  import { onNui } from './nui.ts'
3
4
 
5
+ type Resolve = {
6
+ success: true,
7
+ returned: JsonValue
8
+ } | {
9
+ success: false
10
+ }
11
+
12
+ const DEFAULT_TIMEOUT = 5000
13
+ const pendingResolves: Record<number, (resolve: Resolve) => void> = {}
14
+ const requests: string[] = []
15
+ let requestId = 0
16
+
17
+ onNui<Request<JsonValue, '__nuiServer', {
18
+ id: string
19
+ data: {
20
+ requestData: JsonValue
21
+ timeout?: number
22
+ }
23
+ }>>('__nuiServer', async ({ id, data }) => {
24
+ const { timeout = DEFAULT_TIMEOUT, requestData } = data
25
+
26
+ const response = await new Promise<Resolve>(resolve => {
27
+ requestId = requestId + 1
28
+ const currentRequestId = requestId
29
+ pendingResolves[currentRequestId] = resolve
30
+
31
+ if (!requests.includes(id)) {
32
+ requests.push(id)
33
+ onNet(`lenix/nui:${id}`, (selfrequestId: number, response: JsonValue) => {
34
+ if (!pendingResolves[selfrequestId]) return
35
+
36
+ pendingResolves[selfrequestId]({ success: true, returned: response })
37
+ delete pendingResolves[selfrequestId]
38
+ })
39
+ }
40
+
41
+ emitNet(`lenix/nui:${id}`, currentRequestId, requestData)
42
+
43
+ setTimeout(() => {
44
+ if (pendingResolves[currentRequestId]) {
45
+ pendingResolves[currentRequestId]({ success: false })
46
+ delete pendingResolves[currentRequestId]
47
+ }
48
+ }, timeout)
49
+ })
50
+
51
+ asserts(response.success, `server nui<${id}> timed out after ${timeout}ms, possible slow respose or request does not exist`)
52
+
53
+ return response.returned
54
+ })
55
+
4
56
  onNui<Request<true, '__nuiFocus', {
5
57
  keyboard: boolean,
6
58
  cursor: boolean
package/client/client.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { JsonValue } from '@lenix/lenix'
1
+ import type { JsonValue } from '@lenix/lenix'
2
2
  import { repeat } from '../shared/repeat.ts'
3
3
  import type { Vec3, Vec4 } from '../shared/types.ts'
4
4
 
package/nui/fetch.ts CHANGED
@@ -22,3 +22,22 @@ export const fetchNui = async <T extends NuiFetchGeneric>(
22
22
  throw `Error occured while emiting an nui<${id}>.\n${e}`
23
23
  }
24
24
  }
25
+
26
+ export const fetchNuiServer = <T extends NuiFetchGeneric>(
27
+ id: T[1],
28
+ requestData: T[2],
29
+ timeout?: number,
30
+ ): Promise<T[0]> => fetchNui<Request<T[0], '__nuiServer', {
31
+ id: T[1]
32
+ data: {
33
+ timeout?: number
34
+ requestData: T[2]
35
+ }
36
+ }>>(
37
+ '__nuiServer', {
38
+ id,
39
+ data: {
40
+ timeout,
41
+ requestData,
42
+ }
43
+ })
package/nui/focus.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Request } from '../shared/types.ts'
1
+ import type { Request } from '../shared/types.ts'
2
2
  import { fetchNui } from './fetch.ts'
3
3
 
4
4
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lenix",
3
- "version": "1.7.25",
3
+ "version": "1.8.1",
4
4
  "description": "Typed FiveM utilities for client, server, shared, and NUI scripts",
5
5
  "author": "Lenix",
6
6
  "license": "MIT",
package/server/index.ts CHANGED
@@ -5,4 +5,5 @@
5
5
  */
6
6
 
7
7
  export * from './server'
8
- export * from '../shared/api'
8
+ export * from '../shared/api'
9
+ export * from './nui'
package/server/nui.ts ADDED
@@ -0,0 +1,25 @@
1
+ import { asserts } from "@lenix/lenix";
2
+ import type { NuiFetchGeneric } from "../shared/types";
3
+
4
+ const clientRequests: string[] = []
5
+
6
+ export const onNuiServer = <T extends NuiFetchGeneric>(
7
+ id: T[1],
8
+ cb: (client: number, data: T[2]) => T[0] | Promise<T[0]>
9
+ ) => {
10
+ asserts(!clientRequests.includes(id), `server nui<${id}> is already defined`)
11
+
12
+ clientRequests.push(id)
13
+
14
+ onNet(`lenix/nui:${id}`, async(requestId: number, parameters: Parameters<typeof cb>[1]) => {
15
+ const clientSource = source
16
+
17
+ try {
18
+ const result = await cb(clientSource, parameters)
19
+ emitNet(`lenix/nui:${id}`, clientSource, requestId, result)
20
+ } catch (error) {
21
+ emitNet(`lenix/nui:${id}`, clientSource, requestId)
22
+ throw error
23
+ }
24
+ })
25
+ }
package/shared/types.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { JsonValue } from "@lenix/lenix"
1
+ import type { JsonValue } from "@lenix/lenix"
2
2
 
3
3
  /**
4
4
  * Three-dimensional coordinates plus heading.
@@ -18,11 +18,14 @@ export type Event<Id extends string, Params extends unknown[] = never> = [Id, Pa
18
18
  /**
19
19
  * Typed request tuple containing a response type, request id, and object payload.
20
20
  */
21
+ interface JsonObject {
22
+ [key: string]: JsonValue
23
+ }
21
24
  export type Request<
22
25
  Response extends JsonValue,
23
26
  Id extends string,
24
- Params extends object = { [key: string]: unknown }
27
+ Params extends JsonObject = JsonObject
25
28
  > = Params extends readonly unknown[] ? never
26
29
  : [Response, Id, Params]
27
30
 
28
- export type NuiFetchGeneric = Request<JsonValue, string, object>
31
+ export type NuiFetchGeneric = Request<JsonValue, string, JsonObject>