Skip to content

Commit

Permalink
feat(package): cache
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Sep 17, 2022
1 parent 1dbfd01 commit 95dd3b1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
7 changes: 6 additions & 1 deletion package/client/index.ts
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions package/server/index.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 22 additions & 0 deletions package/shared/resource/cache/index.ts
Original file line number Diff line number Diff line change
@@ -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);
};
1 change: 1 addition & 0 deletions package/shared/resource/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './version';
export * from './cache';

0 comments on commit 95dd3b1

Please sign in to comment.