bug fix for self parenting

This commit is contained in:
세젤귀 고양이들 2022-01-01 20:49:53 +09:00
parent 9189cbcd92
commit a6bc37dbc7
2 changed files with 46 additions and 50 deletions

View file

@ -14,15 +14,16 @@ local module = {};
function module.init(shared) function module.init(shared)
local new = {}; local new = {};
local InstanceNew = Instance.new; local InstanceNew = Instance.new;
local event = shared.event; ---@module src.event local event = shared.event; ---@module "src.event"
local bind = event.bind; local bind = event.bind;
local store = shared.store; ---@module src.store local store = shared.store; ---@module "src.store"
local addObject = store.addObject; local addObject = store.addObject;
local storeNew = store.new; local storeNew = store.new;
local mount = shared.mount; ---@module src.mount local mount = shared.mount; ---@module "src.mount"
local getHolder = mount.getHolder; local getHolder = mount.getHolder;
local advancedTween = shared.advancedTween; ---@module src.libs.AdvancedTween local mountf = mount.mount;
local round = shared.round; ---@module src.libs.round local advancedTween = shared.advancedTween; ---@module "src.libs.AdvancedTween"
local round = shared.round; ---@module "src.libs.round"
local function setProperty(item,index,value,ClassName) local function setProperty(item,index,value,ClassName)
ClassName = ClassName or item.ClassName; ClassName = ClassName or item.ClassName;
@ -69,7 +70,7 @@ function module.init(shared)
item = func(parsed); item = func(parsed);
local holder = getHolder(item); local holder = getHolder(item);
for _,v in ipairs(childs) do for _,v in ipairs(childs) do
mount(item,v,holder); mountf(item,v,holder);
end end
return item; return item;
end end
@ -142,7 +143,7 @@ function module.init(shared)
setProperty(item,index,value,ClassName); setProperty(item,index,value,ClassName);
elseif indexType == "number" then -- object elseif indexType == "number" then -- object
-- child object -- child object
mount(item,((iprop == 1) and value or value:Clone()),holder); mountf(item,((iprop == 1) and value or value:Clone()),holder);
end end
end end
end end
@ -219,7 +220,7 @@ function module.init(shared)
rawset(self,"__holder",object); rawset(self,"__holder",object);
if parent then if parent then
rawset(self,"__parent",parent); rawset(self,"__parent",parent);
mount(self,parent) mountf(self,parent)
end end
-- prevent gc -- prevent gc
((type(object) == "table" and object.__object) or object):GetPropertyChangedSignal "ClassName":Connect(function() ((type(object) == "table" and object.__object) or object):GetPropertyChangedSignal "ClassName":Connect(function()

View file

@ -7,7 +7,7 @@ function module.init(shared)
local mount = {}; local mount = {};
mount.__index = mount; mount.__index = mount;
function mount:unmount() function mount:unmount()
for _,this in ipairs(self.items) do local this = self.this
local typeThis = type(this); local typeThis = type(this);
if typeThis == "userdata" then if typeThis == "userdata" then
this:Destroy(); this:Destroy();
@ -23,20 +23,16 @@ function module.init(shared)
-- todo for remove child on parent -- todo for remove child on parent
end end
end end
end
function new.getHolder(item) function new.getHolder(item)
return (type(item) == "table") and (item._holder or item.holder or item.__holder) or item; return (type(item) == "table") and (item._holder or item.holder or item.__holder) or item;
end end
local getHolder = new.getHolder local getHolder = new.getHolder
-- we should add plugin support -- we should add plugin support
local pack = table.pack; function new.mount(to,this,holder)
function new.mount(to,...) local thisObject = this;
local items = pack(...);
for _,this in ipairs(items) do
local thisO = this;
if type(this) == "table" then if type(this) == "table" then
thisO = this.__object; thisObject = this.__object;
local parent = rawget(this,"__parent"); local parent = rawget(this,"__parent");
if type(parent) == "table" then if type(parent) == "table" then
local parentChild = rawget(parent,"__child"); local parentChild = rawget(parent,"__child");
@ -50,8 +46,8 @@ function module.init(shared)
end end
rawset(this,"__parent",to); rawset(this,"__parent",to);
end end
if thisO then if thisObject then
thisO.Parent = getHolder(to); thisObject.Parent = holder or getHolder(to);
end end
if type(to) == "table" then if type(to) == "table" then
local child = rawget(to,"__child"); local child = rawget(to,"__child");
@ -61,8 +57,7 @@ function module.init(shared)
end end
insert(child,this); insert(child,this);
end end
end return setmetatable({to = to,this = this},mount);
return setmetatable({to = to,this = items},mount);
end end
setmetatable(new,{ setmetatable(new,{