Skip to content

Commit

Permalink
fix: request pool block
Browse files Browse the repository at this point in the history
  • Loading branch information
AbaoFromCUG committed Apr 14, 2024
1 parent df2fba5 commit a4fb5e6
Show file tree
Hide file tree
Showing 16 changed files with 152 additions and 95 deletions.
7 changes: 7 additions & 0 deletions .neoconf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"neodev": {
"library": {
"plugins": ["plenary.nvim", "websocket.nvim"]
}
}
}
6 changes: 6 additions & 0 deletions .stylua.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
column_width = 150
line_endings = "Unix"
indent_type = "Spaces"
indent_width = 4
quote_style = "AutoPreferDouble"
no_call_parentheses = false
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ With 💤lazy.nvim:
-- auto connect server
auto_connect = true,
rpc_client = "async",
-- your jupyter host + neopyter port
remote_address = "127.0.0.1:9001",
file_pattern = { "*.ju.*" },
on_attach = function(bufnr)
Expand Down
2 changes: 1 addition & 1 deletion lua-tests/neopyter/utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ describe("parse notebook", function()
source = "# this is markdown cell with markdown too",
start_line = 1,
end_line = 5,
title= "cell's title",
title = "cell's title",
cell_type = "markdown",
},
}, cells)
Expand Down
10 changes: 6 additions & 4 deletions lua/neopyter/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ end
local function run_blocking(suspend_fn, ...)
local resolved = false
vim.schedule(function()
a.run(suspend_fn, function()
a.run(suspend_fn, function(ee)
print(ee)
resolved = true
end)
end)
Expand All @@ -34,10 +35,13 @@ function M.check()
run_blocking(function()
local status = jupyter.jupyterlab:is_attached()
if status then
health.info("Neopyter status: active")
local nvim_plugin_ver = jupyter.jupyterlab:get_nvim_plugin_version()
health.info(string.format("Neopyter@%s status: active", nvim_plugin_ver))
local is_connecting = jupyter.jupyterlab.client:is_connecting()
if is_connecting then
health.info("Rpc server status: active")
local jupyterlab_extension_ver = jupyter.jupyterlab:get_jupyterlab_extension_version()
health.info(string.format("Jupyter lab extension version: %s", jupyterlab_extension_ver))
else
health.info("Rpc server status: inactive")
end
Expand All @@ -48,9 +52,7 @@ function M.check()
select_mark = "*"
end
local msg = ""
print(notebook.local_path)
local nbconnect = notebook:is_connecting()
print(notebook.remote_path)
if nbconnect then
msg = string.format("%s %s 💫 %s", select_mark, notebook.local_path, notebook.remote_path)
else
Expand Down
7 changes: 1 addition & 6 deletions lua/neopyter/jupyter/jupyterlab.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,7 @@ function JupyterLab:connect(address)
local nvim_version = self:get_nvim_plugin_version()
if jupyterlab_version ~= nil and nvim_version ~= jupyterlab_version then
utils.notify_error(
string.format(
"The version of jupyterlab extension(%s) and neovim plugin(%s) do not match",
jupyterlab_version,
nvim_version
)
string.format("The version of jupyterlab extension(%s) and neovim plugin(%s) do not match", jupyterlab_version, nvim_version)
)
end
end
Expand Down Expand Up @@ -211,7 +207,6 @@ JupyterLab = async_wrap(JupyterLab, {
"attach",
"is_attached",
"is_connecting",
"get_nvim_plugin_version",
})

return JupyterLab
2 changes: 1 addition & 1 deletion lua/neopyter/jupyter/notebook.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function Notebook:attach()
return
end
self:partial_sync(start_row, old_end_row, new_end_row)
end, function() end)
end)
end,
})

Expand Down
2 changes: 1 addition & 1 deletion lua/neopyter/logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ return {
end,
warn = function(msg)
-- TODO:log to file
print(msg)
-- print(msg)
end,
}
11 changes: 2 additions & 9 deletions lua/neopyter/rpc/asyncclient.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,7 @@ function AsyncRpcClient:request(method, ...)
else
if method == "getVersion" then
utils.notify_error(
string.format(
"jupyterlab extension is outdated, it is recommended to update with `pip install -U neopyter`",
method,
res
)
string.format("jupyterlab extension is outdated, it is recommended to update with `pip install -U neopyter`", method, res)
)
else
utils.notify_error(string.format("RPC request [%s] failed, with error: %s", method, res))
Expand All @@ -123,10 +119,7 @@ function AsyncRpcClient:handle_response(data)
local callback = self.request_pool[msgid]
self.request_pool[msgid] = nil
logger.log(string.format("msgid [%s] response acceptd", msgid))
assert(
callback,
string.format("msg %s can't find callback: request_pool=%s", msgid, vim.inspect(self.request_pool))
)
assert(callback, string.format("msg %s can't find callback: request_pool=%s", msgid, vim.inspect(self.request_pool)))
if error == vim.NIL then
callback(true, result)
else
Expand Down
80 changes: 50 additions & 30 deletions lua/neopyter/rpc/wsserverclient.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ local a = require("plenary.async")
---@field single_connection? websocket.Connection
---@field private msg_count number
---@field private request_pool table<number, fun(...):any>
---@field private decoder neopyter.MsgpackDecoder
local WSServerClient = RpcClient:new({}) --[[@as neopyter.WSServerClient]]

---RpcClient constructor
Expand All @@ -20,7 +19,6 @@ function WSServerClient:new(opt)
local o = setmetatable(opt or {}, { __index = self }) --[[@as neopyter.WSServerClient]]
o.msg_count = 0
o.request_pool = {}
o.decoder = msgpack.Decoder:new()
return o
end

Expand All @@ -29,6 +27,10 @@ end
---@async
function WSServerClient:connect(address)
local restart_server = address ~= nil and self.address ~= address
for _, fun in pairs(self.request_pool) do
fun(false, "cancel")
end
self.request_pool = {}

self.address = address or self.address
assert(self.address, "Rpc client address is empty")
Expand Down Expand Up @@ -56,6 +58,9 @@ function WSServerClient:connect(address)
end,
})
end,
on_disconnect = function()
self:disconnect()
end,
})
end

Expand All @@ -65,6 +70,11 @@ function WSServerClient:disconnect()
self.single_connection:close()
self.single_connection = nil
self.server:close()

for _, fun in pairs(self.request_pool) do
fun(false, "cancel")
end
self.request_pool = {}
else
logger("disconnect, but connection not exists")
end
Expand All @@ -86,8 +96,11 @@ end
---@param ... unknown # name
---@return unknown|nil
function WSServerClient:request(method, ...)
if not self:is_connecting() then
utils.notify_error(string.format("RPC tcp client is disconnected, can't request [%s]", method))
if self.server == nil then
utils.notify_error(string.format("RPC websocket server is stop, can't request [%s]", method))
return
elseif self.single_connection == nil then
utils.notify_error(string.format("RPC websocket server is listening, but without client, can't request [%s]", method))
return
end
local msgid = self:gen_id()
Expand All @@ -105,11 +118,7 @@ function WSServerClient:request(method, ...)
else
if method == "getVersion" then
utils.notify_error(
string.format(
"jupyterlab extension is outdated, it is recommended to update with `pip install -U neopyter`",
method,
res
)
string.format("jupyterlab extension is outdated, it is recommended to update with `pip install -U neopyter`", method, res)
)
else
utils.notify_error(string.format("RPC request [%s] failed, with error: %s", method, res))
Expand All @@ -121,33 +130,44 @@ end
---@param data string
---@package
function WSServerClient:handle_response(data)
self.decoder:feed(data)
while true do
local msg = self.decoder:next()
if msg == nil then
break
end
-- logger.log(vim.inspect(msg))
if #msg == 4 and msg[1] == 1 then
local msgid, error, result = msg[2], msg[3], msg[4]
local callback = self.request_pool[msgid]
self.request_pool[msgid] = nil
logger.log(string.format("msgid [%s] response acceptd", msgid))
assert(
callback,
string.format("msg %s can't find callback: request_pool=%s", msgid, vim.inspect(self.request_pool))
local status, msg = pcall(vim.mpack.decode, data)
assert(status, vim.inspect(msg) .. data)
if status == false then
logger.warn("parse mpack error, reset request pool")
self:reset_request()
return
end
if #msg == 4 and msg[1] == 1 then
local msgid, error, result = msg[2], msg[3], msg[4]
local callback = self.request_pool[msgid]
self.request_pool[msgid] = nil
logger.log(string.format("msgid [%s] response acceptd", msgid))
assert(
callback,
string.format(
"msg %s can't find callback: request_pool=%s, msg=%s",
msgid,
vim.inspect(vim.tbl_keys(self.request_pool)),
vim.inspect(msg)
)
if error == vim.NIL then
callback(true, result)
else
callback(false, error)
end
)
if error == vim.NIL then
callback(true, result)
else
assert(false, "msgpack rpc response spec error, msg=" .. data)
callback(false, error)
end
else
assert(false, "msgpack rpc response spec error, msg=" .. data)
end
end

function WSServerClient:notify(event, ...) end

function WSServerClient:reset_request()
for _, fun in pairs(self.request_pool) do
fun(false, "reset")
end
self.request_pool = {}
end

return WSServerClient
2 changes: 1 addition & 1 deletion lua/neopyter/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function M.nvim_create_autocmd(event, opts)
end, function() end)
end
end
vim.api.nvim_create_autocmd(event, opts)
a.api.nvim_create_autocmd(event, opts)
end

---@class neopyter.ParseOption
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "neopyter",
"version": "0.2.0",
"version": "0.2.1",
"description": "A JupyterLab extension. Integrate JupyterLab and Neovim",
"workspaces": [
"ui-tests"
Expand Down
6 changes: 1 addition & 5 deletions scripts/minidoc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,4 @@ if _G.MiniDoc == nil then
minidoc.setup({})
end

minidoc.generate(
{ "lua/neopyter.lua", "lua/neopyter/jupyter/jupyterlab.lua", "lua/neopyter/jupyter/notebook.lua" },
nil,
nil
)
minidoc.generate({ "lua/neopyter.lua", "lua/neopyter/jupyter/jupyterlab.lua", "lua/neopyter/jupyter/notebook.lua" }, nil, nil)
34 changes: 24 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { StatusSidePanel } from './statusidepanel';
import { statusPageIcon } from './icons';
import { WindowedList } from '@jupyterlab/ui-components';
import { IConfig } from './settings';
import { DockPanel, Widget } from '@lumino/widgets';

// Transfer Data
type TCell = {
Expand Down Expand Up @@ -61,20 +62,14 @@ const neopyterPlugin: JupyterFrontEndPlugin<void> = {
restorer.add(sidebar, '@neopyter/graphsidebar');
}

let currentNotebookPanel: NotebookPanel | null = nbtracker.currentWidget;
labShell.currentChanged.connect(() => {
if (labShell.currentWidget instanceof NotebookPanel) {
currentNotebookPanel = labShell.currentWidget;
}
});

const getNotebookModel = (path?: string) => {
let notebookPanel;
if (path) {
notebookPanel = docmanager.findWidget(path) as unknown as NotebookPanel;
}
let notebook = notebookPanel?.content as Notebook | undefined;
if (!notebook) {
const currentNotebookPanel = labShell.currentWidget as NotebookPanel;
if (currentNotebookPanel?.isUntitled) {
notebookPanel = currentNotebookPanel;
notebook = notebookPanel.content;
Expand Down Expand Up @@ -117,7 +112,7 @@ const neopyterPlugin: JupyterFrontEndPlugin<void> = {
};
const docmanagerDispatcher = {
getCurrentNotebook: () => {
const notebookPanel = currentNotebookPanel;
const notebookPanel = labShell.currentWidget as NotebookPanel;
if (notebookPanel) {
const context = docmanager.contextForWidget(notebookPanel);
return context?.localPath;
Expand Down Expand Up @@ -149,8 +144,27 @@ const neopyterPlugin: JupyterFrontEndPlugin<void> = {
activateNotebook: (path: string) => {
const { notebookPanel } = getNotebookModel(path);
labShell.activateById(notebookPanel.id);
notebookPanel.node.focus();
currentNotebookPanel = notebookPanel;
notebookPanel.activate();
const emitEvent = (widget: Widget) => {
const event = new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window
});
widget.node.click();
widget.node.dispatchEvent(event);
};
emitEvent(labShell);
// @ts-expect-error hack private property
const dockPanel: DockPanel = labShell._dockPanel;
// for (const tabBar of dockPanel.tabBars) {
// tabBar.titles.findIndex(title => {
// console.log(title.owner);
// return false;
// });
// }

emitEvent(notebookPanel);
},
closeFile: async (path: string) => {
return await docmanager.closeFile(path);
Expand Down
Loading

0 comments on commit a4fb5e6

Please sign in to comment.