local o={}o.__index=o function o.new(M,U)return setmetatable({Connected=true;_signal=M;_fn=U,_next=false},o)end function o.Disconnect(o)if not o.Connected then return end o.Connected=false if o._signal._handlerListHead==o then o._signal._handlerListHead=o._next else local M=o._signal._handlerListHead while M and M._next~=o do M=M._next end if M then M._next=o._next end end end o.Destroy=o.Disconnect
-- Connection class
local Connection = {}
Connection.__index = Connection

function Connection.new(signal, fn)
	return setmetatable({
		Connected = true,
		_signal = signal,
		_fn = fn,
		_next = false,
	}, Connection)
end

function Connection:Disconnect()
	if not self.Connected then
		return
	end
	self.Connected = false

	-- Unhook the node, but DON'T clear it. That way any fire calls that are
	-- currently sitting on this node will be able to iterate forwards off of
	-- it, but any subsequent fire calls will not hit it, and it will be GCed
	-- when no more fire calls are sitting on it.
	if self._signal._handlerListHead == self then
		self._signal._handlerListHead = self._next
	else
		local prev = self._signal._handlerListHead
		while prev and prev._next ~= self do
			prev = prev._next
		end
		if prev then
			prev._next = self._next
		end
	end
end

Connection.Destroy = Connection.Disconnect
