local O=require(script.Parent.runTests)local z={}local b={}function b.addInstance(i)if z[i]then return end local X,y,q if i:IsA("\076\117\097\083\111\117\114\099\101\067\111\110\116\097\105\110\101\114")then local z=i:GetPropertyChangedSignal("\083\111\117\114\099\101")X=z:Connect(function()O()end)end local M=i:GetChildren()for O,z in ipairs(M)do b.addInstance(z)end y=i.ChildAdded:Connect(b.addInstance)q=i.AncestryChanged:Connect(function(O,z)if not z then b.removeInstance(i)end end)z[i]={X,y;q}O()end function b.removeInstance(O)local b=z[O]for O,z in pairs(b)do z:Disconnect()end z[O]=nil end function b.removeAll()for O in pairs(z)do b.removeInstance(O)end z={}end return b
local runTests = require(script.Parent.runTests)

local connections = {}

local Watcher = {}

function Watcher.addInstance(instance)
    if connections[instance] then
        return
    end

    local sourceConnection, childAddedConnection, removedConnection

    if instance:IsA("LuaSourceContainer") then
        local sourceEvent = instance:GetPropertyChangedSignal("Source")

        sourceConnection =
            sourceEvent:Connect(
            function()
                runTests()
            end
        )
    end

    local children = instance:GetChildren()
    for _, child in ipairs(children) do
        Watcher.addInstance(child)
    end

    childAddedConnection = instance.ChildAdded:Connect(Watcher.addInstance)

    removedConnection =
        instance.AncestryChanged:Connect(
        function(_, newParent)
            if not newParent then
                Watcher.removeInstance(instance)
            end
        end
    )

    connections[instance] = {
        sourceConnection,
        childAddedConnection,
        removedConnection
    }

    runTests()
end

function Watcher.removeInstance(instance)
    local instanceConnections = connections[instance]

    for _, connection in pairs(instanceConnections) do
        connection:Disconnect()
    end

    connections[instance] = nil
end

function Watcher.removeAll()
    for instance in pairs(connections) do
        Watcher.removeInstance(instance)
    end

    connections = {}
end

return Watcher
