From 2addf179cbb73cd2a68e47c49fae1abc346a549f Mon Sep 17 00:00:00 2001 From: Daniel P H Fox Date: Thu, 18 Apr 2024 17:05:52 +0100 Subject: [PATCH] Separate out scope merge function --- src/Memory/scoped.luau | 23 ++--------------------- src/Utility/merge.luau | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 21 deletions(-) create mode 100644 src/Utility/merge.luau diff --git a/src/Memory/scoped.luau b/src/Memory/scoped.luau index d62e09a60..9a552afff 100644 --- a/src/Memory/scoped.luau +++ b/src/Memory/scoped.luau @@ -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 diff --git a/src/Utility/merge.luau b/src/Utility/merge.luau new file mode 100644 index 000000000..94542922f --- /dev/null +++ b/src/Utility/merge.luau @@ -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