Skip to content

Commit 35c0b43

Browse files
committed
feat: add abortSignal for cancelling of async function
1 parent f1df3d6 commit 35c0b43

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

lua/nvim-tree/async.lua

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,37 @@ end
128128

129129
M.Interrupter = Interrupter
130130

131+
---This is useful for cancelling execution async function
132+
---@class AbortSignal
133+
---@field aborted boolean
134+
---@field private abort_cbs function[]
135+
local AbortSignal = {}
136+
137+
---@return AbortSignal
138+
function AbortSignal.new()
139+
local obj = {
140+
aborted = false,
141+
abort_cbs = {},
142+
}
143+
144+
setmetatable(obj, { __index = AbortSignal })
145+
return obj
146+
end
147+
148+
function AbortSignal:abort()
149+
if not self.aborted then
150+
self.aborted = true
151+
for _, cb in pairs(self.abort_cbs) do
152+
cb()
153+
end
154+
end
155+
end
156+
157+
---@param cb function
158+
function AbortSignal:on_abort(cb)
159+
table.insert(self.abort_cbs, cb)
160+
end
161+
162+
M.AbortSignal = AbortSignal
163+
131164
return M

0 commit comments

Comments
 (0)