]> git.lizzy.rs Git - dragonblocks3d-lua.git/blob - src/timeout.lua
Dynamic World System
[dragonblocks3d-lua.git] / src / timeout.lua
1 local timeout = Dragonblocks.create_class()
2
3 timeout.list = {}
4
5 function timeout:constructor(sec, func, ...)
6         self.exp, self.func, self.args = socket.gettime() + sec, func, table.pack(...)
7         table.insert(timeout.list, self)
8 end
9
10 function timeout:clear()
11         self.cleared = true
12 end
13
14 function Dragonblocks.set_timeout(sec, func, ...)
15         return timeout(sec, func, ...)
16 end
17
18 function Dragonblocks:clear_timeout()
19         self:clear()
20 end
21
22 Dragonblocks:add_task(function()
23         while true do
24                 local tolist = timeout.list
25                 local tm = socket.gettime()
26                 timeout.list = {}
27                 for _, to in pairs(tolist) do
28                         if not to.cleared then
29                                 if to.exp <= tm then
30                                         to.func(table.unpack(to.args))
31                                 else
32                                         table.insert(timeout.list, to)
33                                 end
34                         end
35                 end
36                 coroutine.yield()
37         end
38 end)