View Single Post
09-13-19, 04:38 AM   #1
Odjur
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 7
Is there any way to throttle an arbitrary function?

I am currently writing my functions to run for N iterations before calling them again, but it would be much more convenient and consistent to be able to pass my functions off to a handler/scheduler and have them take care of them for me. This is an example of what I am trying to do:

Code:
local thread
local delay
local count = 0

local queue = function()
	for a = 1, 10000000 do
		count = a
	end
end

C_Timer.NewTicker(0, function()
	if queue then
		if not thread then
			thread = coroutine.create(function()
				delay = C_Timer.NewTicker(0, function()
					print(count)
					
					if coroutine.status(thread) == "dead" then
						queue = nil
						thread = nil
						delay:Cancel()
					else
						coroutine.yield()
					end
				end)
				
				queue()
			end)
		end
		
		coroutine.resume(thread)
	end
end)
I was trying to pass my functions to the variable 'queue' and have them turned into coroutines that would be yielded every frame. However it seems that C_Timer.NewTicker doesn't run asynchronously, so it never gets to yield the coroutine.

Is there any way to do what I am trying to do? It seems strange to me that I can't come up with a clean way to throttle my functions. Thanks for your help.
  Reply With Quote