local function v(v,N,u)local j={}for v,N in pairs(v)do j[v]=N end j[N]=u return j end local function N(v,N)local u={}for v,j in pairs(v)do if v~=N then u[v]=j end end return u end local function u()local u={}local function j(j,a)assert(typeof(a)=="\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 l={callback=a}u=v(u,a,l)local function J()assert(not l.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")l.disconnected=true u=N(u,a)end return J end local function a(v,...)for v,N in pairs(u)do if not N.disconnected then v(...)end end end return{subscribe=j;fire=a}end return u
--[[
	This is a simple signal implementation that has a dead-simple API.

		local signal = createSignal()

		local disconnect = signal:subscribe(function(foo)
			print("Cool foo:", foo)
		end)

		signal:fire("something")

		disconnect()
]]

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
