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,21 +7,20 @@ 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();
elseif typeThis == "table" then elseif typeThis == "table" then
pcall(function() pcall(function()
this.Parent = nil; this.Parent = nil;
end); end);
local destroyThis = this.Destroy; local destroyThis = this.Destroy;
if destroyThis then if destroyThis then
pcall(destroyThis,this); pcall(destroyThis,this);
end
--if sef.to when
-- todo for remove child on parent
end end
--if sef.to when
-- todo for remove child on parent
end end
end end
@ -30,39 +29,35 @@ function module.init(shared)
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(...); if type(this) == "table" then
for _,this in ipairs(items) do thisObject = this.__object;
local thisO = this; local parent = rawget(this,"__parent");
if type(this) == "table" then if type(parent) == "table" then
thisO = this.__object; local parentChild = rawget(parent,"__child");
local parent = rawget(this,"__parent"); if parentChild then
if type(parent) == "table" then for i,v in pairs(parentChild) do
local parentChild = rawget(parent,"__child"); if v == this then
if parentChild then parentChild[i] = nil;
for i,v in pairs(parentChild) do
if v == this then
parentChild[i] = nil;
end
end end
end end
end end
rawset(this,"__parent",to);
end
if thisO then
thisO.Parent = getHolder(to);
end
if type(to) == "table" then
local child = rawget(to,"__child");
if not child then
child = {};
rawset(to,"__child",child);
end
insert(child,this);
end end
rawset(this,"__parent",to);
end end
return setmetatable({to = to,this = items},mount); if thisObject then
thisObject.Parent = holder or getHolder(to);
end
if type(to) == "table" then
local child = rawget(to,"__child");
if not child then
child = {};
rawset(to,"__child",child);
end
insert(child,this);
end
return setmetatable({to = to,this = this},mount);
end end
setmetatable(new,{ setmetatable(new,{