wifi-tally_Oostendam/nodemcu-firmware/lua_modules/cohelper/cohelper.lua
Eljakim Herrewijnen 50b5fc1824 Initial commit
2021-09-27 21:52:27 +02:00

28 lines
676 B
Lua

--[[ A coroutine Helper T. Ellison, June 2019
This version of couroutine helper demonstrates the use of corouting within
NodeMCU execution to split structured Lua code into smaller tasks
]]
--luacheck: read globals node
local modname = ...
local function taskYieldFactory(co)
local post = node.task.post
return function(nCBs) -- upval: co,post
post(function () -- upval: co, nCBs
coroutine.resume(co, nCBs or 0)
end)
return coroutine.yield() + 1
end
end
return { exec = function(func, ...) -- upval: modname
package.loaded[modname] = nil
local co = coroutine.create(func)
return coroutine.resume(co, taskYieldFactory(co), ... )
end }