diff --git a/deep.lua b/deep.lua index ffd545c..89f6a3d 100644 --- a/deep.lua +++ b/deep.lua @@ -26,8 +26,7 @@ local deep = { } local execQueue = {} -local maxIndex = 1 -local minIndex = 1 +local execKeys = {} -- for compatibility with Lua 5.1/5.2 local unpack = rawget(table, "unpack") or unpack @@ -44,13 +43,7 @@ deep.queue = function(i, fun, ...) end local arg = { ... } - - if i < minIndex then - minIndex = i - elseif i > maxIndex then - maxIndex = i - end - + if arg and #arg > 0 then local t = function() return fun(unpack(arg)) end @@ -66,18 +59,23 @@ deep.queue = function(i, fun, ...) table.insert(execQueue[i], fun) end end + + table.insert(execKeys, i) end deep.execute = function() - for i = minIndex, maxIndex do - if execQueue[i] then - for _, fun in pairs(execQueue[i]) do - fun() + if next(execQueue) ~= nil then + table.sort(execKeys) + + for k = 1, #execKeys do + for i = 1, #execQueue[execKeys[k]] do + execQueue[execKeys[k]][i]() end end + + execQueue = {} + execKeys = {} end - - execQueue = {} end return deep