fix: namespace ops must happen on main loop

this plugin is becoming a bit of vim.schedule hell...
maybe the :and_then() callback should make sure it runs on the main
loop? maybe offer an :and_then(), which runs on main loop, and a :and_then_soon(),
which runs in the callback thread itself
This commit is contained in:
əlemi 2025-02-15 12:49:56 +01:00
parent 91bc8f8dba
commit 214f7db24b
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -155,6 +155,7 @@ local function join(workspace)
register_cursor_callback(ws:cursor(), ws:id())
register_cursor_handler(ws:cursor())
CODEMP.workspace = ws
vim.schedule(function ()
for _, user in pairs(CODEMP.workspace:user_list()) do
buffers.users[user.name] = ""
user_hl[user.name] = {
@ -165,12 +166,14 @@ local function join(workspace)
}
end
require('codemp.window').update()
end)
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 == "UserLeave" then
if buffers.users[event.name] ~= nil then
vim.schedule(function()
local buf_name = buffers.users[event.name]
local buf_id = buffers.map_rev[buf_name]
if buf_id ~= nil then
@ -178,8 +181,10 @@ local function join(workspace)
end
buffers.users[event.name] = nil
user_hl[event.name] = nil
end)
end
elseif event.type == "UserJoin" then
vim.schedule(function()
buffers.users[event.name] = ""
user_hl[event.name] = {
ns = vim.api.nvim_create_namespace("codemp-cursor-" .. event.name),
@ -187,6 +192,7 @@ local function join(workspace)
pos = { 0, 0 },
mark = nil,
}
end)
end
end
vim.schedule(function () require('codemp.window').update() end)