lenix 1.7.24 → 1.7.25
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/nui.ts +3 -3
- package/nui/fetch.ts +2 -3
- package/nui/on.ts +11 -11
- package/package.json +1 -1
- package/shared/types.ts +2 -0
package/client/nui.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { NuiFetchGeneric } from '../shared/types.ts'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Sends an event from the game client to the NUI browser.
|
|
5
5
|
*/
|
|
6
|
-
export const
|
|
6
|
+
export const emitNui = <T extends [string, unknown[]]>(id: T[0], ...params: T[1]): void => {
|
|
7
7
|
if (
|
|
8
8
|
!SendNuiMessage(
|
|
9
9
|
JSON.stringify({
|
|
@@ -19,7 +19,7 @@ export const emitEvent = <T extends [string, unknown[]]>(id: T[0], ...params: T[
|
|
|
19
19
|
/**
|
|
20
20
|
* Registers a typed NUI callback on the game client.
|
|
21
21
|
*/
|
|
22
|
-
export const onNui = <T extends
|
|
22
|
+
export const onNui = <T extends NuiFetchGeneric>(
|
|
23
23
|
id: T[1],
|
|
24
24
|
cb: (data: T[2]) => T[0] | Promise<T[0]>
|
|
25
25
|
): void => {
|
package/nui/fetch.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { Request } from '../shared/types.ts'
|
|
1
|
+
import type { NuiFetchGeneric, Request } from '../shared/types.ts'
|
|
3
2
|
|
|
4
3
|
declare function GetParentResourceName(): string
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* Fetchs a typed request from the NUI browser to the game client.
|
|
8
7
|
*/
|
|
9
|
-
export const fetchNui = async <T extends
|
|
8
|
+
export const fetchNui = async <T extends NuiFetchGeneric>(
|
|
10
9
|
id: T[1],
|
|
11
10
|
data: T[2]
|
|
12
11
|
): Promise<T[0]> => {
|
package/nui/on.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { Event } from '../shared/types.ts'
|
|
2
2
|
|
|
3
|
-
interface
|
|
3
|
+
interface Logger<T extends unknown[]> {
|
|
4
4
|
callback: (...params: T) => void
|
|
5
5
|
log?: true
|
|
6
6
|
}
|
|
7
|
-
const events = new Map<string, Set<
|
|
7
|
+
const events = new Map<string, Set<Logger<unknown[]>>>()
|
|
8
8
|
|
|
9
9
|
const handler = (event: MessageEvent) => {
|
|
10
10
|
const { id, params } = event.data
|
|
@@ -24,34 +24,34 @@ const handler = (event: MessageEvent) => {
|
|
|
24
24
|
/**
|
|
25
25
|
* Registers a typed browser-side handler for events sent from the game client.
|
|
26
26
|
*/
|
|
27
|
-
export const
|
|
27
|
+
export const onNuiEmit = <
|
|
28
28
|
T extends Event<string, unknown[]>
|
|
29
29
|
>(
|
|
30
30
|
id: T[0],
|
|
31
31
|
cb: (...params: T[1]) => void,
|
|
32
|
-
|
|
32
|
+
log?: true
|
|
33
33
|
): () => void => {
|
|
34
34
|
if (events.size === 0) {
|
|
35
35
|
window.addEventListener('message', handler)
|
|
36
|
-
|
|
36
|
+
log && console.info(`Event<${id}> is the first on the window`)
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
const callbacks = events.get(id) ?? new Set<
|
|
40
|
-
const listenerCb = { callback: cb, log:
|
|
39
|
+
const callbacks = events.get(id) ?? new Set<Logger<unknown[]>>()
|
|
40
|
+
const listenerCb = { callback: cb, log: log }
|
|
41
41
|
callbacks.add(listenerCb)
|
|
42
42
|
events.set(id, callbacks)
|
|
43
|
-
|
|
43
|
+
log && console.info(`Event<${id}> was registered`)
|
|
44
44
|
|
|
45
45
|
return () => {
|
|
46
|
-
|
|
46
|
+
log && console.info(`Event<${id}> was unregistered`)
|
|
47
47
|
callbacks.delete(listenerCb)
|
|
48
48
|
if (callbacks.size === 0) {
|
|
49
49
|
events.delete(id)
|
|
50
|
-
|
|
50
|
+
log && console.info(`Event<${id}> got no callbacks left, memory cleared successfully`)
|
|
51
51
|
}
|
|
52
52
|
if (events.size === 0) {
|
|
53
53
|
window.removeEventListener('message', handler)
|
|
54
|
-
|
|
54
|
+
log && console.info(`No events left, this event<${id}> was the last, opting out the listen...`)
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
}
|
package/package.json
CHANGED