Skip to content

Instantly share code, notes, and snippets.

@gallexme
Created January 11, 2021 15:55
Show Gist options
  • Save gallexme/e6ad3d0ffc2cac9d26bb6c38257c9e0d to your computer and use it in GitHub Desktop.
Save gallexme/e6ad3d0ffc2cac9d26bb6c38257c9e0d to your computer and use it in GitHub Desktop.
List = {}
function List.new ()
return {first = 0, last = -1}
end
function List.pushleft (list, value)
local first = list.first - 1
list.first = first
list[first] = value
end
function List.pushright (list, value)
local last = list.last + 1
list.last = last
list[last] = value
end
function List.popleft (list)
local first = list.first
if first > list.last then error("list is empty") end
local value = list[first]
list[first] = nil -- to allow garbage collection
list.first = first + 1
return value
end
function List.popright (list)
local last = list.last
if list.first > last then error("list is empty") end
local value = list[last]
list[last] = nil -- to allow garbage collection
list.last = last - 1
return value
end
function script.onStart()
script.taskRunner = Tasks.onStart()
end
function script.onFlush()
end
function script.onUpdate()
coroutine.resume(script.taskRunner)
end
function script.onActionStart(action)
end
function script.onActionStop(action)
end
function script.onActionLoop(action)
end
script.onStart()
local Tasks = {
runningTasks = List.new(),
}
function Tasks.run()
system.print("Task Run")
--debug.lua_sethook(coroutine.yield, 'line', 100)
while true do
local status, task = pcall(List.popright, Tasks.runningTasks)
if (status) then
coroutine.resume(task)
if (coroutine.status(task) ~= 'dead') then
List.pushleft(Tasks.runningTasks, task)
end
end
coroutine.yield()
end
end
function Tasks.schedule(task)
List.pushleft(Tasks.runningTasks, task)
system.print("NEW TASK SCHEDULED")
end
function Tasks.onStart()
system.print("Task on start")
Tasks.runnerTask = coroutine.create(Tasks.run)
coroutine.resume(Tasks.runnerTask)
return Tasks.runnerTask
end
return Tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment