mirror of
https://github.com/Refactorio/RedMew.git
synced 2025-01-12 02:28:02 +02:00
added weights to tasks
This commit is contained in:
parent
6d0dfc297b
commit
5e65e4e970
@ -20,6 +20,7 @@ end
|
||||
global.callbacks = {}
|
||||
global.next_async_callback_time = -1
|
||||
global.task_queue = global.task_queue or Queue.new()
|
||||
global.total_task_weight = 0
|
||||
local function on_tick()
|
||||
local queue = global.task_queue
|
||||
for i = 1, get_task_per_tick() do
|
||||
@ -29,8 +30,10 @@ local function on_tick()
|
||||
if not success then
|
||||
log(result)
|
||||
Queue.pop(queue)
|
||||
global.total_task_weight = global.total_task_weight - task.weight
|
||||
elseif not result then
|
||||
Queue.pop(queue)
|
||||
global.total_task_weight = global.total_task_weight - task.weight
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -50,7 +53,7 @@ local function on_tick()
|
||||
end
|
||||
|
||||
function get_task_per_tick()
|
||||
local size = Queue.size(global.task_queue)
|
||||
local size = global.total_task_weight
|
||||
local apt = math.floor(math.log10(size + 1))
|
||||
if apt < 1 then
|
||||
return 1
|
||||
@ -74,9 +77,10 @@ function Task.set_timeout(sec, callback, params)
|
||||
end
|
||||
|
||||
|
||||
function Task.queue_task(func_name, params)
|
||||
local queue = global.task_queue
|
||||
Queue.push(queue, {func_name = func_name, params = params})
|
||||
function Task.queue_task(func_name, params, weight)
|
||||
weight = weight or 1
|
||||
global.total_task_weight = global.total_task_weight + weight
|
||||
Queue.push(global.task_queue, {func_name = func_name, params = params, weight = weight})
|
||||
end
|
||||
|
||||
Event.register(defines.events.on_tick, on_tick)
|
||||
|
Loading…
Reference in New Issue
Block a user