local function a(a,z,d)local m={}for a,z in pairs(a)do m[a]=z end m[z]=d return m end local function z(a,z)local d={}for a,m in pairs(a)do if a~=z then d[a]=m end end return d end local function d()local d={}local function m(m,Q)assert(typeof(Q)=="\102\117\110\099\116\105\111\110","\067\097\110 \111\110\108\121 \115\117\098\115\099\114\105\098\101 \116\111 \115\105\103\110\097\108\115 \119\105\116\104 \097 \102\117\110\099\116\105\111\110\046")local E={callback=Q}d=a(d,Q,E)local function W()assert(not E.disconnected,"\076\105\115\116\101\110\101\114\115 \099\097\110 \111\110\108\121 \098\101 \100\105\115\099\111\110\110\101\099\116\101\100 \111\110\099\101\046")E.disconnected=true d=z(d,Q)end return W end local function Q(a,...)for a,z in pairs(d)do if not z.disconnected then a(...)end end end return{subscribe=m;fire=Q}end return d


local function addToMap(map, addKey, addValue)
	local new = {}

	for key, value in pairs(map) do
		new[key] = value
	end

	new[addKey] = addValue

	return new
end

local function removeFromMap(map, removeKey)
	local new = {}

	for key, value in pairs(map) do
		if key ~= removeKey then
			new[key] = value
		end
	end

	return new
end

local function createSignal()
	local connections = {}

	local function subscribe(_, callback)
		assert(typeof(callback) == "function", "Can only subscribe to signals with a function.")

		local connection = {
			callback = callback,
		}

		connections = addToMap(connections, callback, connection)

		local function disconnect()
			assert(not connection.disconnected, "Listeners can only be disconnected once.")

			connection.disconnected = true
			connections = removeFromMap(connections, callback)
		end

		return disconnect
	end

	local function fire(_, ...)
		for callback, connection in pairs(connections) do
			if not connection.disconnected then
				callback(...)
			end
		end
	end

	return {
		subscribe = subscribe,
		fire = fire,
	}
end

return createSignal
