Skip to content

Commit

Permalink
fix(asyncjob): interrupt long time job
Browse files Browse the repository at this point in the history
  • Loading branch information
Marskey committed Dec 29, 2023
1 parent e065145 commit 03baf04
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
32 changes: 29 additions & 3 deletions lua/telescope/_.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,13 @@ end
---@class BasePipe
---@field super Object: Always available
---@field handle uv_pipe_t: A pipe handle
---@field close_cb function: A handle closed callback
---@field extend function: Extend
local BasePipe = Object:extend()

function BasePipe:new()
self.eof_tx, self.eof_rx = channel.oneshot()
self.close_cb = nil
end

function BasePipe:close(force)
Expand All @@ -116,7 +118,7 @@ function BasePipe:close(force)

self.handle:read_stop()
if not self.handle:is_closing() then
self.handle:close()
self.handle:close(self.close_cb)
end

self._closed = true
Expand All @@ -133,11 +135,23 @@ end
function LinesPipe:read()
local read_tx, read_rx = channel.oneshot()

self.close_cb = function()
if not read_tx then
return
end
read_tx()
read_tx = nil
end

self.handle:read_start(function(err, data)
assert(not err, err)
self.handle:read_stop()

read_tx(data)
if read_tx then
read_tx(data)
read_tx = nil
end

if data == nil then
self.eof_tx()
end
Expand Down Expand Up @@ -220,11 +234,23 @@ end
function ChunkPipe:read()
local read_tx, read_rx = channel.oneshot()

self.close_cb = function()
if not read_tx then
return
end
read_tx()
read_tx = nil
end

self.handle:read_start(function(err, data)
assert(not err, err)
self.handle:read_stop()

read_tx(data)
if read_tx then
read_tx(data)
read_tx = nil
end

if data == nil then
self.eof_tx()
end
Expand Down
5 changes: 5 additions & 0 deletions lua/telescope/finders/async_job_finder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ return function(opts)
job:close(true)
end
end,
stop_read = function()
if job then
job:close(true)
end
end,
}, {
__call = callable,
})
Expand Down
1 change: 1 addition & 0 deletions lua/telescope/finders/async_oneshot_finder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ return function(opts)
end,
results = results,
entry_maker = entry_maker,
stop_read = function() end,
}, {
__call = function(_, prompt, process_result, process_complete)
if not job_started then
Expand Down
1 change: 1 addition & 0 deletions lua/telescope/finders/async_static_finder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ return function(opts)
results = results,
entry_maker = entry_maker,
close = function() end,
stop_read = function() end,
}, {
__call = function(_, _, process_result, process_complete)
for i, v in ipairs(results) do
Expand Down
1 change: 1 addition & 0 deletions lua/telescope/pickers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ function Picker:find()
find_id = self:_next_find_id()

status_updater { completed = false }
self.finder:stop_read()
self._on_lines(...)
end
end,
Expand Down

0 comments on commit 03baf04

Please sign in to comment.