aboutsummaryrefslogtreecommitdiff
path: root/timeouts.lua
blob: 7b7e71a4e8a7030a39e38814555f12bcdb78405c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
local unpack = unpack or table.unpack
lua_async.timeouts = {
	pool = {},
	executing = {},
	last_id = 0,
}

function setTimeout(callback, ms, ...)
	local id = lua_async.timeouts.last_id + 1
	lua_async.timeouts.last_id = id
	lua_async.timeouts.pool[id] = {
		time_left = (ms or 0) / 1000,
		callback = callback,
		args = {...},
	}
	return id
end

function clearTimeout(id)
	lua_async.timeouts.pool[id] = nil
	lua_async.timeouts.executing[id] = nil
end

function lua_async.timeouts.step(dtime)
	lua_async.timeouts.executing = lua_async.timeouts.pool
	lua_async.timeouts.pool = {}

	for id, timeout in pairs(lua_async.timeouts.executing) do
		timeout.time_left = timeout.time_left - dtime

		if timeout.time_left <= 0 then
			timeout.callback(unpack(timeout.args))
		else
			lua_async.timeouts.pool[id] = timeout
		end

		lua_async.timeouts.executing[id] = nil
	end
end