cohelper.lua 676 B

123456789101112131415161718192021222324252627
  1. --[[ A coroutine Helper T. Ellison, June 2019
  2. This version of couroutine helper demonstrates the use of corouting within
  3. NodeMCU execution to split structured Lua code into smaller tasks
  4. ]]
  5. --luacheck: read globals node
  6. local modname = ...
  7. local function taskYieldFactory(co)
  8. local post = node.task.post
  9. return function(nCBs) -- upval: co,post
  10. post(function () -- upval: co, nCBs
  11. coroutine.resume(co, nCBs or 0)
  12. end)
  13. return coroutine.yield() + 1
  14. end
  15. end
  16. return { exec = function(func, ...) -- upval: modname
  17. package.loaded[modname] = nil
  18. local co = coroutine.create(func)
  19. return coroutine.resume(co, taskYieldFactory(co), ... )
  20. end }