Playing around with stuff

This commit is contained in:
J Francisco Rader authored and J Francisco Rader committed 2021-03-30 02:07:56 -03:00
1 parent 282e99df18
commit 04670233bc
9 files changed
+98 -37

No files matched your search

+63 -1
View File
@@ -1,4 +1,66 @@
import { ClUtils } from './client';
import events from '../utils/events';
import { uuidv4 } from 'fivem-js';
import { sendEmailEvent } from '../utils/messages';
import apps from '../utils/apps';
ClUtils.registerNuiServerCallback(events.EMAIL_FETCH_INBOX);
// ClUtils.registerNuiServerCallback(apps.EMAIL, events.EMAIL_FETCH_INBOX);
/* RegisterNuiCallbackType(events.EMAIL_FETCH_INBOX);
on(`__cfx_nui:${events.EMAIL_FETCH_INBOX}`, (data: any, cb: any) => {
console.log('wtf', data);
cb([]);
});
*/
interface ICallback {
cb: (err: any, data?: unknown) => void;
timeout: NodeJS.Timeout;
}
const event = events.EMAIL_FETCH_INBOX;
const _callbacks = new Map<string, ICallback>();
const uid = uuidv4();
const callbackId = `${event}:${uid}`;
RegisterNuiCallbackType(event);
on(`__cfx_nui:${event}`, (data: any, cb: (err: any, data?: any) => void) => {
if (_callbacks.has(callbackId)) {
cb(new Error(`Nui Server Callback with name "${event}" is already waiting for server!`));
return;
}
emitNet(event, data);
_callbacks.set(callbackId, {
cb,
timeout: setTimeout(() => {
if (_callbacks.has(callbackId)) {
_callbacks
.get(callbackId)
.cb(new Error(`Nui Server Callback with name "${event}" timed out`));
_callbacks.delete(callbackId);
}
}, 20000),
});
});
onNet(`${event}Success`, (response: any) => {
if (_callbacks.has(callbackId)) {
const callback = _callbacks.get(callbackId);
clearTimeout(callback.timeout);
callback.cb(null, response);
_callbacks.delete(callbackId);
sendEmailEvent(`${event}Success`, response);
}
});
onNet(`${event}Error`, (err: any) => {
if (_callbacks.has(callbackId)) {
const callback = _callbacks.get(callbackId);
clearTimeout(callback.timeout);
callback.cb(err);
_callbacks.delete(callbackId);
sendEmailEvent(`${event}Error`, err);
}
});
+14 -21
View File
@@ -1,4 +1,5 @@
import { uuidv4 } from '../utils/fivem';
import { sendMessage } from '../utils/messages';
interface ISettings {
promiseTimeout: number;
@@ -59,51 +60,43 @@ export default class ClientUtils {
});
}
public registerNuiServerCallback<I = any, R = any>(event: string) {
RegisterNuiCallbackType(event);
public registerNuiServerCallback<I = any, R = any>(
app: string,
event: string,
successEvent?: string,
errorEvent?: string,
) {
const uid = uuidv4();
const callbackId = `${event}:${uid}`;
RegisterNuiCallbackType(event);
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) => {
const _success = successEvent || `${event}Success`;
onNet(_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);
sendMessage(app, _success, response);
}
});
onNet(`${event}Error`, (err: any) => {
const _error = errorEvent || `${event}Error`;
onNet(_error, (err: any) => {
if (this._callbacks.has(callbackId)) {
const callback = this._callbacks.get(callbackId);
clearTimeout(callback.timeout);
callback.cb(err);
this._callbacks.delete(callbackId);
sendMessage(app, _error, err);
}
});
}
+1
View File
@@ -10,6 +10,7 @@ import './cl_photo';
import './cl_messages';
import './cl_call';
import './cl_match';
import './cl_email';
import './functions';
export const ClUtils = new ClientUtils();