local j={}j.__index=j function j.new(L,u)return setmetatable({_connected=true;_signal=L;_fn=u;_next=false},j)end function j.Disconnect(j)if not j._connected then return end j._connected=false if j._signal._handlerListHead==j then j._signal._handlerListHead=j._next else local L=j._signal._handlerListHead while L and L._next~=j do L=L._next end if L then L._next=j._next end end end j.Destroy=j.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
