@@ -8,11 +8,11 @@ local DEFAULT_ERROR = "Unkown error."
8
8
9
9
local M = {}
10
10
11
- --- @private
11
+ --- @package
12
12
--- @type { [Future] : boolean }
13
13
M ._watching = setmetatable ({}, { __mode = " k" })
14
14
15
- --- @private
15
+ --- @package
16
16
--- @type { [thread] : Future }
17
17
M ._handles = {}
18
18
65
65
66
66
--- @class Waitable : diffview.Object
67
67
local Waitable = oop .create_class (" Waitable" )
68
+ M .Waitable = Waitable
68
69
69
70
--- @abstract
70
71
--- @return any ... # Any values returned by the waitable
71
72
function Waitable :await () oop .abstract_stub () end
72
73
73
- M .Waitable = Waitable
74
+ --- Schedule a callback to be invoked when this waitable has settled.
75
+ --- @param callback function
76
+ function Waitable :finally (callback )
77
+ (M .void (function ()
78
+ local ret = tbl_pack (M .await (self ))
79
+ callback (tbl_unpack (ret ))
80
+ end ))()
81
+ end
74
82
75
83
--- @class Future : Waitable
76
84
--- @operator call : Future
77
- --- @field private thread thread
78
- --- @field private listeners Future[]
79
- --- @field private parent ? Future
80
- --- @field private func ? function
81
- --- @field private return_values ? any[]
82
- --- @field private err ? string
83
- --- @field private kind AsyncKind
84
- --- @field private started boolean
85
- --- @field private awaiting_cb boolean
86
- --- @field private done boolean
87
- --- @field private has_raised boolean # `true` if this future has raised an error.
85
+ --- @field package thread thread
86
+ --- @field package listeners Future[]
87
+ --- @field package parent ? Future
88
+ --- @field package func ? function
89
+ --- @field package return_values ? any[]
90
+ --- @field package err ? string
91
+ --- @field package kind AsyncKind
92
+ --- @field package started boolean
93
+ --- @field package awaiting_cb boolean
94
+ --- @field package done boolean
95
+ --- @field package has_raised boolean # `true` if this future has raised an error.
88
96
local Future = oop .create_class (" Future" , Waitable )
89
97
90
98
function Future :init (opt )
@@ -107,18 +115,18 @@ function Future:init(opt)
107
115
self .has_raised = false
108
116
end
109
117
110
- --- @private
118
+ --- @package
111
119
--- @return string
112
120
function Future :__tostring ()
113
121
return dstring (self .thread )
114
122
end
115
123
116
- --- @private
124
+ --- @package
117
125
function Future :destroy ()
118
126
M ._handles [self .thread ] = nil
119
127
end
120
128
121
- --- @private
129
+ --- @package
122
130
--- @param value boolean
123
131
function Future :set_done (value )
124
132
self .done = value
@@ -138,7 +146,7 @@ function Future:get_returned()
138
146
return unpack (self .return_values , 2 , table.maxn (self .return_values ))
139
147
end
140
148
141
- --- @private
149
+ --- @package
142
150
--- @param ... any
143
151
function Future :dprint (...)
144
152
if not DiffviewGlobal .logger then return end
@@ -149,7 +157,7 @@ function Future:dprint(...)
149
157
end
150
158
end
151
159
152
- --- @private
160
+ --- @package
153
161
--- @param ... any
154
162
function Future :dprintf (...)
155
163
self :dprint (fmt (... ))
@@ -165,21 +173,21 @@ function Future:unwatch()
165
173
M ._watching [self ] = nil
166
174
end
167
175
168
- --- @private
176
+ --- @package
169
177
--- @return boolean
170
178
function Future :is_watching ()
171
179
return not not M ._watching [self ]
172
180
end
173
181
174
- --- @private
182
+ --- @package
175
183
--- @param force ? boolean
176
184
function Future :raise (force )
177
185
if self .has_raised and not force then return end
178
186
self .has_raised = true
179
187
error (self .err )
180
188
end
181
189
182
- --- @private
190
+ --- @package
183
191
function Future :step (...)
184
192
self :dprint (" step" )
185
193
local ret = { coroutine.resume (self .thread , ... ) }
@@ -217,7 +225,7 @@ function Future:step(...)
217
225
end
218
226
end
219
227
220
- --- @private
228
+ --- @package
221
229
--- @param ok boolean
222
230
--- @param ... any
223
231
function Future :notify_all (ok , ...)
@@ -241,6 +249,7 @@ function Future:notify_all(ok, ...)
241
249
end
242
250
end
243
251
252
+ --- @override
244
253
--- @return any ... # Return values
245
254
function Future :await ()
246
255
if self .err then
@@ -302,7 +311,7 @@ function Future:await()
302
311
return self :get_returned ()
303
312
end
304
313
305
- --- @private
314
+ --- @package
306
315
--- @return any ...
307
316
function Future :toplevel_await ()
308
317
local ok , status
@@ -337,15 +346,13 @@ end
337
346
--- @field nparams ? integer
338
347
--- @field args any[]
339
348
340
- --- @private
349
+ --- @package
341
350
--- @param func function
342
351
--- @param opt async ._run.Opt
343
352
function M ._run (func , opt )
344
- --- @diagnostic disable : invisible
345
353
opt = opt or {}
346
354
347
355
local handle --- @type Future
348
- local wrapped_cb
349
356
local use_err_handler = not not current_thread ()
350
357
351
358
local function wrapped_func (...)
@@ -381,7 +388,7 @@ function M._run(func, opt)
381
388
if opt .kind == " callback" then
382
389
local cur_cb = opt .args [opt .nparams ]
383
390
384
- function wrapped_cb (...)
391
+ local function wrapped_cb (...)
385
392
handle :set_done (true )
386
393
handle .return_values = { true , ... }
387
394
if cur_cb then cur_cb (... ) end
@@ -405,7 +412,6 @@ function M._run(func, opt)
405
412
handle :step (tbl_unpack (opt .args ))
406
413
407
414
return handle
408
- --- @diagnostic enable : invisible
409
415
end
410
416
411
417
--- Create an async task for a function with no return values.
@@ -566,4 +572,6 @@ M.scheduler = M.wrap(function(fast_only, callback)
566
572
vim .schedule (callback )
567
573
end )
568
574
575
+ M .schedule_now = M .wrap (vim .schedule , 1 )
576
+
569
577
return M
0 commit comments