File tree 3 files changed +35
-11
lines changed
3 files changed +35
-11
lines changed Original file line number Diff line number Diff line change @@ -9,10 +9,14 @@ local M = {}
9
9
M ._queue = {}
10
10
M ._executor = assert (vim .loop .new_check ())
11
11
M ._running = false
12
+ M .SLEEP = " sleep"
13
+ --- @type Async
14
+ M .current = nil
12
15
13
16
--- @class Async
14
17
--- @field co thread
15
18
--- @field opts AsyncOpts
19
+ --- @field sleeping ? boolean
16
20
local Async = {}
17
21
18
22
--- @param fn async fun ()
@@ -29,16 +33,38 @@ function Async:running()
29
33
return coroutine.status (self .co ) ~= " dead"
30
34
end
31
35
36
+ function Async :sleep (ms )
37
+ self .sleeping = true
38
+ vim .defer_fn (function ()
39
+ self .sleeping = false
40
+ end , ms )
41
+ end
42
+
43
+ function Async :suspend ()
44
+ self .sleeping = true
45
+ end
46
+
47
+ function Async :resume ()
48
+ self .sleeping = false
49
+ end
50
+
32
51
function Async :step ()
52
+ if self .sleeping then
53
+ return true
54
+ end
33
55
local status = coroutine.status (self .co )
34
56
if status == " suspended" then
57
+ M .current = self
35
58
local ok , res = coroutine.resume (self .co )
59
+ M .current = nil
36
60
if not ok then
37
61
if self .opts .on_error then
38
62
self .opts .on_error (tostring (res ))
39
63
end
40
64
elseif res then
41
- if self .opts .on_yield then
65
+ if res == M .SLEEP then
66
+ self .sleeping = true
67
+ elseif self .opts .on_yield then
42
68
self .opts .on_yield (res )
43
69
end
44
70
end
Original file line number Diff line number Diff line change @@ -222,6 +222,8 @@ function Task:spawn(cmd, opts)
222
222
end
223
223
end
224
224
225
+ self ._running :suspend ()
226
+
225
227
local running = true
226
228
local ret = true
227
229
--- @param output string
@@ -234,6 +236,7 @@ function Task:spawn(cmd, opts)
234
236
end
235
237
ret = ok
236
238
running = false
239
+ self ._running :resume ()
237
240
end
238
241
239
242
if headless then
@@ -244,9 +247,8 @@ function Task:spawn(cmd, opts)
244
247
end
245
248
end
246
249
Process .spawn (cmd , opts )
247
- while running do
248
- coroutine.yield ()
249
- end
250
+ coroutine.yield ()
251
+ assert (not running , " process still running?" )
250
252
return ret
251
253
end
252
254
Original file line number Diff line number Diff line change 232
232
--- @async
233
233
--- @param ms number
234
234
function M .sleep (ms )
235
- local continue = false
236
- vim .defer_fn (function ()
237
- continue = true
238
- end , ms )
239
- while not continue do
240
- coroutine.yield ()
241
- end
235
+ local async = require (" lazy.async" ).current
236
+ assert (async , " Not in an async context" )
237
+ async :sleep (ms )
242
238
end
243
239
244
240
function M ._dump (value , result )
You can’t perform that action at this time.
0 commit comments