fix: workspace controller, no longer need poller

This commit is contained in:
əlemi 2024-10-26 20:23:57 +02:00
parent a130c24687
commit 04ca78ac16
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 8 additions and 47 deletions

View file

@ -29,40 +29,6 @@ local function color(name)
} }
end end
---@class AsyncPoller
---@field promise WorkspaceEventPromise | nil
---@field timer luv.Timer
---@field generator fun(): WorkspaceEventPromise
---@field callback fun(e: WorkspaceEvent)
---@field stop fun(self: AsyncPoller)
---@return AsyncPoller
local function async_poller(generator, callback)
---@type AsyncPoller
local poller = {
promise = nil,
generator = generator,
callback = callback,
timer = vim.uv.new_timer(),
stop = function (this)
if this.promise ~= nil then
this.promise:cancel()
end
this.timer:stop()
this.timer:close()
end
}
poller.timer:start(500, 500, function()
if poller.promise == nil then poller.promise = poller.generator() end
if poller.promise.ready then
local res = poller.promise:await()
vim.schedule(function() poller.callback(res) end)
poller.promise = nil
end
end)
return poller
end
---@param first integer ---@param first integer
---@param last integer ---@param last integer
---@return integer, integer, integer, integer ---@return integer, integer, integer, integer
@ -227,7 +193,6 @@ return {
}, },
available_colors = colors, available_colors = colors,
color = color, color = color,
poller = async_poller,
sep = separator, sep = separator,
setup_colors = setup_colors, setup_colors = setup_colors,
} }

View file

@ -181,7 +181,7 @@ local function join(workspace)
register_cursor_handler(ws:cursor()) register_cursor_handler(ws:cursor())
CODEMP.workspace = ws CODEMP.workspace = ws
for _, user in pairs(CODEMP.workspace:user_list()) do for _, user in pairs(CODEMP.workspace:user_list()) do
buffers.users[user] = "" buffers.users[user.name] = ""
user_hl[user] = { user_hl[user] = {
ns = vim.api.nvim_create_namespace("codemp-cursor-" .. user.name), ns = vim.api.nvim_create_namespace("codemp-cursor-" .. user.name),
hi = utils.color(user.name), hi = utils.color(user.name),
@ -190,15 +190,10 @@ local function join(workspace)
} }
end end
require('codemp.window').update() require('codemp.window').update()
local ws_name = ws:id() local async = vim.uv.new_async(function ()
events_poller = utils.poller( while true do
function() local event = ws:try_recv():await()
if CODEMP.client == nil then return nil end if event == nil then break end
local wspace = CODEMP.client:get_workspace(ws_name)
if wspace == nil then return nil end
return wspace:recv()
end,
function(event)
if event.type == "leave" then if event.type == "leave" then
if buffers.users[event.value] ~= nil then if buffers.users[event.value] ~= nil then
local buf_name = buffers.users[event.value] local buf_name = buffers.users[event.value]
@ -218,9 +213,10 @@ local function join(workspace)
mark = nil, mark = nil,
} }
end end
require('codemp.window').update()
end end
) require('codemp.window').update()
end)
ws:callback(function(_) async:send() end)
end) end)
end end