robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local async =require ( 'vim.async._core' ) 2local runtime =require ( 'vim.async._runtime' ) 3 4--- An event can be used to notify multiple tasks that some event has 5--- happened. An Event object manages an internal flag that can be set to true 6--- with the `set()` method and reset to `false` with the `clear()` method. 7--- The `wait()` method blocks until the flag is set to `true`. The flag is 8--- set to `false` initially. 9--- @class vim.async.Event 10--- @field private _is_set boolean 11--- @field private _waiters (function|false)[] 12local Event ={} 13Event . __index =Event 14 15--- @param waiters (function|false)[] 16--- @return boolean 17local function has_waiters ( waiters ) 18for _ , waiter in ipairs ( waiters ) do 19if waiter then 20return true 21end 22end 23return false 24end 25 26--- Set the event. 27--- 28--- All tasks waiting for event to be set will be awakened on a later event-loop 29--- turn. 30--- 31--- If `max_woken` is provided, only up to `max_woken` waiters will be woken. 32--- If waiters are woken this way, the event is reset because the signal is 33--- reserved for those waiters. 34--- @param max_woken? integer 35function Event : set ( max_woken ) 36if self . _is_set then 37return 38end 39 40local limited =max_woken ~= nil 41if not has_waiters ( self . _waiters ) then 42self . _is_set =true 43return 44end 45 46self . _is_set =true 47if limited then 48-- The signal is reserved for existing waiters and will be assigned on the 49-- scheduled turn. New waiters must not consume it first. 50self . _is_set =false 51end 52 53runtime . schedule ( function () 54local waiters =self . _waiters 55local waiters_to_notify ={} --- @type function[] 56local limit =max_woken or math . huge 57while # waiters > 0 and # waiters_to_notify < limit do 58local waiter =table . remove ( waiters , 1 ) 59if waiter then 60waiters_to_notify [ # waiters_to_notify + 1 ] =waiter 61end 62end 63 64if limited and # waiters_to_notify == 0 and not has_waiters ( waiters ) then 65self . _is_set =true 66end 67 68for _ , waiter in ipairs ( waiters_to_notify ) do 69waiter () 70end 71end ) 72end 73 74--- Wait until the event is set. 75--- 76--- If the event is set, return immediately. Otherwise block until another 77--- task calls set(). 78--- @async 79function Event : wait () 80async . await ( function ( callback ) 81if self . _is_set then 82callback () 83else 84table . insert ( self . _waiters , callback ) 85return { 86close =function ( _ , on_close ) 87-- set() compacts the waiter list, so cancellation cannot rely on the 88-- original insertion index still pointing at this callback. 89for i , waiter in ipairs ( self . _waiters ) do 90if waiter == callback then 91self . _waiters [ i ] =false 92break 93end 94end 95if on_close then 96on_close () 97end 98end , 99} 100end 101end ) 102end 103 104--- Clear (unset) the event. 105--- 106--- Tasks awaiting on wait() will now block until the set() method is called 107--- again. 108function Event : clear () 109self . _is_set =false 110end 111 112--- Create a new event. 113--- 114--- An event can signal to multiple listeners to resume execution. 115--- The event can be set from a non-async context. 116--- @return vim.async.Event 117return function () 118return setmetatable ( { 119_waiters ={} , 120_is_set =false , 121} , Event ) 122end