local T={}local q local H={}local r=game:GetService("\082\117\110\083\101\114\118\105\099\101")function T.CreateScheduler(n,T)local s={}local W={}local a=true local F local b=nil local U=tick()r.RenderStepped:Wait()local function h()q=tick()for T=#H,1,-1 do H[T+1]=H[T]>=q-1 and H[T]or nil end H[1]=q end local function z()b=r.RenderStepped:Connect(h)while true do if a then break end local q=tick()-U>=1 and#H or#H/(tick()-U)if q>=T and tick()-H[1]<1/T then if#W>0 then W[1]()table.remove(W,1)else a=true break end else r.RenderStepped:Wait()end end b:Disconnect()b=nil end function s.Pause(T)F=true a=true end function s.Resume(T)if F then F=false a=false z()end end function s.Destroy(T)s:Pause()for T in pairs(s)do s[T]=nil end setmetatable(s,{__index=function()error("\065\116\116\101\109\112\116 \116\111 \117\115\101 \100\101\115\116\114\111\121\101\100 \115\099\104\101\100\117\108\101\114")end;__newindex=function()error("\065\116\116\101\109\112\116 \116\111 \117\115\101 \100\101\115\116\114\111\121\101\100 \115\099\104\101\100\117\108\101\114")end})end function s.QueueTask(q,T)W[#W+1]=T if a and not F then a=false z()end end return s end return T
-- Author: EchoReaper
-- Roblox Link: https://www.roblox.com/Task-Scheduler-item?id=348019935
-- Publically released January 25, 2016

-- Changes made from EchoReaper's version:
	-- GetCurrentFPS() method removed
	-- FPS is only tracked when the 'Loop' function is running for performance reasons
	-- Styled code in consistency with the rest of the AeroGameFramework codebase

--[[
	
	local scheduler = TaskScheduler:CreateScheduler(targetedMinimumFPS)
	
	scheduler:QueueTask(function)
	scheduler:Pause()
	scheduler:Resume()
	scheduler:Destroy()
	
--]]




local TaskScheduler = {}

local lastIteration
local frameUpdateTable = {}

local runService = game:GetService("RunService")

--[[
	param targetFps  Task scheduler won't run a task if it'd make the FPS drop below this amount
					 (WARNING) this only holds true if it is used properly. If you try to complete 10 union operations
					 at once in a single task then of course your FPS is going to drop -- queue the union operations
					 up one at a time so the task scheduler can do its job.
					
					
	returns scheduler
		method Pause      Pauses the scheduler so it won't run tasks. Tasks may still be added while the scheduler is
						  paused. They just won't be touched until it's resumed. Performance efficient -- disables 
						  execution loop entirely until scheduler is resumed.
		
		method Resume     Resumes the paused scheduler.
		
		method Destroy    Destroys the scheduler so it can't be used anymore.
		
		method QueueTask  Queues a task for automatic execution.
			param callback  function (task) to be run.
	
	Example usage:
	
	local scheduler = TaskScheduler:CreateScheduler(60)
	local totalOperations = 0
	local paused
	for i=1,100 do
		scheduler:QueueTask(function()
			local partA = Instance.new("Part", workspace)
			local partB = Instance.new("Part", workspace)
			plugin:Union({partA, partB}):Destroy()
			totalOperations = totalOperations + 1
			print("Times unioned:", totalOperations)
			if (totalOperations == 50) then
				scheduler:Pause()
				paused = true
			end
		end)
	end
	
	repeat wait() until paused
	wait(2)
	scheduler:Resume()
--]]


function TaskScheduler:CreateScheduler(targetFps)
	
	local scheduler = {}
	local queue = {}
	local sleeping = true
	local paused
	
	local updateFrameTableEvent = nil
	
	local start = tick()
	runService.RenderStepped:Wait()
	
	local function UpdateFrameTable()
		lastIteration = tick()
		for i = #frameUpdateTable,1,-1 do
			frameUpdateTable[i + 1] = ((frameUpdateTable[i] >= (lastIteration - 1)) and frameUpdateTable[i] or nil)
		end	
		frameUpdateTable[1] = lastIteration
	end

	local function Loop()
		updateFrameTableEvent = runService.RenderStepped:Connect(UpdateFrameTable)
		while (true) do
			if (sleeping) then break end
			local fps = (((tick() - start) >= 1 and #frameUpdateTable) or (#frameUpdateTable / (tick() - start)))
			if (fps >= targetFps and (tick() - frameUpdateTable[1]) < (1 / targetFps)) then
				if (#queue > 0) then
					queue[1]()
					table.remove(queue, 1)
				else
					sleeping = true
					break
				end
			else
				runService.RenderStepped:Wait()
			end
		end
		updateFrameTableEvent:Disconnect()
		updateFrameTableEvent = nil
	end

	function scheduler:Pause()
		paused = true
		sleeping = true
	end
	
	function scheduler:Resume()
		if (paused) then
			paused = false
			sleeping = false
			Loop()
		end
	end
	
	function scheduler:Destroy()
		scheduler:Pause()
		for i in pairs(scheduler) do
			scheduler[i] = nil
		end
		setmetatable(scheduler, {
			__index = function()
				error("Attempt to use destroyed scheduler")
			end;
			__newindex = function()
				error("Attempt to use destroyed scheduler")
			end;
		})
	end
	
	function scheduler:QueueTask(callback)
		queue[#queue + 1] = callback 
		if (sleeping and not paused) then
			sleeping = false
			Loop()
		end
	end
	
	return scheduler
	
end


return TaskScheduler