local g={}g.__index=g local function r(g)return g:IsA("\077\111\100\117\108\101\083\099\114\105\112\116")end function g.new(r)assert(r)local m=setmetatable({},g)m.connections={}m.cache={}m.roots=r return m end function g.withSignal(r,g)r.signal=g return r end function g.findOldScripts(g)for m,H in pairs(g.roots)do for m,H in pairs(H:GetDescendants())do if r(H)then g:listenTo(H)end end end end function g.getModuleCache(g)return g.cache end function g.watchForNewScripts(g)for m,H in pairs(g.roots)do table.insert(g.connections,H.DescendantAdded:Connect(function(m)if r(m)then g:listenTo(m)end end))end end function g.listenTo(r,g)r.cache[g]=(g:GetPropertyChangedSignal("\083\111\117\114\099\101")):Connect(function()r.signal:Fire()end)end function g.start(g)g:findOldScripts()g:watchForNewScripts()end function g.destruct(g)for g,r in pairs(g.connections)do r:Disconnect()end g.connections={}end return g
local watchDatamodel = {}
watchDatamodel.__index = watchDatamodel

local function isInteresting(object)
	return object:IsA("ModuleScript")
end

function watchDatamodel.new(roots)
	assert(roots)
	local self = setmetatable({}, watchDatamodel)

	self.connections = {}
	self.cache = {}
	self.roots = roots

	return self
end

function watchDatamodel:withSignal(signal)
	self.signal = signal
	return self
end

function watchDatamodel:findOldScripts()
	for _, root in pairs(self.roots) do
		for _, object in pairs(root:GetDescendants()) do
			if isInteresting(object) then
				self:listenTo(object)
			end
		end
	end
end

function watchDatamodel:getModuleCache()
	return self.cache
end

function watchDatamodel:watchForNewScripts()
	for _, root in pairs(self.roots) do
		table.insert(self.connections, root.DescendantAdded:Connect(function(object)
			if isInteresting(object) then
				self:listenTo(object)
			end
		end))
	end
end

function watchDatamodel:listenTo(object)
	self.cache[object] = object:GetPropertyChangedSignal("Source"):Connect(function()
		self.signal:Fire()
	end)
end

function watchDatamodel:start()
	self:findOldScripts()
	self:watchForNewScripts()
end

function watchDatamodel:destruct()
	for _, connection in pairs(self.connections) do
		connection:Disconnect()
	end
	self.connections = {}
end

return watchDatamodel