first commit
This commit is contained in:
54
lib/knife/convoke.lua
Normal file
54
lib/knife/convoke.lua
Normal file
@@ -0,0 +1,54 @@
|
||||
return function (routine)
|
||||
local routines = { routine }
|
||||
local routineIndex = 1
|
||||
local isFinished = false
|
||||
|
||||
local function execute ()
|
||||
local continueCount = 0
|
||||
local run
|
||||
|
||||
local function continue ()
|
||||
continueCount = continueCount + 1
|
||||
return function (...)
|
||||
continueCount = continueCount - 1
|
||||
if continueCount == 0 then
|
||||
return run(...)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function wait (...)
|
||||
return coroutine.yield(...)
|
||||
end
|
||||
|
||||
local r = coroutine.create(function ()
|
||||
isFinished = false
|
||||
while routineIndex <= #routines do
|
||||
routines[routineIndex](continue, wait)
|
||||
continueCount = 0
|
||||
routineIndex = routineIndex + 1
|
||||
end
|
||||
isFinished = true
|
||||
end)
|
||||
|
||||
run = function (...)
|
||||
return coroutine.resume(r, ...)
|
||||
end
|
||||
|
||||
run()
|
||||
end
|
||||
|
||||
local function appendOrExecute (routine)
|
||||
if routine then
|
||||
routines[#routines + 1] = routine
|
||||
if isFinished then
|
||||
execute()
|
||||
end
|
||||
return appendOrExecute
|
||||
else
|
||||
execute()
|
||||
end
|
||||
end
|
||||
|
||||
return appendOrExecute
|
||||
end
|
||||
Reference in New Issue
Block a user