-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcotool.lua
More file actions
81 lines (69 loc) · 1.51 KB
/
cotool.lua
File metadata and controls
81 lines (69 loc) · 1.51 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
local function wrap(fn, ...)
local co = coroutine.create(fn)
local ok, ret = coroutine.resume(co, ...)
if not ok then
return error(("%s\n%s\ninside coroutine %s started by"):format(
ret, debug.traceback(co), co)
)
end
local args = {...}
return function(...)
local ok, ret = coroutine.resume(co, ..., unpack(args))
if not ok then
return error(("%s\n%s\ninside coroutine %s resumed by"):format(
ret, debug.traceback(co), co)
)
end
return ret
end
end
local function fun(fn)
return function(...)
return wrap(fn, ...)
end
end
local function wait_frame()
return coroutine.yield(true)
end
local function wait_t(t)
while true do
local now = wait_frame()
if now >= t then
return now
end
end
end
local function sleep(t)
return wait_t(sys.now() + t)
end
local function until_t(t)
return function()
local now = wait_frame()
if now < t then
return now
end
end
end
local function from_to(starts, ends)
return function()
local now
while true do
now = wait_frame()
if now >= starts then
break
end
end
if now < ends then
return now
end
end
end
return {
wrap = wrap;
fun = fun;
wait_t = wait_t;
sleep = sleep;
wait_frame = wait_frame;
until_t = until_t;
from_to = from_to;
}