@@ -2,22 +2,26 @@ local a = require "plenary.async.async"
2
2
local Deque = require (" plenary.async.structs" ).Deque
3
3
local tbl = require " plenary.tbl"
4
4
5
+ --- @class PlenaryAsyncControl
5
6
local M = {}
6
7
8
+ --- @class PlenaryAsyncCondvar
9
+ --- @field handles (fun (): nil ) []
7
10
local Condvar = {}
8
11
Condvar .__index = Condvar
9
12
10
- --- @class Condvar
11
- --- @return Condvar
13
+ --- @return PlenaryAsyncCondvar
12
14
function Condvar .new ()
13
15
return setmetatable ({ handles = {} }, Condvar )
14
16
end
15
17
16
18
--- `blocks` the thread until a notification is received
19
+ --- @param self PlenaryAsyncCondvar
20
+ --- @param callback fun (): nil
17
21
Condvar .wait = a .wrap (function (self , callback )
18
22
-- not calling the callback will block the coroutine
19
23
table.insert (self .handles , callback )
20
- end , 2 )
24
+ end , 2 ) --[[ @as async fun(): nil ]]
21
25
22
26
--- notify everyone that is waiting on this Condvar
23
27
function Condvar :notify_all ()
52
56
53
57
M .Condvar = Condvar
54
58
59
+ --- @class PlenaryAsyncSemaphore
60
+ --- @field handles (fun ( permit : PlenaryAsyncPermit ): nil ) []
61
+ --- @field permits integer
55
62
local Semaphore = {}
56
63
Semaphore .__index = Semaphore
57
64
58
- --- @class Semaphore
59
- --- @param initial_permits number : the number of permits that it can give out
60
- --- @return Semaphore
65
+ --- @param initial_permits integer the number of permits that it can give out
66
+ --- @return PlenaryAsyncSemaphore
61
67
function Semaphore .new (initial_permits )
62
68
vim .validate {
63
69
initial_permits = {
79
85
--- permit:forget()
80
86
--- when a permit can be acquired returns it
81
87
--- call permit:forget() to forget the permit
88
+ --- @param self PlenaryAsyncSemaphore
89
+ --- @param callback fun ( permit : PlenaryAsyncPermit ): nil
82
90
Semaphore .acquire = a .wrap (function (self , callback )
83
91
if self .permits > 0 then
84
92
self .permits = self .permits - 1
@@ -87,8 +95,10 @@ Semaphore.acquire = a.wrap(function(self, callback)
87
95
return
88
96
end
89
97
98
+ --- @class PlenaryAsyncPermit
90
99
local permit = {}
91
100
101
+ --- @param self_permit PlenaryAsyncPermit
92
102
permit .forget = function (self_permit )
93
103
self .permits = self .permits + 1
94
104
@@ -99,16 +109,17 @@ Semaphore.acquire = a.wrap(function(self, callback)
99
109
end
100
110
101
111
callback (permit )
102
- end , 2 )
112
+ end , 2 ) --[[ @as async fun(self: PlenaryAsyncSemaphore): PlenaryAsyncPermit ]]
103
113
104
114
M .Semaphore = Semaphore
105
115
116
+ --- @class PlenaryAsyncControlChannel
106
117
M .channel = {}
107
118
108
119
--- Creates a oneshot channel
109
120
--- returns a sender and receiver function
110
121
--- the sender is not async while the receiver is
111
- --- @return function , function
122
+ --- @return fun ( ... ): nil tx , async fun (): ... rx
112
123
M .channel .oneshot = function ()
113
124
local val = nil
114
125
local saved_callback = nil
@@ -147,20 +158,26 @@ M.channel.oneshot = function()
147
158
if is_single then
148
159
return callback (val )
149
160
else
150
- return callback (tbl .unpack (val ))
161
+ return callback (tbl .unpack (val --[[ @as table ]] ))
151
162
end
152
163
else
153
164
saved_callback = callback
154
165
end
155
- end , 1 )
166
+ end , 1 ) --[[ @as async fun(): ... ]]
156
167
157
168
return sender , receiver
158
169
end
159
170
171
+ --- @class PlenaryAsyncCounterTx
172
+ --- @field send fun (): nil
173
+
174
+ --- @class PlenaryAsyncCounterRx
175
+ --- @field recv async fun (): nil
176
+ --- @field last async fun (): nil
177
+
160
178
--- A counter channel.
161
179
--- Basically a channel that you want to use only to notify and not to send any actual values.
162
- --- @return function : sender
163
- --- @return function : receiver
180
+ --- @return PlenaryAsyncCounterTx tx , PlenaryAsyncCounterRx rx
164
181
M .channel .counter = function ()
165
182
local counter = 0
166
183
local condvar = Condvar .new ()
@@ -191,9 +208,15 @@ M.channel.counter = function()
191
208
return Sender , Receiver
192
209
end
193
210
211
+ --- @class PlenaryAsyncMpscTx
212
+ --- @field send fun ( ... : any ): nil
213
+
214
+ --- @class PlenaryAsyncMpscRx
215
+ --- @field recv async fun (): ...
216
+ --- @field last async fun (): ...
217
+
194
218
--- A multiple producer single consumer channel
195
- --- @return table
196
- --- @return table
219
+ --- @return PlenaryAsyncMpscTx , PlenaryAsyncMpscRx
197
220
M .channel .mpsc = function ()
198
221
local deque = Deque .new ()
199
222
local condvar = Condvar .new ()
0 commit comments