-- ScriptConnection object:


local ScriptConnection = {
	--[[
		_listener = listener,
		_script_signal = script_signal,
		_disconnect_listener = disconnect_listener,
		_disconnect_param = disconnect_param,
		
		_next = next_script_connection,
		_is_connected = is_connected,
	--]]
}
ScriptConnection.__index = ScriptConnection

function ScriptConnection:Disconnect()

	if self._is_connected == false then
		return
	end

	self._is_connected = false
	self._script_signal._listener_count -= 1

	if self._script_signal._head == self then
		self._script_signal._head = self._next
	else
		local prev = self._script_signal._head
		while prev ~= nil and prev._next ~= self do
			prev = prev._next
		end
		if prev ~= nil then
			prev._next = self._next
		end
	end

	if self._disconnect_listener ~= nil then
		if not FreeRunnerThread then
			FreeRunnerThread = coroutine.create(RunEventHandlerInFreeThread)
		end
		task.spawn(FreeRunnerThread, self._disconnect_listener, self._disconnect_param)
		self._disconnect_listener = nil
	end

end
