File tree 1 file changed +10
-11
lines changed
1 file changed +10
-11
lines changed Original file line number Diff line number Diff line change @@ -2,8 +2,10 @@ local M = {}
2
2
3
3
--- @type Async[]
4
4
M ._queue = {}
5
- M ._executor = assert (vim .loop .new_check ())
6
- M ._running = false
5
+ M ._executor = assert (vim .loop .new_timer ())
6
+
7
+ M .TIMER = 10
8
+ M .BUDGET = 100
7
9
8
10
--- @type table<thread , Async>
9
11
M ._threads = setmetatable ({}, { __mode = " k" })
68
70
69
71
--- @async
70
72
function Async :sleep (ms )
71
- self ._suspended = true
72
73
vim .defer_fn (function ()
73
- self . _suspended = false
74
+ self : resume ()
74
75
end , ms )
75
- coroutine.yield ()
76
+ self : suspend ()
76
77
end
77
78
78
79
--- @async
@@ -120,12 +121,11 @@ function Async:step()
120
121
end
121
122
122
123
function M .step ()
123
- M ._running = true
124
- local budget = 1 * 1e6
125
- local start = vim .loop .hrtime ()
124
+ local budget = M .BUDGET * 1e6
125
+ local start = vim .uv .hrtime ()
126
126
local count = # M ._queue
127
127
local i = 0
128
- while # M ._queue > 0 and vim .loop .hrtime () - start < budget do
128
+ while # M ._queue > 0 and vim .uv .hrtime () - start < budget do
129
129
--- @type Async
130
130
local state = table.remove (M ._queue , 1 )
131
131
if state :step () then
@@ -136,7 +136,6 @@ function M.step()
136
136
break
137
137
end
138
138
end
139
- M ._running = false
140
139
if # M ._queue == 0 then
141
140
return M ._executor :stop ()
142
141
end
146
145
function M .add (async )
147
146
table.insert (M ._queue , async )
148
147
if not M ._executor :is_active () then
149
- M ._executor :start (vim .schedule_wrap (M .step ))
148
+ M ._executor :start (1 , M . TIMER , vim .schedule_wrap (M .step ))
150
149
end
151
150
return async
152
151
end
You can’t perform that action at this time.
0 commit comments