forked from nvim-lua/plenary.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync.lua
More file actions
150 lines (130 loc) · 3.3 KB
/
async.lua
File metadata and controls
150 lines (130 loc) · 3.3 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
local co = coroutine
local uv = vim.loop
local thread_loop = function(thread, callback)
local idle = uv.new_idle()
idle:start(function()
local success = co.resume(thread)
assert(success, "Coroutine failed")
if co.status(thread) == "dead" then
idle:stop()
callback()
end
end)
end
-- use with wrap
local pong = function(func, callback)
assert(type(func) == "function", "type error :: expected func")
local thread = co.create(func)
local step
step = function(...)
local res = {co.resume(thread, ...)}
local stat = res[1]
local ret = {select(2, unpack(res))}
assert(stat, "Status should be true")
if co.status(thread) == "dead" then
(callback or function() end)(unpack(ret))
else
assert(#ret == 1, "expected a single return value")
assert(type(ret[1]) == "function", "type error :: expected func")
ret[1](step)
end
end
step()
end
-- use with pong, creates thunk factory
local wrap = function(func)
assert(type(func) == "function", "type error :: expected func, got " .. type(func))
return function(...)
local params = {...}
return function(step)
table.insert(params, step)
return func(unpack(params))
end
end
end
local thread_loop_async = wrap(thread_loop)
-- many thunks -> single thunk
local join = function(thunks)
local len = #thunks
local done = 0
local acc = {}
local thunk = function(step)
if len == 0 then
return step()
end
for i, tk in ipairs(thunks) do
assert(type(tk) == "function", "thunk must be function")
local callback = function(...)
acc[i] = {...}
done = done + 1
if done == len then
step(unpack(acc))
end
end
tk(callback)
end
end
return thunk
end
local function run(future)
future()
end
local function run_all(futures)
for _, future in ipairs(futures) do
future()
end
end
-- sugar over coroutine
local await = function(defer)
assert(type(defer) == "function", "type error :: expected func")
return co.yield(defer)
end
local await_all = function(defer)
assert(type(defer) == "table", "type error :: expected table")
return co.yield(join(defer))
end
local async = function(func)
return function(...)
local args = {...}
return wrap(pong)(function()
return func(unpack(args))
end)
end
end
local pong_loop = async(function(func, callback)
assert(type(func) == "function", "type error :: expected func")
local thread = co.create(func)
local _step
_step = function(...)
local res = {co.resume(thread, ...)}
local stat = res[1]
local ret = {select(2, unpack(res))}
assert(stat, "Status should be true")
if co.status(thread) == "dead" then
(callback or function() end)(unpack(ret))
else
assert(#ret == 1, "expected a single return value")
assert(type(ret[1]) == "function", "type error :: expected func")
-- yield before calling the next one
co.yield()
ret[1](_step)
end
end
local step = function()
thread_loop(co.create(_step))
end
step()
end)
--- because idle is a bad name
local spawn = wrap(pong_loop)
return {
async = async,
join = join,
await = await,
await_all = await_all,
run = run,
run_all = run_all,
spawn = spawn,
wrap = wrap,
wait_for_textlock = wrap(vim.schedule)
}