2024-08-13 14:11:27 +10:00
|
|
|
interface OxCache {
|
|
|
|
|
ped: number;
|
|
|
|
|
vehicle: number | false;
|
|
|
|
|
seat: number | false;
|
|
|
|
|
game: string;
|
|
|
|
|
resource: string;
|
|
|
|
|
playerId: number;
|
|
|
|
|
serverId: number;
|
|
|
|
|
[key: string]: unknown;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const cacheEvents: Record<keyof OxCache, Function[]> = {};
|
|
|
|
|
|
|
|
|
|
export const cache: OxCache = new Proxy(
|
2022-09-18 00:48:04 +10:00
|
|
|
{
|
|
|
|
|
resource: GetCurrentResourceName(),
|
2024-08-13 14:11:27 +10:00
|
|
|
game: GetGameName(),
|
2022-09-18 00:48:04 +10:00
|
|
|
},
|
|
|
|
|
{
|
2023-09-16 10:37:14 +02:00
|
|
|
get(target: any, key: string) {
|
2022-09-17 20:21:06 +02:00
|
|
|
const result = key ? target[key] : target;
|
2022-09-18 22:53:04 +10:00
|
|
|
if (result !== undefined) return result;
|
2022-09-18 00:48:04 +10:00
|
|
|
|
2024-08-13 14:11:27 +10:00
|
|
|
cacheEvents[key] = [];
|
|
|
|
|
|
2022-09-18 00:48:04 +10:00
|
|
|
AddEventHandler(`ox_lib:cache:${key}`, (value: any) => {
|
2024-08-13 14:11:27 +10:00
|
|
|
const oldValue = target[key];
|
|
|
|
|
const events = cacheEvents[key];
|
|
|
|
|
|
|
|
|
|
events.forEach((cb) => cb(value, oldValue));
|
|
|
|
|
|
2022-09-18 00:48:04 +10:00
|
|
|
target[key] = value;
|
|
|
|
|
});
|
|
|
|
|
|
2022-09-18 22:53:04 +10:00
|
|
|
target[key] = exports.ox_lib.cache(key) || false;
|
2022-09-18 00:48:04 +10:00
|
|
|
return target[key];
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2024-08-13 14:11:27 +10:00
|
|
|
export const onCache = <T extends keyof OxCache>(key: T, cb: (value: OxCache[T], oldValue: OxCache[T]) => void) => {
|
|
|
|
|
if (!cacheEvents[key]) cache[key];
|
|
|
|
|
|
|
|
|
|
cacheEvents[key].push(cb);
|
2022-09-18 00:48:04 +10:00
|
|
|
};
|