This commit is contained in:
J Francisco Rader committed 2021-03-30 02:07:04 -03:00
1 parent ff4381fa01
commit a97d858f99
21 files changed
+659 -161

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
import { ClUtils } from './client';
import events from '../utils/events';
ClUtils.registerNuiServerCallback(events.EMAIL_FETCH_INBOX);
+56
View File
@@ -8,7 +8,14 @@ interface ISettingsParams {
promiseTimeout?: number;
}
interface ICallback {
cb: (err: any, data?: unknown) => void;
timeout: NodeJS.Timeout;
}
export default class ClientUtils {
private readonly _callbacks = new Map<string, ICallback>();
private _settings: ISettings;
private _defaultSettings: ISettings = {
promiseTimeout: 5000,
@@ -51,6 +58,55 @@ export default class ClientUtils {
onNet(listenEventName, handleListenEvent);
});
}
public registerNuiServerCallback<I = any, R = any>(event: string) {
RegisterNuiCallbackType(event);
const uid = uuidv4();
const callbackId = `${event}:${uid}`;
on(`__cfx_nui:${event}`, (data: I, cb: (err: any, data?: R) => void) => {
if (this._callbacks.has(callbackId)) {
cb(new Error(`Nui Server Callback with name "${event}" is already waiting for server!`));
return;
}
emitNet(event, data);
this._callbacks.set(callbackId, {
cb,
timeout: setTimeout(() => {
if (this._callbacks.has(callbackId)) {
this._callbacks
.get(callbackId)
.cb(
new Error(
`Nui Server Callback with name "${event}" timed out after ${this._settings.promiseTimeout}ms`,
),
);
this._callbacks.delete(callbackId);
}
}, this._settings.promiseTimeout),
});
});
onNet(`${event}Success`, (response: R) => {
if (this._callbacks.has(callbackId)) {
const callback = this._callbacks.get(callbackId);
clearTimeout(callback.timeout);
callback.cb(null, response);
this._callbacks.delete(callbackId);
}
});
onNet(`${event}Error`, (err: any) => {
if (this._callbacks.has(callbackId)) {
const callback = this._callbacks.get(callbackId);
clearTimeout(callback.timeout);
callback.cb(err);
this._callbacks.delete(callbackId);
}
});
}
}
export const NuiCallback = (event: string, callback: Function) => {