local W=(require(script.Parent.GlobalConfig)).get()local function R()local R={}local z={}local b=false local function w(w,I)if W.internalTypeChecks then if typeof(I)~="\102\117\110\099\116\105\111\110"then error("\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",2)end end local l={callback=I;disconnected=false}if b and not R[I]then z[I]=l end R[I]=l local function i()if l.disconnected then error("\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",2)end l.disconnected=true R[I]=nil z[I]=nil end return i end local function I(W,...)b=true for W,R in R do if not R.disconnected and not z[W]then W(...)end end b=false for W,R in z do z[W]=nil end end return{subscribe=w,fire=I}end return R
--[[
	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 config = require(script.Parent.GlobalConfig).get()

local function createSignal()
	local connections = {}
	local suspendedConnections = {}
	local firing = false

	local function subscribe(_, callback)
		if config.internalTypeChecks then
			if typeof(callback) ~= "function" then
				error("Can only subscribe to signals with a function.", 2)
			end
		end

		local connection = {
			callback = callback,
			disconnected = false,
		}

		-- If the callback is already registered, don't add to the suspendedConnection. Otherwise, this will disable
		-- the existing one.
		if firing and not connections[callback] then
			suspendedConnections[callback] = connection
		end

		connections[callback] = connection

		local function disconnect()
			if connection.disconnected then
				error("Listeners can only be disconnected once.", 2)
			end

			connection.disconnected = true
			connections[callback] = nil
			suspendedConnections[callback] = nil
		end

		return disconnect
	end

	local function fire(_, ...)
		firing = true
		for callback, connection in connections do
			if not connection.disconnected and not suspendedConnections[callback] then
				callback(...)
			end
		end

		firing = false

		for callback, _ in suspendedConnections do
			suspendedConnections[callback] = nil
		end
	end

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

return createSignal
