Skip to content

Commit

Permalink
refactor(client/api): removal of all target options; garbage collection
Browse files Browse the repository at this point in the history
Make "options" optional when removing target options, instead removing
the target entirely.
Additionally, remove any empty tables for targets after removing options.
  • Loading branch information
thelindat committed Dec 12, 2022
1 parent dedc989 commit 495f298
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions client/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function target.addModel(arr, options)
end

---@param arr number | number[]
---@param options table
---@param options? table
function target.removeModel(arr, options)
if type(arr) ~= 'table' then arr = { arr } end
local resource = GetInvokingResource()
Expand All @@ -148,7 +148,13 @@ function target.removeModel(arr, options)
model = type(model) == 'string' and joaat(model) or model

if Models[model] then
removeTarget(Models[model], options, resource)
if options then
removeTarget(Models[model], options, resource)
end

if not options or #Models[model] == 0 then
Models[model] = nil
end
end
end
end
Expand All @@ -175,7 +181,7 @@ function target.addEntity(arr, options)
end

---@param arr number | number[]
---@param options table
---@param options? table
function target.removeEntity(arr, options)
if type(arr) ~= 'table' then arr = { arr } end
local resource = GetInvokingResource()
Expand All @@ -184,7 +190,13 @@ function target.removeEntity(arr, options)
local netId = arr[i]

if Entities[netId] then
removeTarget(Entities[netId], options, resource)
if options then
removeTarget(Entities[netId], options, resource)
end

if not options or #Entities[netId] == 0 then
Entities[netId] = nil
end
end
end
end
Expand Down Expand Up @@ -213,7 +225,7 @@ function target.addLocalEntity(arr, options)
end

---@param arr number | number[]
---@param options table
---@param options? table
function target.removeLocalEntity(arr, options)
if type(arr) ~= 'table' then arr = { arr } end
local resource = GetInvokingResource()
Expand All @@ -222,7 +234,13 @@ function target.removeLocalEntity(arr, options)
local entity = arr[i]

if LocalEntities[entity] then
removeTarget(LocalEntities[entity], options, resource)
if options then
removeTarget(LocalEntities[entity], options, resource)
end

if not options or #LocalEntities[entity] == 0 then
LocalEntities[entity] = nil
end
end
end
end
Expand All @@ -245,12 +263,18 @@ end
---@param target table
local function removeResourceTargets(resource, target)
for i = 1, #target do
for _, options in pairs(target[i]) do
local tbl = target[i]

for key, options in pairs(tbl) do
for j = #options, 1, -1 do
if options[j].resource == resource then
table.remove(options, j)
end
end

if #options == 0 then
tbl[key] = nil
end
end
end
end
Expand Down

0 comments on commit 495f298

Please sign in to comment.