Skip to content

Commit 84aba4d

Browse files
committed
once for onSafeState
1 parent b216737 commit 84aba4d

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

lua/multicursor-nvim/core.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ end
2121
--- Registers a safe state callback.
2222
--- It will be called whenever the input manager is in a safe state.
2323
--- @param callback fun(info: mc.SafeStateInfo)
24-
function core.onSafeState(callback)
25-
inputManager:onSafeState(callback)
24+
--- @param opts? { once?: boolean }
25+
function core.onSafeState(callback, opts)
26+
inputManager:onSafeState(callback, opts)
2627
end
2728

2829
--- Registers a keymap layer callback.

lua/multicursor-nvim/input-manager.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,18 @@ end
121121
--- @field wasMode string
122122

123123
--- @param callback fun(info: mc.SafeStateInfo)
124-
function InputManager:onSafeState(callback)
124+
--- @param opts? { once?: boolean }
125+
function InputManager:onSafeState(callback, opts)
125126
if not self._safeStateCallbacks then
126127
self._safeStateCallbacks = {}
127128
end
129+
if opts and opts.once then
130+
local wrapped = callback
131+
callback = function(info)
132+
wrapped(info)
133+
tbl.remove(self._safeStateCallbacks, callback)
134+
end
135+
end
128136
self._safeStateCallbacks[#self._safeStateCallbacks + 1] = callback
129137
end
130138

lua/multicursor-nvim/tbl.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ function tbl.filter(t, callback)
6565
return result
6666
end
6767

68+
--- @generic T
69+
--- @param t T[]
70+
--- @param value T
71+
function tbl.remove(t, value)
72+
local idx = tbl.indexOf(t, value)
73+
if idx then
74+
table.remove(t, idx)
75+
end
76+
end
77+
6878
--- @generic T
6979
--- @param t T[]
7080
--- @callback fun(v: T, i: integer, t: T[]): any

0 commit comments

Comments
 (0)