Skip to content
This repository was archived by the owner on Jul 12, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/instances/BaseInstance.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ BaseInstance.properties.ChildAdded = InstanceProperty.readOnly({
getDefault = Signal.new
})

BaseInstance.properties.ChildRemoved = InstanceProperty.readOnly({
getDefault = Signal.new
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line looks double-indented!

})

BaseInstance.properties.Parent = InstanceProperty.normal({
set = function(self, key, value)
local instance = getmetatable(self).instance
Expand All @@ -74,6 +78,7 @@ BaseInstance.properties.Parent = InstanceProperty.normal({

if instance.properties.Parent ~= nil then
getmetatable(instance.properties.Parent).instance.children[self] = nil
instance.properties.Parent.ChildRemoved:Fire(self)
end

instance.properties.Parent = value
Expand Down
32 changes: 32 additions & 0 deletions lib/instances/BaseInstance_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -671,4 +671,36 @@ describe("instances.BaseInstance", function()
assert.spy(spy).was_called_with(child)
end)
end)

describe("ChildRemoved", function()
it("should fire when a child is destroyed", function()
local parent = BaseInstance:new()
local child = BaseInstance:new()

local spy = spy.new(function() end)
parent.ChildRemoved:Connect(spy)

child.Parent = parent
assert.spy(spy).was_not_called()

child:Destroy()
assert.spy(spy).was_called_with(child)
end)

it("should fire when a child's parent is set to nil", function()
local parent = BaseInstance:new()
local child = BaseInstance:new()

local spy = spy.new(function() end)
parent.ChildRemoved:Connect(spy)

child.Parent = parent

assert.spy(spy).was_not_called()

child.Parent = nil

assert.spy(spy).was_called_with(child)
end)
end)
end)