feat(package): cache

This commit is contained in:
Linden
2022-09-18 00:48:04 +10:00
parent 1dbfd01f48
commit 95dd3b1838
4 changed files with 31 additions and 1 deletions

View File

@@ -1,4 +1,9 @@
import * as lib from './resource';
export * from './resource';
import { cache, onCache } from '../shared/resource';
cache.playerId = PlayerId();
cache.serverId = GetPlayerServerId(cache.playerId);
export { cache, onCache };
export * from './resource';
export default lib;

View File

@@ -1,4 +1,6 @@
import * as lib from './resource';
import { cache, onCache } from '../shared/resource';
export * from './resource';
export default lib;
export { cache, onCache };

22
package/shared/resource/cache/index.ts vendored Normal file
View File

@@ -0,0 +1,22 @@
export const cache: Record<string, any> = new Proxy(
{
resource: GetCurrentResourceName(),
},
{
get(target, key: string) {
let result = key ? target[key] : target;
if (result) return result;
AddEventHandler(`ox_lib:cache:${key}`, (value: any) => {
target[key] = value;
});
target[key] = exports.ox_lib.cache(key);
return target[key];
},
}
);
export const onCache = (key: string, cb: (value: any) => void) => {
AddEventHandler(`ox_lib:cache:${key}`, cb);
};

View File

@@ -1 +1,2 @@
export * from './version';
export * from './cache';