local c={}c.__index=c function c.new(O,v)return setmetatable({Connected=true;_signal=O;_fn=v;_next=false},c)end function c.Disconnect(c)if not c.Connected then return end c.Connected=false if c._signal._handlerListHead==c then c._signal._handlerListHead=c._next else local O=c._signal._handlerListHead while O and O._next~=c do O=O._next end if O then O._next=c._next end end end c.Destroy=c.Disconnect

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
