mirror of
https://github.com/centau/vide.git
synced 2026-08-20 23:01:37 +00:00
Add flag to disable setting of default properties
This commit is contained in:
parent
3c2fafa1cd
commit
5f752fe903
3 changed files with 18 additions and 12 deletions
|
|
@ -3,23 +3,26 @@ local Instance = game and Instance or require "../test/mock".Instance :: never
|
|||
|
||||
local defaults = require "./defaults"
|
||||
local apply = require "./apply"
|
||||
local flags = require "./flags"
|
||||
|
||||
local ctor_cache = {} :: { [string]: () -> Instance }
|
||||
|
||||
setmetatable(ctor_cache :: any, {
|
||||
__index = function(self, class)
|
||||
local ok, instance: Instance = pcall(Instance.new, class :: any)
|
||||
if not ok then error(`invalid class name, could not create instance of class { class }`, 0) end
|
||||
|
||||
local default: { [string]: unknown }? = defaults[class]
|
||||
if default then
|
||||
for i, v in next, default do
|
||||
(instance :: any)[i] = v
|
||||
end
|
||||
end
|
||||
|
||||
local function ctor(properties: Props): Instance
|
||||
return apply(instance:Clone(), properties)
|
||||
local ok, instance: Instance = pcall(Instance.new, class :: any)
|
||||
if not ok then error(`invalid class name { class }`, 0) end
|
||||
|
||||
if flags.defaults then
|
||||
local default: { [string]: unknown }? = defaults[class]
|
||||
if default then
|
||||
for i, v in default do
|
||||
(instance :: any)[i] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return apply(instance, properties)
|
||||
end
|
||||
|
||||
self[class] = ctor
|
||||
|
|
|
|||
|
|
@ -7,5 +7,7 @@ local is_O2 = inline_test() ~= "inline_test"
|
|||
return {
|
||||
strict = not is_O2,
|
||||
batch = false,
|
||||
defer_nested_properties = true
|
||||
defaults = true,
|
||||
use_default_properties = true,
|
||||
defer_nested_properties = true,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue