@@ -2,22 +2,26 @@ local a = require "plenary.async.async"
22local Deque = require (" plenary.async.structs" ).Deque
33local tbl = require " plenary.tbl"
44
5+ --- @class PlenaryAsyncControl
56local M = {}
67
8+ --- @class PlenaryAsyncCondvar
9+ --- @field handles (fun (): nil )[]
710local Condvar = {}
811Condvar .__index = Condvar
912
10- --- @class Condvar
11- --- @return Condvar
13+ --- @return PlenaryAsyncCondvar
1214function Condvar .new ()
1315 return setmetatable ({ handles = {} }, Condvar )
1416end
1517
1618--- `blocks` the thread until a notification is received
19+ --- @param self PlenaryAsyncCondvar
20+ --- @param callback fun (): nil
1721Condvar .wait = a .wrap (function (self , callback )
1822 -- not calling the callback will block the coroutine
1923 table.insert (self .handles , callback )
20- end , 2 )
24+ end , 2 ) --[[ @as async fun(): nil ]]
2125
2226--- notify everyone that is waiting on this Condvar
2327function Condvar :notify_all ()
5256
5357M .Condvar = Condvar
5458
59+ --- @class PlenaryAsyncSemaphore
60+ --- @field handles (fun ( permit : PlenaryAsyncPermit ): nil )[]
61+ --- @field permits integer
5562local Semaphore = {}
5663Semaphore .__index = Semaphore
5764
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
6167function Semaphore .new (initial_permits )
6268 vim .validate {
6369 initial_permits = {
7985--- permit:forget()
8086--- when a permit can be acquired returns it
8187--- call permit:forget() to forget the permit
88+ --- @param self PlenaryAsyncSemaphore
89+ --- @param callback fun ( permit : PlenaryAsyncPermit ): nil
8290Semaphore .acquire = a .wrap (function (self , callback )
8391 if self .permits > 0 then
8492 self .permits = self .permits - 1
@@ -87,8 +95,10 @@ Semaphore.acquire = a.wrap(function(self, callback)
8795 return
8896 end
8997
98+ --- @class PlenaryAsyncPermit
9099 local permit = {}
91100
101+ --- @param self_permit PlenaryAsyncPermit
92102 permit .forget = function (self_permit )
93103 self .permits = self .permits + 1
94104
@@ -99,16 +109,17 @@ Semaphore.acquire = a.wrap(function(self, callback)
99109 end
100110
101111 callback (permit )
102- end , 2 )
112+ end , 2 ) --[[ @as async fun(self: PlenaryAsyncSemaphore): PlenaryAsyncPermit ]]
103113
104114M .Semaphore = Semaphore
105115
116+ --- @class PlenaryAsyncControlChannel
106117M .channel = {}
107118
108119--- Creates a oneshot channel
109120--- returns a sender and receiver function
110121--- the sender is not async while the receiver is
111- --- @return function , function
122+ --- @return fun ( ... ): nil tx , async fun (): ... rx
112123M .channel .oneshot = function ()
113124 local val = nil
114125 local saved_callback = nil
@@ -147,20 +158,26 @@ M.channel.oneshot = function()
147158 if is_single then
148159 return callback (val )
149160 else
150- return callback (tbl .unpack (val ))
161+ return callback (tbl .unpack (val --[[ @as table ]] ))
151162 end
152163 else
153164 saved_callback = callback
154165 end
155- end , 1 )
166+ end , 1 ) --[[ @as async fun(): ... ]]
156167
157168 return sender , receiver
158169end
159170
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+
160178--- A counter channel.
161179--- 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
164181M .channel .counter = function ()
165182 local counter = 0
166183 local condvar = Condvar .new ()
@@ -191,9 +208,15 @@ M.channel.counter = function()
191208 return Sender , Receiver
192209end
193210
211+ --- @class PlenaryAsyncMpscTx
212+ --- @field send fun ( ... : any ): nil
213+
214+ --- @class PlenaryAsyncMpscRx
215+ --- @field recv async fun (): ...
216+ --- @field last async fun (): ...
217+
194218--- A multiple producer single consumer channel
195- --- @return table
196- --- @return table
219+ --- @return PlenaryAsyncMpscTx , PlenaryAsyncMpscRx
197220M .channel .mpsc = function ()
198221 local deque = Deque .new ()
199222 local condvar = Condvar .new ()
0 commit comments