Add flag to disable setting of default properties

This commit is contained in:
centauri 2025-06-23 21:36:51 +01:00
parent 3c2fafa1cd
commit 5f752fe903
3 changed files with 18 additions and 12 deletions

View file

@ -19,6 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- `show()` now receives a source to its callback returning the current value - `show()` now receives a source to its callback returning the current value
of the condition. of the condition.
- Ignore `false` passed as a child. - Ignore `false` passed as a child.
- Flag `vide.defaults` to disable the setting of default properties.
### Changed ### Changed

View file

@ -3,23 +3,26 @@ local Instance = game and Instance or require "../test/mock".Instance :: never
local defaults = require "./defaults" local defaults = require "./defaults"
local apply = require "./apply" local apply = require "./apply"
local flags = require "./flags"
local ctor_cache = {} :: { [string]: () -> Instance } local ctor_cache = {} :: { [string]: () -> Instance }
setmetatable(ctor_cache :: any, { setmetatable(ctor_cache :: any, {
__index = function(self, class) __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 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 end
self[class] = ctor self[class] = ctor

View file

@ -7,5 +7,7 @@ local is_O2 = inline_test() ~= "inline_test"
return { return {
strict = not is_O2, strict = not is_O2,
batch = false, batch = false,
defer_nested_properties = true defaults = true,
use_default_properties = true,
defer_nested_properties = true,
} }