Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into update-branch
Browse files Browse the repository at this point in the history
  • Loading branch information
ggfto committed Aug 21, 2024
2 parents 98e5525 + 5ac07d8 commit e4b6106
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
2 changes: 1 addition & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ rdr3_warning 'I acknowledge that this is a prerelease build of RedM, and I am aw

name 'ox_lib'
author 'Overextended'
version '3.24.0'
version '3.25.0'
license 'LGPL-3.0-or-later'
repository 'https://github.com/overextended/ox_lib'
description 'A library of shared functions to utilise in other resources.'
Expand Down
28 changes: 23 additions & 5 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ end
local lib = setmetatable({
name = ox_lib,
context = context,
onCache = function(key, cb)
AddEventHandler(('ox_lib:cache:%s'):format(key), cb)
end
}, {
__index = call,
__call = call,
Expand Down Expand Up @@ -173,14 +170,27 @@ end
---Caches the result of a function, optionally clearing it after timeout ms.
function cache(key, func, timeout) end

local cacheEvents = {}

local cache = setmetatable({ game = GetGameName(), resource = resourceName }, {
__index = context == 'client' and function(self, key)
__index = function(self, key)
cacheEvents[key] = {}

AddEventHandler(('ox_lib:cache:%s'):format(key), function(value)
local oldValue = self[key]
local events = cacheEvents[key]

for i = 1, #events do
Citizen.CreateThreadNow(function()
events[i](value, oldValue)
end)
end

self[key] = value
end)

return rawset(self, key, export.cache(nil, key) or false)[key]
end or nil,
end,

__call = function(self, key, func, timeout)
local value = rawget(self, key)
Expand All @@ -197,6 +207,14 @@ local cache = setmetatable({ game = GetGameName(), resource = resourceName }, {
end,
})

function lib.onCache(key, cb)
if not cacheEvents[key] then
getmetatable(cache).__index(cache, key)
end

table.insert(cacheEvents[key], cb)
end

_ENV.cache = cache

local notifyEvent = ('__ox_notify_%s'):format(cache.resource)
Expand Down
2 changes: 1 addition & 1 deletion package/server/resource/addCommand/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ setTimeout(() => {
emitNet('chat:addSuggestions', -1, registeredCommmands);
}, 1000);

on('playerJoining', (source: number) => {
on('playerJoining', () => {
emitNet('chat:addSuggestions', source, registeredCommmands);
});

Expand Down
29 changes: 26 additions & 3 deletions package/shared/resource/cache/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
export const cache: Record<string, any> = new Proxy(
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(
{
resource: GetCurrentResourceName(),
game: GetGameName(),
},
{
get(target: any, key: string) {
const result = key ? target[key] : target;
if (result !== undefined) return result;

cacheEvents[key] = [];

AddEventHandler(`ox_lib:cache:${key}`, (value: any) => {
const oldValue = target[key];
const events = cacheEvents[key];

events.forEach((cb) => cb(value, oldValue));

target[key] = value;
});

Expand All @@ -17,6 +38,8 @@ export const cache: Record<string, any> = new Proxy(
}
);

export const onCache = <T = any>(key: string, cb: (value: T) => void) => {
AddEventHandler(`ox_lib:cache:${key}`, cb);
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);
};

0 comments on commit e4b6106

Please sign in to comment.