Skip to content

Commit

Permalink
fix(consumers/state): wrap listeners with semaphore
Browse files Browse the repository at this point in the history
  • Loading branch information
rcarriga committed May 24, 2024
1 parent 6f35d79 commit b781c08
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lua/neotest/consumers/state/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ local tracker
---@nodoc
local function init(client)
local updated_event = nio.control.event()
local semaphore = nio.control.semaphore()
local wrap_listener = function(listener)
return function(...)
local args = { ... }
semaphore.with(function()
listener(unpack(args))
end)
end
end
tracker = StateTracker:new(client)
local function update_positions()
while true do
Expand Down Expand Up @@ -38,21 +47,21 @@ local function init(client)
logger.error("Error in state consumer", debug.traceback(msg, 2))
end)
end)
client.listeners.discover_positions = function(adapter_id)
client.listeners.discover_positions = wrap_listener(function(adapter_id)
if not tracker:adapter_state(adapter_id) then
tracker:register_adapter(adapter_id)
end
updated_event.set()
end
end)

client.listeners.run = function(adapter_id, _, position_ids)
client.listeners.run = wrap_listener(function(adapter_id, _, position_ids)
tracker:update_running(adapter_id, position_ids)
end
end)

client.listeners.results = function(adapter_id, results)
client.listeners.results = wrap_listener(function(adapter_id, results)
tracker:decrement_running(adapter_id, results)
updated_event.set()
end
end)
end

---@param args? table
Expand Down

0 comments on commit b781c08

Please sign in to comment.