mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +00:00
Support nesting Parent (#48)
* allow nesting parent property * update changelog * fix mock instances Parent property not being a proxy * add tests Closes #47
This commit is contained in:
parent
7064489a36
commit
37e8e05206
4 changed files with 39 additions and 21 deletions
|
|
@ -28,6 +28,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
||||||
destroyed.
|
destroyed.
|
||||||
- Error reporting should be improved with better formatting when effects invoke
|
- Error reporting should be improved with better formatting when effects invoke
|
||||||
other effects and no more loss of stack traces.
|
other effects and no more loss of stack traces.
|
||||||
|
- Nesting parent properties now work, and they are now also checked for
|
||||||
|
duplicates like other properties.
|
||||||
|
|
||||||
### Removed
|
### Removed
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,9 @@ type Cache = {
|
||||||
Array<(Instance) -> ()> -- action callbacks
|
Array<(Instance) -> ()> -- action callbacks
|
||||||
>,
|
>,
|
||||||
|
|
||||||
|
-- what to parent the instance to after running actions
|
||||||
|
parent: unknown,
|
||||||
|
|
||||||
-- cache to detect duplicate property setting at same nesting depth
|
-- cache to detect duplicate property setting at same nesting depth
|
||||||
nested_debug: Map<
|
nested_debug: Map<
|
||||||
number, -- depth
|
number, -- depth
|
||||||
|
|
@ -47,6 +50,7 @@ local function borrow_cache(): Cache
|
||||||
actions = setmetatable({} :: any, { -- lazy init
|
actions = setmetatable({} :: any, { -- lazy init
|
||||||
__index = function(self, i) self[i] = {}; return self[i] end
|
__index = function(self, i) self[i] = {}; return self[i] end
|
||||||
}),
|
}),
|
||||||
|
parent = nil,
|
||||||
nested_debug = setmetatable({} :: any, {
|
nested_debug = setmetatable({} :: any, {
|
||||||
__index = function(self, i: number) self[i] = {}; return self[i] end
|
__index = function(self, i: number) self[i] = {}; return self[i] end
|
||||||
}),
|
}),
|
||||||
|
|
@ -61,8 +65,6 @@ end
|
||||||
|
|
||||||
local function process_properties(properties: Map<unknown, unknown>, instance: Instance, cache: Cache, depth: number)
|
local function process_properties(properties: Map<unknown, unknown>, instance: Instance, cache: Cache, depth: number)
|
||||||
for property, value in properties do
|
for property, value in properties do
|
||||||
if property == "Parent" then continue end
|
|
||||||
|
|
||||||
if type(property) == "string" then
|
if type(property) == "string" then
|
||||||
if flags.strict then -- check for duplicate property assignment at nesting depth
|
if flags.strict then -- check for duplicate property assignment at nesting depth
|
||||||
if cache.nested_debug[depth][property] then
|
if cache.nested_debug[depth][property] then
|
||||||
|
|
@ -71,6 +73,11 @@ local function process_properties(properties: Map<unknown, unknown>, instance: I
|
||||||
cache.nested_debug[depth][property] = true
|
cache.nested_debug[depth][property] = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if property == "Parent" then
|
||||||
|
cache.parent = value
|
||||||
|
continue
|
||||||
|
end
|
||||||
|
|
||||||
if type(value) == "function" then
|
if type(value) == "function" then
|
||||||
if typeof((instance :: any)[property]) == "RBXScriptSignal" then
|
if typeof((instance :: any)[property]) == "RBXScriptSignal" then
|
||||||
table.insert(cache.events, property) -- add event name to buffer
|
table.insert(cache.events, property) -- add event name to buffer
|
||||||
|
|
@ -106,9 +113,6 @@ local function apply<T>(instance: T & Instance, properties: { [unknown]: unknown
|
||||||
error "attempt to call a constructor returned by create() with no properties"
|
error "attempt to call a constructor returned by create() with no properties"
|
||||||
end
|
end
|
||||||
|
|
||||||
-- queue parent assignment if any for last
|
|
||||||
local parent: unknown = properties.Parent
|
|
||||||
|
|
||||||
local caches = borrow_cache()
|
local caches = borrow_cache()
|
||||||
local events = caches.events
|
local events = caches.events
|
||||||
local actions = caches.actions
|
local actions = caches.actions
|
||||||
|
|
@ -135,6 +139,7 @@ local function apply<T>(instance: T & Instance, properties: { [unknown]: unknown
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local parent = caches.parent
|
||||||
if parent then
|
if parent then
|
||||||
if type(parent) == "function" then
|
if type(parent) == "function" then
|
||||||
implicit_effect.parent(instance, parent :: () -> Instance)
|
implicit_effect.parent(instance, parent :: () -> Instance)
|
||||||
|
|
@ -145,6 +150,7 @@ local function apply<T>(instance: T & Instance, properties: { [unknown]: unknown
|
||||||
|
|
||||||
table.clear(events)
|
table.clear(events)
|
||||||
for _, queued in next, actions do table.clear(queued) end
|
for _, queued in next, actions do table.clear(queued) end
|
||||||
|
caches.parent = nil
|
||||||
if flags.strict then table.clear(nested_debug) end
|
if flags.strict then table.clear(nested_debug) end
|
||||||
table.clear(nested_stack)
|
table.clear(nested_stack)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -86,6 +86,9 @@ local Instance = {} :: any do
|
||||||
local proxies = {} :: { [Data]: userdata? }
|
local proxies = {} :: { [Data]: userdata? }
|
||||||
setmetatable(proxies :: any, { __mode = "v" })
|
setmetatable(proxies :: any, { __mode = "v" })
|
||||||
|
|
||||||
|
-- allocate variables for the metamethods as __index and get_proxy cross refrences each other
|
||||||
|
local __index, __newindex
|
||||||
|
|
||||||
local function get_data(userdata: userdata): Data
|
local function get_data(userdata: userdata): Data
|
||||||
local function f(userdata: userdata): ProxyMT
|
local function f(userdata: userdata): ProxyMT
|
||||||
return getmetatable(userdata :: any)
|
return getmetatable(userdata :: any)
|
||||||
|
|
@ -94,6 +97,19 @@ local Instance = {} :: any do
|
||||||
return f(userdata).data
|
return f(userdata).data
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function get_proxy(data: Data): userdata
|
||||||
|
return proxies[data] or (function()
|
||||||
|
local userdata = newproxy(true)
|
||||||
|
local proxy = getmetatable(userdata)
|
||||||
|
proxy.proxy = userdata
|
||||||
|
proxy.data = data
|
||||||
|
proxy.__index = __index
|
||||||
|
proxy.__newindex = __newindex
|
||||||
|
proxies[data] = userdata
|
||||||
|
return userdata
|
||||||
|
end)()
|
||||||
|
end
|
||||||
|
|
||||||
local function is_instance(value: unknown): boolean
|
local function is_instance(value: unknown): boolean
|
||||||
local mt = getmetatable(value :: any)
|
local mt = getmetatable(value :: any)
|
||||||
return mt and mt.data and mt.data.type == "Instance"
|
return mt and mt.data and mt.data.type == "Instance"
|
||||||
|
|
@ -101,16 +117,16 @@ local Instance = {} :: any do
|
||||||
|
|
||||||
local methods = {}
|
local methods = {}
|
||||||
|
|
||||||
local function __index(userdata: userdata, property: string): ()
|
__index = function(userdata: userdata, property: string): ()
|
||||||
local data = get_data(userdata)
|
local data = get_data(userdata)
|
||||||
return if methods[property] then methods[property]
|
return if methods[property] then methods[property]
|
||||||
elseif property == "Name" then data.name
|
elseif property == "Name" then data.name
|
||||||
elseif property == "Parent" then data.parent
|
elseif property == "Parent" then (data.parent and get_proxy(data.parent))
|
||||||
elseif property == "Destroying" then data.destroying
|
elseif property == "Destroying" then data.destroying
|
||||||
else data.properties[property]
|
else data.properties[property]
|
||||||
end
|
end
|
||||||
|
|
||||||
local function __newindex(userdata: userdata, property: string, value: unknown)
|
__newindex = function(userdata: userdata, property: string, value: unknown)
|
||||||
local data = get_data(userdata)
|
local data = get_data(userdata)
|
||||||
if property == "Name" then
|
if property == "Name" then
|
||||||
if type(value) ~= "string" then error("name must be a string", 2) end
|
if type(value) ~= "string" then error("name must be a string", 2) end
|
||||||
|
|
@ -135,19 +151,6 @@ local Instance = {} :: any do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_proxy(data: Data): userdata
|
|
||||||
return proxies[data] or (function()
|
|
||||||
local userdata = newproxy(true)
|
|
||||||
local proxy = getmetatable(userdata)
|
|
||||||
proxy.proxy = userdata
|
|
||||||
proxy.data = data
|
|
||||||
proxy.__index = __index
|
|
||||||
proxy.__newindex = __newindex
|
|
||||||
proxies[data] = userdata
|
|
||||||
return userdata
|
|
||||||
end)()
|
|
||||||
end
|
|
||||||
|
|
||||||
function Instance.new(class: string): Instance
|
function Instance.new(class: string): Instance
|
||||||
local data = {
|
local data = {
|
||||||
name = "UNNAMED",
|
name = "UNNAMED",
|
||||||
|
|
|
||||||
|
|
@ -759,6 +759,13 @@ TEST("create()", wrap_root(function()
|
||||||
CHECK(text.Text == "test")
|
CHECK(text.Text == "test")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
do CASE "set nested parent"
|
||||||
|
local frame = create "Frame" {}
|
||||||
|
local text = create "TextLavel" { { Parent = frame } }
|
||||||
|
CHECK(frame:GetChildren()[1] == text)
|
||||||
|
CHECK(text.Parent == frame)
|
||||||
|
end
|
||||||
|
|
||||||
do CASE "nested deferred"
|
do CASE "nested deferred"
|
||||||
local text = create "TextLabel" {
|
local text = create "TextLabel" {
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue