Tocuto

Lua Asyncio (another example)

Apr 26th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.90 KB | None | 0 0
  1. local await = coroutine.yield
  2. local loop = Loop()
  3. loop.max_runtime = 30 -- You can play a bit with these values
  4. loop.iteration_cooldown = 4000 -- This is the best one when you need to spam a bunch of tasks that are not that heavy!
  5. loop.accept_faster = true -- It runs it all as fast as it cans
  6. -- These tasks are very soft so this configuration will be one of the bests for it!
  7.  
  8. local is_online = {}
  9.  
  10. function eventPlayerDataLoaded(player, data)
  11.     is_online[player] = true
  12. end
  13.  
  14. function isOnline(player, handler)
  15.     system.loadPlayerData(player)
  16.     is_online[player] = false
  17.  
  18.     loop.sleep() -- waits for the next iteration
  19.  
  20.     handler(is_online[player])
  21. end
  22.  
  23. function eventChatCommand(player, command)
  24.     loop.add_task(Task(isOnline, command, function(is_online)
  25.         tfm.exec.chatMessage(command .. " is o" .. (is_online and "n" or "ff") .. "line!")
  26.     end))
  27. end
  28.  
  29. function eventLoop()
  30.     loop.run_tasks()
  31. end
Add Comment
Please, Sign In to add comment