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