Skip to content

Commit

Permalink
fix(rpc): release rpc registries after fzf exit (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
linrongbin16 authored Nov 9, 2023
1 parent 775c842 commit 52fb9ed
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions lua/fzfx/general.lua
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ local function general(name, query, bang, pipeline_configs, default_pipeline)
or default_context_maker

local context = context_maker()
local rpc_registries = {}

--- @param query_params string
local function provide_rpc(query_params)
Expand All @@ -784,15 +785,15 @@ local function general(name, query, bang, pipeline_configs, default_pipeline)
previewer_switch:preview(name, line_params, context)
end

local provide_rpc_registry_id =
server.get_rpc_server():register(provide_rpc)
local preview_rpc_registry_id =
server.get_rpc_server():register(preview_rpc)
local provide_rpc_id = server.get_rpc_server():register(provide_rpc)
local preview_rpc_id = server.get_rpc_server():register(preview_rpc)
table.insert(rpc_registries, provide_rpc_id)
table.insert(rpc_registries, preview_rpc_id)

local query_command = string.format(
"%s %s %s %s %s",
fzf_helpers.make_lua_command("general", "provider.lua"),
provide_rpc_registry_id,
provide_rpc_id,
provider_switch.metafile,
provider_switch.resultfile,
utils.shellescape(query)
Expand All @@ -804,7 +805,7 @@ local function general(name, query, bang, pipeline_configs, default_pipeline)
local reload_query_command = string.format(
"%s %s %s %s {q}",
fzf_helpers.make_lua_command("general", "provider.lua"),
provide_rpc_registry_id,
provide_rpc_id,
provider_switch.metafile,
provider_switch.resultfile
)
Expand All @@ -815,7 +816,7 @@ local function general(name, query, bang, pipeline_configs, default_pipeline)
local preview_command = string.format(
"%s %s %s %s {}",
fzf_helpers.make_lua_command("general", "previewer.lua"),
preview_rpc_registry_id,
preview_rpc_id,
previewer_switch.metafile,
previewer_switch.resultfile
)
Expand All @@ -830,12 +831,13 @@ local function general(name, query, bang, pipeline_configs, default_pipeline)
local function preview_label_rpc(line_params)
previewer_switch:preview_label(name, line_params, context)
end
local preview_label_rpc_registry_id =
local preview_label_rpc_id =
server.get_rpc_server():register(preview_label_rpc)
table.insert(rpc_registries, preview_label_rpc_id)
preview_label_command = string.format(
"%s %s {}",
fzf_helpers.make_lua_command("rpc", "notify.lua"),
preview_label_rpc_registry_id
preview_label_rpc_id
)
log.debug(
"|fzfx.general - general| preview_label_command:%s",
Expand Down Expand Up @@ -888,7 +890,6 @@ local function general(name, query, bang, pipeline_configs, default_pipeline)
})
end

local interaction_rpc_registries = {}
-- when no interactions, no need to add help
if type(pipeline_configs.interactions) == "table" then
for _, interaction_opts in pairs(pipeline_configs.interactions) do
Expand All @@ -903,17 +904,14 @@ local function general(name, query, bang, pipeline_configs, default_pipeline)
action(line_params, context)
end

local interaction_rpc_registry_id =
local interaction_rpc_id =
server.get_rpc_server():register(interaction_rpc)
table.insert(
interaction_rpc_registries,
interaction_rpc_registry_id
)
table.insert(rpc_registries, interaction_rpc_id)

local action_command = string.format(
"%s %s {}",
fzf_helpers.make_lua_command("rpc", "request.lua"),
interaction_rpc_registry_id
interaction_rpc_id
)
local bind_builder = string.format(
"%s:execute-silent(%s)",
Expand All @@ -931,8 +929,6 @@ local function general(name, query, bang, pipeline_configs, default_pipeline)
end
end

local switch_rpc_registries = {}

-- when only have 1 pipeline, no need to add help for switch keys
if pipeline_size > 1 then
for pipeline, provider_opts in pairs(pipeline_configs.providers) do
Expand All @@ -943,14 +939,13 @@ local function general(name, query, bang, pipeline_configs, default_pipeline)
previewer_switch:switch(pipeline)
end

local switch_rpc_registry_id =
server.get_rpc_server():register(switch_rpc)
table.insert(switch_rpc_registries, switch_rpc_registry_id)
local switch_rpc_id = server.get_rpc_server():register(switch_rpc)
table.insert(rpc_registries, switch_rpc_id)

local switch_command = string.format(
"%s %s",
fzf_helpers.make_lua_command("rpc", "request.lua"),
switch_rpc_registry_id
switch_rpc_id
)
local bind_builder = string.format(
"%s:unbind(%s)+execute-silent(%s)+change-header(%s)+reload(%s)",
Expand Down Expand Up @@ -1018,11 +1013,11 @@ local function general(name, query, bang, pipeline_configs, default_pipeline)
actions,
context,
function()
server.get_rpc_server():unregister(provide_rpc_registry_id)
server.get_rpc_server():unregister(preview_rpc_registry_id)
for _, switch_registry_id in ipairs(switch_rpc_registries) do
server.get_rpc_server():unregister(switch_registry_id)
end
vim.schedule_wrap(function()
for _, rpc_id in ipairs(rpc_registries) do
server:get_rpc_server():unregister(rpc_id)
end
end)
end
)
return p
Expand Down

0 comments on commit 52fb9ed

Please sign in to comment.