4
4
5
5
-- Store all the async threads in a weak table so we don't prevent them from
6
6
-- being garbage collected
7
- local handles = setmetatable ({}, { __mode = ' k ' })
7
+ local handles = setmetatable ({}, { __mode = " k " })
8
8
9
9
local M = {}
10
10
@@ -28,10 +28,12 @@ function M.running()
28
28
end
29
29
30
30
local function is_Async_T (handle )
31
- if handle
32
- and type (handle ) == ' table'
31
+ if
32
+ handle
33
+ and type (handle ) == " table"
33
34
and vim .is_callable (handle .cancel )
34
- and vim .is_callable (handle .is_cancelled ) then
35
+ and vim .is_callable (handle .is_cancelled )
36
+ then
35
37
return true
36
38
end
37
39
end
63
65
--- @tparam any ... Arguments for func
64
66
--- @treturn async_t Handle
65
67
function M .run (func , callback , ...)
66
- vim .validate {
67
- func = { func , ' function' },
68
- callback = { callback , ' function' , true }
69
- }
68
+ vim .validate ( {
69
+ func = { func , " function" },
70
+ callback = { callback , " function" , true },
71
+ })
70
72
71
73
local co = coroutine.create (func )
72
74
local handle = Async_T .new (co )
73
75
74
76
local function step (...)
75
- local ret = {coroutine.resume (co , ... )}
77
+ local ret = { coroutine.resume (co , ... ) }
76
78
local ok = ret [1 ]
77
79
78
80
if not ok then
79
81
local err = ret [2 ]
80
- error (string.format (" The coroutine failed with this message:\n %s\n %s" ,
81
- err , debug.traceback (co )))
82
+ error (
83
+ string.format (" The coroutine failed with this message:\n %s\n %s" , err , debug.traceback (co ))
84
+ )
82
85
end
83
86
84
- if coroutine.status (co ) == ' dead' then
87
+ if coroutine.status (co ) == " dead" then
85
88
if callback then
86
89
callback (unpack (ret , 4 , table.maxn (ret )))
87
90
end
88
91
return
89
92
end
90
93
91
94
local nargs , fn = ret [2 ], ret [3 ]
92
- local args = {select (4 , unpack (ret ))}
95
+ local args = { select (4 , unpack (ret )) }
93
96
94
- assert (type (fn ) == ' function' , " type error :: expected func" )
97
+ assert (type (fn ) == " function" , " type error :: expected func" )
95
98
96
99
args [nargs ] = step
97
100
@@ -106,10 +109,10 @@ function M.run(func, callback, ...)
106
109
end
107
110
108
111
local function wait (argc , func , ...)
109
- vim .validate {
110
- argc = { argc , ' number' },
111
- func = { func , ' function' },
112
- }
112
+ vim .validate ( {
113
+ argc = { argc , " number" },
114
+ func = { func , " function" },
115
+ })
113
116
114
117
-- Always run the wrapped functions in xpcall and re-raise the error in the
115
118
-- coroutine. This makes pcall work as normal.
@@ -124,7 +127,7 @@ local function wait(argc, func, ...)
124
127
end , unpack (args , 1 , argc ))
125
128
end
126
129
127
- local ret = {coroutine.yield (argc , pfunc , ... )}
130
+ local ret = { coroutine.yield (argc , pfunc , ... ) }
128
131
129
132
local ok = ret [1 ]
130
133
if not ok then
@@ -141,12 +144,12 @@ end
141
144
--- @tparam function func callback style function to execute
142
145
--- @tparam any ... Arguments for func
143
146
function M .wait (...)
144
- if type (select (1 , ... )) == ' number' then
147
+ if type (select (1 , ... )) == " number" then
145
148
return wait (... )
146
149
end
147
150
148
151
-- Assume argc is equal to the number of passed arguments.
149
- return wait (select (' # ' , ... ) - 1 , ... )
152
+ return wait (select (" # " , ... ) - 1 , ... )
150
153
end
151
154
152
155
--- Use this to create a function which executes in an async context but
@@ -157,20 +160,20 @@ end
157
160
--- @tparam boolean strict Error when called in non-async context
158
161
--- @treturn function(...):async_t
159
162
function M .create (func , argc , strict )
160
- vim .validate {
161
- func = { func , ' function' },
162
- argc = { argc , ' number' , true }
163
- }
163
+ vim .validate ( {
164
+ func = { func , " function" },
165
+ argc = { argc , " number" , true },
166
+ })
164
167
argc = argc or 0
165
168
return function (...)
166
169
if M .running () then
167
170
if strict then
168
- error (' This function must run in a non-async context' )
171
+ error (" This function must run in a non-async context" )
169
172
end
170
173
return func (... )
171
174
end
172
175
local callback = select (argc + 1 , ... )
173
- return M .run (func , callback , unpack ({... }, 1 , argc ))
176
+ return M .run (func , callback , unpack ({ ... }, 1 , argc ))
174
177
end
175
178
end
176
179
@@ -179,11 +182,11 @@ end
179
182
--- @tparam function func
180
183
--- @tparam boolean strict Error when called in non-async context
181
184
function M .void (func , strict )
182
- vim .validate { func = { func , ' function' } }
185
+ vim .validate ( { func = { func , " function" } })
183
186
return function (...)
184
187
if M .running () then
185
188
if strict then
186
- error (' This function must run in a non-async context' )
189
+ error (" This function must run in a non-async context" )
187
190
end
188
191
return func (... )
189
192
end
@@ -198,13 +201,13 @@ end
198
201
--- @tparam boolean strict Error when called in non-async context
199
202
--- @treturn function Returns an async function
200
203
function M .wrap (func , argc , strict )
201
- vim .validate {
202
- argc = { argc , ' number' },
203
- }
204
+ vim .validate ( {
205
+ argc = { argc , " number" },
206
+ })
204
207
return function (...)
205
208
if not M .running () then
206
209
if strict then
207
- error (' This function must run in an async context' )
210
+ error (" This function must run in an async context" )
208
211
end
209
212
return func (... )
210
213
end
@@ -229,7 +232,7 @@ function M.join(thunks, n, interrupt_check)
229
232
local ret = {}
230
233
231
234
local function cb (...)
232
- ret [# ret + 1 ] = {... }
235
+ ret [# ret + 1 ] = { ... }
233
236
to_go = to_go - 1
234
237
if to_go == 0 then
235
238
finish (ret )
@@ -256,11 +259,11 @@ end
256
259
--- @tparam function fn
257
260
--- @param ... arguments to apply to ` fn`
258
261
function M .curry (fn , ...)
259
- local args = {... }
260
- local nargs = select (' # ' , ... )
262
+ local args = { ... }
263
+ local nargs = select (" # " , ... )
261
264
return function (...)
262
- local other = {... }
263
- for i = 1 , select (' # ' , ... ) do
265
+ local other = { ... }
266
+ for i = 1 , select (" # " , ... ) do
264
267
args [nargs + i ] = other [i ]
265
268
end
266
269
fn (unpack (args ))
0 commit comments