robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
3.2 KiB122 linesraw
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)
18  for _, waiter in ipairs(waiters) do
19    if waiter then
20      return true
21    end
22  end
23  return 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)
36  if self._is_set then
37    return
38  end
39
40  local limited = max_woken ~= nil
41  if not has_waiters(self._waiters) then
42    self._is_set = true
43    return
44  end
45
46  self._is_set = true
47  if 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.
50    self._is_set = false
51  end
52
53  runtime.schedule(function()
54    local waiters = self._waiters
55    local waiters_to_notify = {} --- @type function[]
56    local limit = max_woken or math.huge
57    while #waiters > 0 and #waiters_to_notify < limit do
58      local waiter = table.remove(waiters, 1)
59      if waiter then
60        waiters_to_notify[#waiters_to_notify + 1] = waiter
61      end
62    end
63
64    if limited and #waiters_to_notify == 0 and not has_waiters(waiters) then
65      self._is_set = true
66    end
67
68    for _, waiter in ipairs(waiters_to_notify) do
69      waiter()
70    end
71  end)
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()
80  async.await(function(callback)
81    if self._is_set then
82      callback()
83    else
84      table.insert(self._waiters, callback)
85      return {
86        close = function(_, on_close)
87          -- set() compacts the waiter list, so cancellation cannot rely on the
88          -- original insertion index still pointing at this callback.
89          for i, waiter in ipairs(self._waiters) do
90            if waiter == callback then
91              self._waiters[i] = false
92              break
93            end
94          end
95          if on_close then
96            on_close()
97          end
98        end,
99      }
100    end
101  end)
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()
109  self._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()
118  return setmetatable({
119    _waiters = {},
120    _is_set = false,
121  }, Event)
122end