chore(nui): WIP React rewrite

This commit is contained in:
Luke committed 2021-12-30 20:05:07 +01:00
1 parent ce802e7553
commit b2b1a64c8f
35 files changed
+391 -460

No files matched your search

+1 -1
View File
@@ -13,7 +13,7 @@ interface DebugEvent<T = any> {
* @param timer - How long until it should trigger (ms)
*/
export const debugData = <P>(events: DebugEvent<P>[], timer = 1000): void => {
if (isEnvBrowser()) {
if (process.env.NODE_ENV === "development" && isEnvBrowser()) {
for (const event of events) {
setTimeout(() => {
window.dispatchEvent(
+16 -19
View File
@@ -1,31 +1,28 @@
/**
* @param eventName - The endpoint eventname to target
* @param data - Data you wish to send in the NUI Callback
*
* @return returnData - A promise for the data sent back by the NuiCallbacks CB argument
*/
* Simple wrapper around fetch API tailored for CEF/NUI use. This abstraction
* can be extended to include AbortController if needed or if the response isn't
* JSON. Tailor it to your needs.
*
* @param eventName - The endpoint eventname to target
* @param data - Data you wish to send in the NUI Callback
*
* @return returnData - A promise for the data sent back by the NuiCallbacks CB argument
*/
import { isEnvBrowser } from "./misc";
export async function fetchNui<T = any>(
eventName: string,
data: unknown = {}
): Promise<T> {
export async function fetchNui<T = any>(eventName: string, data?: any): Promise<T> {
const options = {
method: "post",
method: 'post',
headers: {
"Content-Type": "application/json; charset=UTF-8",
'Content-Type': 'application/json; charset=UTF-8',
},
body: JSON.stringify(data),
};
if (isEnvBrowser()) return undefined as any;
const resourceName = (window as any).GetParentResourceName
? (window as any).GetParentResourceName()
: "nui-frame-app";
const resourceName = (window as any).GetParentResourceName ? (window as any).GetParentResourceName() : 'nui-frame-app';
const resp = await fetch(`https://${resourceName}/${eventName}`, options);
return await resp.json();
const respFormatted = await resp.json()
return respFormatted
}
+6 -1
View File
@@ -1 +1,6 @@
export const isEnvBrowser = (): boolean => !(window as any).invokeNative;
// Will return whether the current environment is in a regular browser
// and not CEF
export const isEnvBrowser = (): boolean => !(window as any).invokeNative
// Basic no operation function
export const noop = () => {}
-31
View File
@@ -1,31 +0,0 @@
import { onMount, onDestroy } from "svelte";
interface NuiMessage<T = unknown> {
action: string;
data: T;
}
/**
* A function that manage events listeners for receiving data from the client scripts
* @param action The specific `action` that should be listened for.
* @param handler The callback function that will handle data relayed by this function
*
* @example
* useNuiEvent<{visibility: true, wasVisible: 'something'}>('setVisible', (data) => {
* // whatever logic you want
* })
*
**/
export function useNuiEvent<T = unknown>(
action: string,
handler: (data: T) => void
) {
const eventListener = (event: MessageEvent<NuiMessage<T>>) => {
const { action: eventAction, data } = event.data;
eventAction === action && handler(data);
};
onMount(() => window.addEventListener("message", eventListener));
onDestroy(() => window.removeEventListener("message", eventListener));
}