feat: non-blocking API

This commit is contained in:
əlemi 2024-09-17 16:33:22 +02:00
parent 646760f3cb
commit 9dea608f04
Signed by: alemi
GPG key ID: A4895B84D311642C
5 changed files with 179 additions and 170 deletions

View file

@ -13,7 +13,6 @@ local ticks = {}
---@param buffer? integer buffer to use for attaching (will clear content) ---@param buffer? integer buffer to use for attaching (will clear content)
---@param content? string if provided, set this content after attaching ---@param content? string if provided, set this content after attaching
---@param nowait? boolean skip waiting for initial content sync ---@param nowait? boolean skip waiting for initial content sync
---@return BufferController
local function attach(name, buffer, content, nowait) local function attach(name, buffer, content, nowait)
if buffer_id_map[name] ~= nil then if buffer_id_map[name] ~= nil then
error("already attached to buffer " .. name) error("already attached to buffer " .. name)
@ -25,8 +24,7 @@ local function attach(name, buffer, content, nowait)
vim.api.nvim_set_option_value('fileformat', 'unix', { buf = buffer }) vim.api.nvim_set_option_value('fileformat', 'unix', { buf = buffer })
vim.api.nvim_buf_set_name(buffer, name) vim.api.nvim_buf_set_name(buffer, name)
local controller = session.workspace:attach(name):await() session.workspace:attach(name):and_then(function (controller)
if not nowait then if not nowait then
local promise = controller:poll() local promise = controller:poll()
for i=1, 20, 1 do for i=1, 20, 1 do
@ -125,14 +123,14 @@ local function attach(name, buffer, content, nowait)
end end
end end
controller:callback(function (_controller) async:send() end) controller:callback(function (_) async:send() end)
vim.defer_fn(function() async:send() end, 500) -- force a try_recv after 500ms vim.defer_fn(function() async:send() end, 500) -- force a try_recv after 500ms
local filetype = vim.filetype.match({ buf = buffer }) local filetype = vim.filetype.match({ buf = buffer })
vim.api.nvim_set_option_value("filetype", filetype, { buf = buffer }) vim.api.nvim_set_option_value("filetype", filetype, { buf = buffer })
print(" ++ attached to buffer " .. name) print(" ++ attached to buffer " .. name)
require('codemp.window').update() require('codemp.window').update()
return controller end)
end end
---@param name string ---@param name string
@ -180,9 +178,10 @@ local function create(buffer)
if session.workspace == nil then if session.workspace == nil then
error("join a workspace first") error("join a workspace first")
end end
session.workspace:create(buffer):await() session.workspace:create(buffer):and_then(function ()
print(" ++ created buffer " .. buffer) print(" ++ created buffer " .. buffer)
require('codemp.window').update() require('codemp.window').update()
end)
end end
return { return {

View file

@ -8,9 +8,11 @@ local function connect()
if CODEMP.config.password == nil then if CODEMP.config.password == nil then
CODEMP.config.password = vim.g.codemp_password or vim.fn.input("password > ", "") CODEMP.config.password = vim.g.codemp_password or vim.fn.input("password > ", "")
end end
session.client = CODEMP.native.connect(CODEMP.config):await() CODEMP.native.connect(CODEMP.config):and_then(function (client)
session.client = client
require('codemp.window').update() require('codemp.window').update()
vim.schedule(function () workspace.list() end) workspace.list()
end)
end end
return { return {

View file

@ -50,9 +50,10 @@ local connected_actions = {
start = function(ws) start = function(ws)
if ws == nil then error("missing workspace name") end if ws == nil then error("missing workspace name") end
session.client:create_workspace(ws):await() session.client:create_workspace(ws):and_then(function ()
vim.schedule(function () workspace.list() end)
print(" <> created workspace " .. ws) print(" <> created workspace " .. ws)
workspace.list()
end)
end, end,
available = function() available = function()
@ -71,8 +72,9 @@ local connected_actions = {
else else
ws = vim.fn.input("workspace > ", "") ws = vim.fn.input("workspace > ", "")
end end
session.client:invite_to_workspace(ws, user):await() session.client:invite_to_workspace(ws, user):and_then(function ()
print(" ][ invited " .. user .. " to workspace " .. ws) print(" :: invited " .. user .. " to workspace " .. ws)
end)
end, end,
disconnect = function() disconnect = function()
@ -108,8 +110,9 @@ local joined_actions = {
delete = function(path) delete = function(path)
if path == nil then error("missing buffer name") end if path == nil then error("missing buffer name") end
session.workspace:delete(path):await() session.workspace:delete(path):and_then(function()
print(" xx deleted buffer " .. path) print(" xx deleted buffer " .. path)
end)
end, end,
buffers = function() buffers = function()

View file

@ -116,19 +116,21 @@ M.delete = function(state, path, extra)
vim.ui.input({ prompt = "delete buffer '" .. selected.name .. "'?" }, function (input) vim.ui.input({ prompt = "delete buffer '" .. selected.name .. "'?" }, function (input)
if input == nil then return end if input == nil then return end
if not vim.startswith("y", string.lower(input)) then return end if not vim.startswith("y", string.lower(input)) then return end
session.workspace:delete(selected.name):await() session.workspace:delete(selected.name):and_then(function ()
print("deleted buffer " .. selected.name) print("deleted buffer " .. selected.name)
manager.refresh("codemp") manager.refresh("codemp")
end) end)
end)
elseif selected.type == "workspace" then elseif selected.type == "workspace" then
if session.client == nil then error("connect to server first") end if session.client == nil then error("connect to server first") end
vim.ui.input({ prompt = "delete buffer '" .. selected.name .. "'?" }, function (input) vim.ui.input({ prompt = "delete buffer '" .. selected.name .. "'?" }, function (input)
if input == nil then return end if input == nil then return end
if not vim.startswith("y", string.lower(input)) then return end if not vim.startswith("y", string.lower(input)) then return end
session.client:delete_workspace(selected.name):await() session.client:delete_workspace(selected.name):and_then(function ()
print("deleted workspace " .. selected.name) print("deleted workspace " .. selected.name)
manager.refresh("codemp") manager.refresh("codemp")
end) end)
end)
end end
end end
@ -138,22 +140,25 @@ M.add = function(state, path, extra)
if vim.startswith(selected.name, "#") then if vim.startswith(selected.name, "#") then
vim.ui.input({ prompt = "new buffer path" }, function(input) vim.ui.input({ prompt = "new buffer path" }, function(input)
if input == nil or input == "" then return end if input == nil or input == "" then return end
session.workspace:create(input):await() session.workspace:create(input):and_then(function ()
manager.refresh("codemp") manager.refresh("codemp")
end) end)
end)
elseif selected.name == "workspaces" then elseif selected.name == "workspaces" then
vim.ui.input({ prompt = "new workspace name" }, function(input) vim.ui.input({ prompt = "new workspace name" }, function(input)
if input == nil or input == "" then return end if input == nil or input == "" then return end
session.client:create_workspace(input):await() session.client:create_workspace(input):and_then(function ()
vim.schedule(function () require('codemp.workspace').list() end) require('codemp.workspace').list()
end)
end) end)
end end
elseif selected.type == "workspace" then elseif selected.type == "workspace" then
vim.ui.input({ prompt = "user name to invite" }, function(input) vim.ui.input({ prompt = "user name to invite" }, function(input)
if input == nil or input == "" then return end if input == nil or input == "" then return end
session.client:invite_to_workspace(selected.name, input):await() session.client:invite_to_workspace(selected.name, input):and_then(function ()
print("invited user " .. input .. " to workspace " .. selected.name) print("invited user " .. input .. " to workspace " .. selected.name)
end) end)
end)
end end
manager.refresh("codemp") manager.refresh("codemp")
end end

View file

@ -12,14 +12,14 @@ local user_hl = {}
local function fetch_workspaces_list() local function fetch_workspaces_list()
local new_list = {} local new_list = {}
local owned = session.client:list_workspaces(true, false):await() session.client:list_workspaces(true, false):and_then(function (owned)
for _, ws in pairs(owned) do for _, ws in pairs(owned) do
table.insert(new_list, { table.insert(new_list, {
name = ws, name = ws,
owned = true, owned = true,
}) })
end end
local invited = session.client:list_workspaces(false, true):await() session.client:list_workspaces(false, true):and_then(function (invited)
for _, ws in pairs(invited) do for _, ws in pairs(invited) do
table.insert(new_list, { table.insert(new_list, {
name = ws, name = ws,
@ -28,6 +28,8 @@ local function fetch_workspaces_list()
end end
session.available = new_list session.available = new_list
require('codemp.window').update() require('codemp.window').update()
end)
end)
end end
---@param ws Workspace ---@param ws Workspace
@ -105,10 +107,9 @@ local function register_cursor_handler(ws)
end end
---@param workspace string workspace name to join ---@param workspace string workspace name to join
---@return Workspace
---join a workspace and register event handlers ---join a workspace and register event handlers
local function join(workspace) local function join(workspace)
local ws = session.client:join_workspace(workspace):await() session.client:join_workspace(workspace):and_then(function (ws)
print(" >< joined workspace " .. ws.name) print(" >< joined workspace " .. ws.name)
register_cursor_callback(ws) register_cursor_callback(ws)
register_cursor_handler(ws) register_cursor_handler(ws)
@ -139,8 +140,7 @@ local function join(workspace)
session.workspace = ws session.workspace = ws
require('codemp.window').update() require('codemp.window').update()
end)
return ws
end end
local function leave() local function leave()