feat(package): TS callback events

This commit is contained in:
Linden
2022-12-11 12:40:19 +11:00
parent 124a5f15ba
commit 8abc416dad
4 changed files with 94 additions and 1 deletions

View File

@@ -0,0 +1,38 @@
import { cache } from '../cache';
const activeEvents: Record<string, Function> = {};
onNet(`__ox_cb_${cache.resource}`, (key: string, ...args: any) => {
const resolve = activeEvents[key];
return resolve && resolve(...args);
});
export function triggerClientCallback(eventName: string, playerId: number, ...args: any) {
let key: string;
do {
key = `${eventName}:${Math.floor(Math.random() * (100000 + 1))}:${playerId}`;
} while (activeEvents[key]);
emitNet(`__ox_cb_${eventName}`, playerId, cache.resource, key, ...args);
return new Promise((resolve) => {
activeEvents[key] = resolve;
});
}
export function onClientCallback(eventName: string, cb: (playerId: number, ...args: any) => any) {
onNet(`__ox_cb_${eventName}`, (resource: string, key: string, ...args: any) => {
const src = source;
let response: any;
try {
response = cb(src, ...args);
} catch (e: any) {
console.error(`an error occurred while handling callback event ${eventName}`);
console.log(`^3${e.stack}^0`);
}
emitNet(`__ox_cb_${resource}`, src, key, response);
});
}

View File

@@ -1,2 +1,4 @@
export * from './acl';
export * from './locale';
export * from './callback';
export * from './version';