@fivem-nui/core 1.0.0 → 1.1.0
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/EventBus.js +2 -2
- package/dist/EventBus.js.map +1 -1
- package/dist/MessageTransport.d.ts.map +1 -1
- package/dist/MessageTransport.js +6 -1
- package/dist/MessageTransport.js.map +1 -1
- package/dist/NuiClient.d.ts +3 -3
- package/dist/NuiClient.d.ts.map +1 -1
- package/dist/NuiClient.js +14 -9
- package/dist/NuiClient.js.map +1 -1
- package/package.json +1 -1
- package/src/CallbackRegistry.ts +46 -46
- package/src/EventBus.ts +119 -119
- package/src/MessageTransport.ts +95 -88
- package/src/NuiClient.ts +24 -20
- package/src/NuiClientOptions.ts +57 -57
- package/src/RequestCoordinator.ts +105 -105
- package/src/diagnostics/NuiDiagnostics.ts +28 -28
- package/src/types.ts +151 -151
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Counter snapshot exposed by {@link NuiClient.getDiagnostics}.
|
|
3
|
-
*
|
|
4
|
-
* Mirrors the C# `NuiDiagnosticsSnapshot` so both sides of the bridge can be
|
|
5
|
-
* inspected with the same vocabulary.
|
|
6
|
-
*/
|
|
7
|
-
export interface NuiDiagnostics {
|
|
8
|
-
/** Envelopes POSTed to the main resource callback. */
|
|
9
|
-
sent: number;
|
|
10
|
-
/** Envelopes POSTed to a named callback (`notify`, `notifyAndWait`). */
|
|
11
|
-
notified: number;
|
|
12
|
-
/** POSTs that failed or timed out. */
|
|
13
|
-
failed: number;
|
|
14
|
-
/** Messages received from C#, any type. */
|
|
15
|
-
received: number;
|
|
16
|
-
/** Requests that resolved successfully. */
|
|
17
|
-
resolved: number;
|
|
18
|
-
/** Requests rejected by C# or by a timeout. */
|
|
19
|
-
rejected: number;
|
|
20
|
-
/** Requests still awaiting a response. */
|
|
21
|
-
pending: number;
|
|
22
|
-
/** Registered event listeners, across all events. */
|
|
23
|
-
listeners: number;
|
|
24
|
-
/** Registered C#-request handlers. */
|
|
25
|
-
callbacks: number;
|
|
26
|
-
/** Window lifecycle listeners. */
|
|
27
|
-
windowListeners: number;
|
|
28
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Counter snapshot exposed by {@link NuiClient.getDiagnostics}.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors the C# `NuiDiagnosticsSnapshot` so both sides of the bridge can be
|
|
5
|
+
* inspected with the same vocabulary.
|
|
6
|
+
*/
|
|
7
|
+
export interface NuiDiagnostics {
|
|
8
|
+
/** Envelopes POSTed to the main resource callback. */
|
|
9
|
+
sent: number;
|
|
10
|
+
/** Envelopes POSTed to a named callback (`notify`, `notifyAndWait`). */
|
|
11
|
+
notified: number;
|
|
12
|
+
/** POSTs that failed or timed out. */
|
|
13
|
+
failed: number;
|
|
14
|
+
/** Messages received from C#, any type. */
|
|
15
|
+
received: number;
|
|
16
|
+
/** Requests that resolved successfully. */
|
|
17
|
+
resolved: number;
|
|
18
|
+
/** Requests rejected by C# or by a timeout. */
|
|
19
|
+
rejected: number;
|
|
20
|
+
/** Requests still awaiting a response. */
|
|
21
|
+
pending: number;
|
|
22
|
+
/** Registered event listeners, across all events. */
|
|
23
|
+
listeners: number;
|
|
24
|
+
/** Registered C#-request handlers. */
|
|
25
|
+
callbacks: number;
|
|
26
|
+
/** Window lifecycle listeners. */
|
|
27
|
+
windowListeners: number;
|
|
28
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -1,151 +1,151 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Represents an error in NUI communication.
|
|
3
|
-
*/
|
|
4
|
-
export interface NuiError {
|
|
5
|
-
code: string;
|
|
6
|
-
message: string;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Window lifecycle envelope type sent by C# window management.
|
|
11
|
-
*/
|
|
12
|
-
export type NuiWindowAction = 'open' | 'close' | 'focus' | 'unfocus';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Represents a message sent between C# and NUI.
|
|
16
|
-
*/
|
|
17
|
-
export interface NuiMessage {
|
|
18
|
-
id?: string;
|
|
19
|
-
type: 'request' | 'response' | 'event' | NuiWindowAction;
|
|
20
|
-
/**
|
|
21
|
-
* Absent on responses - the correlation `id` already identifies the call.
|
|
22
|
-
*/
|
|
23
|
-
event?: string;
|
|
24
|
-
payload?: unknown;
|
|
25
|
-
success?: boolean;
|
|
26
|
-
error?: NuiError;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Result of an NUI operation.
|
|
31
|
-
*/
|
|
32
|
-
export interface NuiResult<T> {
|
|
33
|
-
success: boolean;
|
|
34
|
-
value?: T;
|
|
35
|
-
error?: NuiError;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Maps NUI event names to their request/response or payload shapes.
|
|
40
|
-
*
|
|
41
|
-
* Empty by default. A contracts module generated by `FiveM.Nui.Contracts`
|
|
42
|
-
* augments this interface, so that {@link NuiClient} calls are checked against
|
|
43
|
-
* the C# contract: a name that is not in the map fails to compile, and the
|
|
44
|
-
* request and response types are inferred from the entry.
|
|
45
|
-
*
|
|
46
|
-
* @example
|
|
47
|
-
* ```ts
|
|
48
|
-
* declare module '@fivem-nui/core' {
|
|
49
|
-
* interface NuiEventMap {
|
|
50
|
-
* 'inventory:use': { request: UseItemRequest; response: UseItemResponse };
|
|
51
|
-
* 'player:healthChanged': { payload: HealthChanged };
|
|
52
|
-
* }
|
|
53
|
-
* }
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
export interface NuiEventMap {}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Event names accepted by {@link NuiClient}. Falls back to any string while
|
|
60
|
-
* {@link NuiEventMap} is empty, so the client stays usable without generated
|
|
61
|
-
* contracts and becomes strict as soon as the map is augmented.
|
|
62
|
-
*/
|
|
63
|
-
export type NuiEventKey = keyof NuiEventMap extends never ? string : keyof NuiEventMap;
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Discriminates event map entries by their communication pattern.
|
|
67
|
-
* - `request` - events that expect a request from the UI (have both request and response)
|
|
68
|
-
* - `response` - events that only have a response (no request from UI)
|
|
69
|
-
* - `payload` - fire-and-forget events
|
|
70
|
-
* - `event` - events that don't fit other categories (fallback)
|
|
71
|
-
*/
|
|
72
|
-
type EventPattern<E> = E extends keyof NuiEventMap
|
|
73
|
-
? NuiEventMap[E] extends { request: any; response: any }
|
|
74
|
-
? 'request'
|
|
75
|
-
: NuiEventMap[E] extends { response: any }
|
|
76
|
-
? 'response'
|
|
77
|
-
: NuiEventMap[E] extends { payload: any }
|
|
78
|
-
? 'payload'
|
|
79
|
-
: 'event'
|
|
80
|
-
: 'event';
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Extracts the event pattern (request/response/payload) for a mapped event.
|
|
84
|
-
* Useful for conditional typing in overload resolution.
|
|
85
|
-
*/
|
|
86
|
-
export type EventCommunication<E> = EventPattern<E>;
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Payload type for `send` and `sendAndWait` methods.
|
|
90
|
-
* - When the event has `payload`, extracts that type (required parameter).
|
|
91
|
-
* - When `NuiEventMap` is empty, returns `unknown` (fallback behavior).
|
|
92
|
-
* - For other event types, returns `undefined` (payload not applicable, but call allowed).
|
|
93
|
-
*/
|
|
94
|
-
export type SendPayload<E> = E extends keyof NuiEventMap
|
|
95
|
-
? NuiEventMap[E] extends { payload: infer TPayload }
|
|
96
|
-
? TPayload
|
|
97
|
-
: undefined
|
|
98
|
-
: unknown;
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Request type for `request` method.
|
|
102
|
-
* - When the event has `request`, extracts that type (required parameter).
|
|
103
|
-
* - When the event doesn't have `request`, returns `undefined` (request not applicable, but call allowed).
|
|
104
|
-
* - When `NuiEventMap` is empty, returns `unknown` (fallback behavior).
|
|
105
|
-
*/
|
|
106
|
-
export type RequestPayload<E> = E extends keyof NuiEventMap
|
|
107
|
-
? NuiEventMap[E] extends { request: infer TRequest }
|
|
108
|
-
? TRequest
|
|
109
|
-
: undefined
|
|
110
|
-
: unknown;
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Response type for `request` method.
|
|
114
|
-
* - When the event has `response`, returns that type.
|
|
115
|
-
* - When `NuiEventMap` is empty, returns `unknown` (fallback behavior).
|
|
116
|
-
*/
|
|
117
|
-
export type RequestResponse<E> = E extends keyof NuiEventMap
|
|
118
|
-
? NuiEventMap[E] extends { response: infer TResponse }
|
|
119
|
-
? TResponse
|
|
120
|
-
: unknown
|
|
121
|
-
: unknown;
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Request payload type for a mapped event, or `undefined` when the event takes
|
|
125
|
-
* none. Resolves to `unknown` while {@link NuiEventMap} is empty.
|
|
126
|
-
*/
|
|
127
|
-
export type NuiEventRequest<E> = E extends keyof NuiEventMap
|
|
128
|
-
? NuiEventMap[E] extends { request: infer TRequest }
|
|
129
|
-
? TRequest
|
|
130
|
-
: undefined
|
|
131
|
-
: unknown;
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Response payload type for a mapped event. Resolves to `unknown` while
|
|
135
|
-
* {@link NuiEventMap} is empty, preserving the untyped behaviour.
|
|
136
|
-
*/
|
|
137
|
-
export type NuiEventResponse<E> = E extends keyof NuiEventMap
|
|
138
|
-
? NuiEventMap[E] extends { response: infer TResponse }
|
|
139
|
-
? TResponse
|
|
140
|
-
: unknown
|
|
141
|
-
: unknown;
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Fire-and-forget payload type for a mapped event. Resolves to `unknown` while
|
|
145
|
-
* {@link NuiEventMap} is empty.
|
|
146
|
-
*/
|
|
147
|
-
export type NuiEventPayload<E> = E extends keyof NuiEventMap
|
|
148
|
-
? NuiEventMap[E] extends { payload: infer TPayload }
|
|
149
|
-
? TPayload
|
|
150
|
-
: undefined
|
|
151
|
-
: unknown;
|
|
1
|
+
/**
|
|
2
|
+
* Represents an error in NUI communication.
|
|
3
|
+
*/
|
|
4
|
+
export interface NuiError {
|
|
5
|
+
code: string;
|
|
6
|
+
message: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Window lifecycle envelope type sent by C# window management.
|
|
11
|
+
*/
|
|
12
|
+
export type NuiWindowAction = 'open' | 'close' | 'focus' | 'unfocus';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Represents a message sent between C# and NUI.
|
|
16
|
+
*/
|
|
17
|
+
export interface NuiMessage {
|
|
18
|
+
id?: string;
|
|
19
|
+
type: 'request' | 'response' | 'event' | NuiWindowAction;
|
|
20
|
+
/**
|
|
21
|
+
* Absent on responses - the correlation `id` already identifies the call.
|
|
22
|
+
*/
|
|
23
|
+
event?: string;
|
|
24
|
+
payload?: unknown;
|
|
25
|
+
success?: boolean;
|
|
26
|
+
error?: NuiError;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Result of an NUI operation.
|
|
31
|
+
*/
|
|
32
|
+
export interface NuiResult<T> {
|
|
33
|
+
success: boolean;
|
|
34
|
+
value?: T;
|
|
35
|
+
error?: NuiError;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Maps NUI event names to their request/response or payload shapes.
|
|
40
|
+
*
|
|
41
|
+
* Empty by default. A contracts module generated by `FiveM.Nui.Contracts`
|
|
42
|
+
* augments this interface, so that {@link NuiClient} calls are checked against
|
|
43
|
+
* the C# contract: a name that is not in the map fails to compile, and the
|
|
44
|
+
* request and response types are inferred from the entry.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* declare module '@fivem-nui/core' {
|
|
49
|
+
* interface NuiEventMap {
|
|
50
|
+
* 'inventory:use': { request: UseItemRequest; response: UseItemResponse };
|
|
51
|
+
* 'player:healthChanged': { payload: HealthChanged };
|
|
52
|
+
* }
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export interface NuiEventMap {}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Event names accepted by {@link NuiClient}. Falls back to any string while
|
|
60
|
+
* {@link NuiEventMap} is empty, so the client stays usable without generated
|
|
61
|
+
* contracts and becomes strict as soon as the map is augmented.
|
|
62
|
+
*/
|
|
63
|
+
export type NuiEventKey = keyof NuiEventMap extends never ? string : keyof NuiEventMap;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Discriminates event map entries by their communication pattern.
|
|
67
|
+
* - `request` - events that expect a request from the UI (have both request and response)
|
|
68
|
+
* - `response` - events that only have a response (no request from UI)
|
|
69
|
+
* - `payload` - fire-and-forget events
|
|
70
|
+
* - `event` - events that don't fit other categories (fallback)
|
|
71
|
+
*/
|
|
72
|
+
type EventPattern<E> = E extends keyof NuiEventMap
|
|
73
|
+
? NuiEventMap[E] extends { request: any; response: any }
|
|
74
|
+
? 'request'
|
|
75
|
+
: NuiEventMap[E] extends { response: any }
|
|
76
|
+
? 'response'
|
|
77
|
+
: NuiEventMap[E] extends { payload: any }
|
|
78
|
+
? 'payload'
|
|
79
|
+
: 'event'
|
|
80
|
+
: 'event';
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Extracts the event pattern (request/response/payload) for a mapped event.
|
|
84
|
+
* Useful for conditional typing in overload resolution.
|
|
85
|
+
*/
|
|
86
|
+
export type EventCommunication<E> = EventPattern<E>;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Payload type for `send` and `sendAndWait` methods.
|
|
90
|
+
* - When the event has `payload`, extracts that type (required parameter).
|
|
91
|
+
* - When `NuiEventMap` is empty, returns `unknown` (fallback behavior).
|
|
92
|
+
* - For other event types, returns `undefined` (payload not applicable, but call allowed).
|
|
93
|
+
*/
|
|
94
|
+
export type SendPayload<E> = E extends keyof NuiEventMap
|
|
95
|
+
? NuiEventMap[E] extends { payload: infer TPayload }
|
|
96
|
+
? TPayload
|
|
97
|
+
: undefined
|
|
98
|
+
: unknown;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Request type for `request` method.
|
|
102
|
+
* - When the event has `request`, extracts that type (required parameter).
|
|
103
|
+
* - When the event doesn't have `request`, returns `undefined` (request not applicable, but call allowed).
|
|
104
|
+
* - When `NuiEventMap` is empty, returns `unknown` (fallback behavior).
|
|
105
|
+
*/
|
|
106
|
+
export type RequestPayload<E> = E extends keyof NuiEventMap
|
|
107
|
+
? NuiEventMap[E] extends { request: infer TRequest }
|
|
108
|
+
? TRequest
|
|
109
|
+
: undefined
|
|
110
|
+
: unknown;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Response type for `request` method.
|
|
114
|
+
* - When the event has `response`, returns that type.
|
|
115
|
+
* - When `NuiEventMap` is empty, returns `unknown` (fallback behavior).
|
|
116
|
+
*/
|
|
117
|
+
export type RequestResponse<E> = E extends keyof NuiEventMap
|
|
118
|
+
? NuiEventMap[E] extends { response: infer TResponse }
|
|
119
|
+
? TResponse
|
|
120
|
+
: unknown
|
|
121
|
+
: unknown;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Request payload type for a mapped event, or `undefined` when the event takes
|
|
125
|
+
* none. Resolves to `unknown` while {@link NuiEventMap} is empty.
|
|
126
|
+
*/
|
|
127
|
+
export type NuiEventRequest<E> = E extends keyof NuiEventMap
|
|
128
|
+
? NuiEventMap[E] extends { request: infer TRequest }
|
|
129
|
+
? TRequest
|
|
130
|
+
: undefined
|
|
131
|
+
: unknown;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Response payload type for a mapped event. Resolves to `unknown` while
|
|
135
|
+
* {@link NuiEventMap} is empty, preserving the untyped behaviour.
|
|
136
|
+
*/
|
|
137
|
+
export type NuiEventResponse<E> = E extends keyof NuiEventMap
|
|
138
|
+
? NuiEventMap[E] extends { response: infer TResponse }
|
|
139
|
+
? TResponse
|
|
140
|
+
: unknown
|
|
141
|
+
: unknown;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Fire-and-forget payload type for a mapped event. Resolves to `unknown` while
|
|
145
|
+
* {@link NuiEventMap} is empty.
|
|
146
|
+
*/
|
|
147
|
+
export type NuiEventPayload<E> = E extends keyof NuiEventMap
|
|
148
|
+
? NuiEventMap[E] extends { payload: infer TPayload }
|
|
149
|
+
? TPayload
|
|
150
|
+
: undefined
|
|
151
|
+
: unknown;
|