local d local R={}R.__index=R function R.new(d,v)local b=setmetatable({_signal=d,_conn=v;Connected=true},R)return b end function R.Disconnect(d)if d._conn then d._conn:Disconnect()d._conn=nil end if not d._signal then return end d.Connected=false local R=d._signal._connections local v=table.find(R,d)if v then local d=#R R[v]=R[d]R[d]=nil end d._signal=nil end function R.IsConnected(d)if d._conn then return d._conn.Connected end return false end R.Destroy=R.Disconnect
--[[

	signal = Signal.new()

	signal:Fire(...)
	signal:Wait()
	signal:WaitPromise()
	signal:Destroy()
	signal:DisconnectAll()
	
	connection = signal:Connect(functionHandler)

	connection.Connected
	connection:Disconnect()
	connection:IsConnected()

--]]

local Promise

local Connection = {}
Connection.__index = Connection

function Connection.new(signal, connection)
	local self = setmetatable({
		_signal = signal,
		_conn = connection,
		Connected = true,
	}, Connection)
	return self
end

function Connection:Disconnect()
	if self._conn then
		self._conn:Disconnect()
		self._conn = nil
	end
	if not self._signal then
		return
	end
	self.Connected = false
	local connections = self._signal._connections
	local connectionIndex = table.find(connections, self)
	if connectionIndex then
		local n = #connections
		connections[connectionIndex] = connections[n]
		connections[n] = nil
	end
	self._signal = nil
end

function Connection:IsConnected()
	if self._conn then
		return self._conn.Connected
	end
	return false
end

Connection.Destroy = Connection.Disconnect
