local K=require(script.Parent.Object)local M=require(script.Parent.t)local D=K:Extend("\077\097\105\100",{mutators=true})local function b(K)local M=typeof(K)local D=M=="\082\066\088\083\099\114\105\112\116\067\111\110\110\101\099\116\105\111\110"or M=="\102\117\110\099\116\105\111\110"or M=="\116\097\098\108\101"and K.Destroy return D,"\069\120\112\101\099\116\101\100 \102\117\110\099\116\105\111\110\044 \082\066\088\083\099\114\105\112\116\067\111\110\110\101\099\116\105\111\110 \111\114 \116\097\098\108\101 \119\105\116\104 \068\101\115\116\114\111\121"end function D.constructor(K)K._tasks={}end local a=M.tuple(M.string,b)function D.Add(K,...)if a(...)then local M,D=...K._tasks[M]=D else local M=...assert(b(M))table.insert(K._tasks,M)end end function D.get(M,K)return M._tasks[K]end function D.set(D,K,M)D:Add(K,M)end function D.Clean(K)local M=K._tasks for K,D in pairs(M)do if typeof(D)=="\082\066\088\083\099\114\105\112\116\067\111\110\110\101\099\116\105\111\110"then M[K]=nil D:Disconnect()end end local D,b=next(M)while b~=nil do M[D]=nil if type(b)=="\102\117\110\099\116\105\111\110"then b()elseif typeof(b)=="\082\066\088\083\099\114\105\112\116\067\111\110\110\101\099\116\105\111\110"then b:Disconnect()elseif b.Destroy then b:Destroy()end D,b=next(M)end end return D
local Object = require(script.Parent.Object)
local t = require(script.Parent.t)
local Maid = Object:Extend("Maid", {mutators = true})

local function t_task(value)
	local _type = typeof(value)
	local isValid = _type == "RBXScriptConnection" or _type == "function" or (_type == "table" and value.Destroy)

	return isValid, "Expected function, RBXScriptConnection or table with Destroy"
end

function Maid:constructor()
	self._tasks = {}
end

local isAddId = t.tuple(t.string, t_task)
function Maid:Add(...)
	if (isAddId(...)) then
		local id, task = ...
		self._tasks[id] = task
	else
		local task = ...
		assert(t_task(task))
		table.insert(self._tasks, task)
	end
end

function Maid:get(index)
	return self._tasks[index]
end

function Maid:set(index, value)
	self:Add(index, value)
end

function Maid:Clean()
	local tasks = self._tasks

	-- Disconnect all events first as we know this is safe
	-- luacov: disable
	-- because Lemur doesn't do RBXScriptConnections properly.
	for index, task in pairs(tasks) do
		if typeof(task) == "RBXScriptConnection" then
			tasks[index] = nil
			task:Disconnect()
		end
	end
	-- luacov: enable

	local index, task = next(tasks)
	while task ~= nil do
		tasks[index] = nil
		if type(task) == "function" then
			task()
	-- luacov: disable
	-- because Lemur doesn't do RBXScriptConnections properly.
		elseif typeof(task) == "RBXScriptConnection" then
			task:Disconnect()
	-- luacov: enable
		elseif task.Destroy then
			task:Destroy()
		end
		index, task = next(tasks)
	end
end

return Maid
