Skip to content

Commit e319f25

Browse files
committed
fix(input): unmount race condition
1 parent e916f67 commit e319f25

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

lua/nui/input/init.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ function Input:unmount()
128128

129129
Input.super.unmount(self)
130130

131+
if self._.loading then
132+
return
133+
end
134+
135+
self._.loading = true
136+
131137
local pending_submit_value = self._.pending_submit_value
132138

133139
vim.schedule(function()
@@ -147,6 +153,7 @@ function Input:unmount()
147153
else
148154
self._.on_close()
149155
end
156+
self._.loading = false
150157
end)
151158
end
152159

tests/nui/input/init_spec.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,25 @@ describe("nui.input", function()
9999

100100
eq(done, true)
101101
end)
102+
103+
it("is called on unmount", function()
104+
local done = false
105+
106+
input = Input(popup_options, {
107+
on_close = function()
108+
done = true
109+
end,
110+
})
111+
112+
input:mount()
113+
input:unmount()
114+
115+
vim.fn.wait(200, function()
116+
return done
117+
end)
118+
119+
eq(done, true)
120+
end)
102121
end)
103122

104123
describe("cursor_position_patch", function()
@@ -210,4 +229,27 @@ describe("nui.input", function()
210229
eq(vim.api.nvim_win_get_cursor(parent_winid), initial_cursor)
211230
end)
212231
end)
232+
233+
describe("method :unmount", function()
234+
it("is idempotent", function()
235+
local done = 0
236+
237+
input = Input(popup_options, {
238+
on_close = function()
239+
done = done + 1
240+
end,
241+
})
242+
243+
input:mount()
244+
input:unmount()
245+
input:unmount()
246+
input:unmount()
247+
248+
vim.fn.wait(200, function()
249+
return done > 1
250+
end)
251+
252+
eq(done, 1)
253+
end)
254+
end)
213255
end)

0 commit comments

Comments
 (0)