diff --git a/lua/codemp/utils.lua b/lua/codemp/utils.lua index eff5939..02bf748 100644 --- a/lua/codemp/utils.lua +++ b/lua/codemp/utils.lua @@ -29,40 +29,6 @@ local function color(name) } 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 last integer ---@return integer, integer, integer, integer @@ -227,7 +193,6 @@ return { }, available_colors = colors, color = color, - poller = async_poller, sep = separator, setup_colors = setup_colors, } diff --git a/lua/codemp/workspace.lua b/lua/codemp/workspace.lua index 2545b3e..a67f478 100644 --- a/lua/codemp/workspace.lua +++ b/lua/codemp/workspace.lua @@ -181,7 +181,7 @@ local function join(workspace) register_cursor_handler(ws:cursor()) CODEMP.workspace = ws for _, user in pairs(CODEMP.workspace:user_list()) do - buffers.users[user] = "" + buffers.users[user.name] = "" user_hl[user] = { ns = vim.api.nvim_create_namespace("codemp-cursor-" .. user.name), hi = utils.color(user.name), @@ -190,15 +190,10 @@ local function join(workspace) } end require('codemp.window').update() - local ws_name = ws:id() - events_poller = utils.poller( - function() - if CODEMP.client == nil then return nil end - local wspace = CODEMP.client:get_workspace(ws_name) - if wspace == nil then return nil end - return wspace:recv() - end, - function(event) + local async = vim.uv.new_async(function () + while true do + local event = ws:try_recv():await() + if event == nil then break end if event.type == "leave" then if buffers.users[event.value] ~= nil then local buf_name = buffers.users[event.value] @@ -218,9 +213,10 @@ local function join(workspace) mark = nil, } end - require('codemp.window').update() end - ) + require('codemp.window').update() + end) + ws:callback(function(_) async:send() end) end) end