local B=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local o=require((B:WaitForChild("\114\098\120\116\115\095\105\110\099\108\117\100\101")):WaitForChild("\082\117\110\116\105\109\101\076\105\098"))local Z=o.Promise local function T(...)return{...}end local function z(B)if type(B)=="\102\117\110\099\116\105\111\110"then B()elseif type(B)=="\116\097\098\108\101"then if type(B.Disconnect)=="\102\117\110\099\116\105\111\110"then B:Disconnect()elseif type(B.disconnect)=="\102\117\110\099\116\105\111\110"then B:disconnect()end elseif typeof(B)=="\082\066\088\083\099\114\105\112\116\067\111\110\110\101\099\116\105\111\110"then B:Disconnect()end end local j={Disconnect=function() end}local function c(B,o)local Z=B.Connect if Z then assert(type(Z)=="\102\117\110\099\116\105\111\110","\110\111\116 \097 \102\117\110\099\116\105\111\110")return Z(B,o)end local T=B.connect if T then assert(type(T)=="\102\117\110\099\116\105\111\110","\110\111\116 \097 \102\117\110\099\116\105\111\110")return T(B,o)end return j end local function E(B,o)o=o or function()return true end return Z._new(debug.traceback(nil,2),function(Z,T,j)local E local V=false local function P()z(E)E=nil end E=c(B,function(...)local B=o(...)if B==true then Z(...)if E then P()else V=true end elseif type(B)~="\098\111\111\108\101\097\110"then error("\080\114\111\109\105\115\101\046\102\114\111\109\069\118\101\110\116 \112\114\101\100\105\099\097\116\101 \115\104\111\117\108\100 \097\108\119\097\121\115 \114\101\116\117\114\110 \097 \098\111\111\108\101\097\110")end end)if V and E then P()return end j(P)end)end return function(...)return(E(...)):andThen(T)end
--!nonstrict
--!optimize 2

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TS = require(ReplicatedStorage:WaitForChild("rbxts_include"):WaitForChild("RuntimeLib"))
local Promise = TS.Promise

local function Pack(...)
	return {...}
end

local function disconnectIt(connection)
	if type(connection) == "function" then
		connection()
	elseif type(connection) == "table" then
		if type(connection.Disconnect) == "function" then
			connection:Disconnect()
		elseif type(connection.disconnect) == "function" then
			connection:disconnect()
		end
	elseif typeof(connection) == "RBXScriptConnection" then
		connection:Disconnect()
	end
end

local BASIC_CONNECTION = {
	Disconnect = function() end;
}

local function connectIt(event, callback)
	local pascalConnect = event.Connect
	if pascalConnect then
		assert(type(pascalConnect) == "function", "not a function")
		return pascalConnect(event, callback)
	end

	local camelConnect = event.connect
	if camelConnect then
		assert(type(camelConnect) == "function", "not a function")
		return camelConnect(event, callback)
	end

	return BASIC_CONNECTION
end

local function fromEvent(event, predicate)
	predicate = predicate or function()
		return true
	end

	return Promise._new(debug.traceback(nil, 2), function(resolve, _, onCancel)
		local connection
		local shouldDisconnect = false

		local function disconnect()
			disconnectIt(connection)
			connection = nil
		end

		-- We use shouldDisconnect because if the callback given to Connect is called before
		-- Connect returns, connection will still be nil. This happens with events that queue up
		-- events when there's nothing connected, such as RemoteEvents

		connection = connectIt(event, function(...)
			local callbackValue = predicate(...)

			if callbackValue == true then
				resolve(...)

				if connection then
					disconnect()
				else
					shouldDisconnect = true
				end
			elseif type(callbackValue) ~= "boolean" then
				error("Promise.fromEvent predicate should always return a boolean")
			end
		end)

		if shouldDisconnect and connection then
			disconnect()
			return
		end

		onCancel(disconnect)
	end)
end

return function(...)
	return fromEvent(...):andThen(Pack)
end
