Skip to content

Commit

Permalink
Separate out scope merge function
Browse files Browse the repository at this point in the history
  • Loading branch information
dphfox committed Apr 18, 2024
1 parent 0f1bd46 commit 2addf17
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
23 changes: 2 additions & 21 deletions src/Memory/scoped.luau
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,15 @@ local task = nil -- Disable usage of Roblox's task scheduler

local Package = script.Parent.Parent
local Types = require(Package.Types)
local logError = require(Package.Logging.logError)
local merge = require(Package.Utility.merge)
local scopePool = require(Package.Memory.scopePool)

local function merge(
into: {[unknown]: unknown},
from: {[unknown]: unknown}?,
...: {[unknown]: unknown}
): {[unknown]: unknown}
if from == nil then
return into
else
for key, value in from do
if into[key] == nil then
into[key] = value
else
logError("mergeConflict", nil, tostring(key))
end
end
return merge(into, ...)
end
end

local function scoped(
...: {[unknown]: unknown}
): {[unknown]: unknown}
return setmetatable(
scopePool.reuseAny() :: any or {},
{__index = merge({}, ...)}
{__index = merge(false, {}, ...)}
) :: any
end

Expand Down
33 changes: 33 additions & 0 deletions src/Utility/merge.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--!strict
--!nolint LocalUnused
--!nolint LocalShadow
local task = nil -- Disable usage of Roblox's task scheduler

--[[
Attempts to merge a variadic number of tables together.
]]

local Package = script.Parent.Parent
local logError = require(Package.Logging.logError)

local function merge(
overwrite: boolean,
into: {[unknown]: unknown},
from: {[unknown]: unknown}?,
...: {[unknown]: unknown}
): {[unknown]: unknown}
if from == nil then
return into
else
for key, value in from do
if into[key] == nil then
into[key] = value
elseif not overwrite then
logError("mergeConflict", nil, tostring(key))
end
end
return merge(overwrite, into, ...)
end
end

return merge

0 comments on commit 2addf17

Please sign in to comment.