fix fragments in effects

This commit is contained in:
alice 2024-08-18 13:30:52 +02:00 committed by centau
parent 72e5fbb6fe
commit 1f76b66ff9
2 changed files with 13 additions and 3 deletions

View file

@ -11,6 +11,7 @@ local graph = require(script.Parent.graph)
type Node<T> = graph.Node<T>
type Array<V> = { V }
type ArrayOrV<V> = {ArrayOrV<V>} | V
type Map<K, V> = { [K]: V }
local free_caches: {
@ -124,7 +125,7 @@ local function apply<T>(instance: T & Instance, properties: { [unknown]: unknown
end
elseif type(property) == "number" then
if type(value) == "function" then
bind.children(instance, value :: () -> Instance | Array<Instance>) -- bind children
bind.children(instance, value :: () -> ArrayOrV<Instance>) -- bind children
elseif type(value) == "table" then
if is_action(value) then
table.insert(actions[(value :: any).priority], (value :: any).callback :: () -> ()) -- add action to buffer

View file

@ -38,6 +38,7 @@ type ChildrenBinding = {
children: () -> Instance | { Instance }
}
type ArrayOrV<V> = V | { V }
local function update_children_effect(p: ChildrenBinding)
local cur_children_set: { [Instance]: true } = p.cur_children_set -- cache of all children parented before update
local new_child_set: { [Instance]: true } = p.new_children_set -- cache of all children parented after update
@ -48,8 +49,14 @@ local function update_children_effect(p: ChildrenBinding)
new_children = { new_children }
end
if new_children then
for _, child in next, new_children :: { Instance } do
local function process_child(child: ArrayOrV<Instance>)
if type(child) == "table" then
for _, child in next, child do
process_child(child)
end
else
if new_child_set[child] then return end -- stops redundant reparenting
new_child_set[child] = true -- record child set from this update
if not cur_children_set[child] then
child.Parent = p.instance -- if child wasn't already parented then parent it
@ -59,6 +66,8 @@ local function update_children_effect(p: ChildrenBinding)
end
end
process_child(new_children)
for child in next, cur_children_set do
child.Parent = nil -- unparent all children that weren't in the new children set
end