-- METHODS

function Signal:Fire(...)
	self._bindableEvent:Fire(...)
end

function Signal:Connect(handler)
	if not (type(handler) == "function") then
		error(("connect(%s)"):format(typeof(handler)), 2)
	end
	
	local connection = self._bindableEvent.Event:Connect(function(...)
		handler(...)
	end)
	
	-- If ``true`` is passed for trackConnectionsChanged within the constructor this will track the amount of active connections
	if self.connectionsChanged then
		self.totalConnections += 1
		self.connectionsChanged:Fire(1)
		local heartbeatConection
		heartbeatConection = game:GetService("RunService").Heartbeat:Connect(function()
			if connection.Connected == false then
				heartbeatConection:Disconnect()
				if self.connectionsChanged then
					self.totalConnections -= 1
					self.connectionsChanged:Fire(-1)
				end
			end
		end)
	end

	return connection
end

function Signal:Wait()
	local args = self._bindableEvent.Event:Wait()
	return unpack(args)
end

function Signal:Destroy()
	if self._bindableEvent then
		self._bindableEvent:Destroy()
		self._bindableEvent = nil
	end
	if self.connectionsChanged then
		self.connectionsChanged:Fire(-self.totalConnections)
		self.connectionsChanged:Destroy()
		self.connectionsChanged = nil
		self.totalConnections = 0
	end
end
Signal.destroy = Signal.Destroy
Signal.Disconnect = Signal.Destroy
Signal.disconnect = Signal.Destroy



return Signal
