aboutsummaryrefslogtreecommitdiff
path: root/util.lua
blob: 7061c0619f379e189766f782ec57a1c9f63ab1eb (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
function lua_async.yield()
	await(Promise(function(resolve)
		setImmediate(resolve)
	end))
end

function lua_async.sleep(ms)
	await(Promise(function(resolve)
		setTimeout(resolve, ms)
	end))
end

function lua_async.kill_thread()
	coroutine.yield(true)
end

function lua_async.resume(co)
	local status, err = coroutine.resume(co)

	if coroutine.status(co) == "dead" or err then
		lua_async.limiting.unset_limit(co)
	end

	if not status then
		error("Error (in async function): " .. err)
	end
end

function lua_async.run()
	local last_time = os.clock()

	while true do
		local current_time = os.clock()
		local dtime = current_time - last_time
		last_time = current_time

		lua_async.step(dtime)
	end
end