From 6b12831817ba17b1962a73a817b8f879c718f508 Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Tue, 13 Aug 2024 14:11:27 +1000 Subject: [PATCH] feat(cache): send oldValue to onCache Internally handle the cache events to prevent race conditions and resolve #618. Improved types. --- init.lua | 28 ++++++++++++++++++++----- package/shared/resource/cache/index.ts | 29 +++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/init.lua b/init.lua index f3429cc1a..83eda560f 100644 --- a/init.lua +++ b/init.lua @@ -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, @@ -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) @@ -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) diff --git a/package/shared/resource/cache/index.ts b/package/shared/resource/cache/index.ts index c3aecfac9..03d849c78 100644 --- a/package/shared/resource/cache/index.ts +++ b/package/shared/resource/cache/index.ts @@ -1,13 +1,34 @@ -export const cache: Record = 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 = {}; + +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; }); @@ -17,6 +38,8 @@ export const cache: Record = new Proxy( } ); -export const onCache = (key: string, cb: (value: T) => void) => { - AddEventHandler(`ox_lib:cache:${key}`, cb); +export const onCache = (key: T, cb: (value: OxCache[T], oldValue: OxCache[T]) => void) => { + if (!cacheEvents[key]) cache[key]; + + cacheEvents[key].push(cb); };