@@ -34,64 +34,68 @@ M.debounce_strategy = {
3434--- @param frequency_in_ms number Miniumum amount of time between invocations of fn.
3535--- @param strategy number The debounce_strategy to use , determines which calls to fn are not dropped.
3636--- @param callback function Called with the result of executing fn as : callback (success , result )
37- M .debounce = function (id , fn , frequency_in_ms , strategy , callback )
38- strategy = strategy or M .debounce_strategy .CALL_FIRST_AND_LAST
37+ M .debounce = function (id , fn , frequency_in_ms , strategy )
3938 local fn_data = tracked_functions [id ]
39+
40+ local defer_function = function ()
41+ fn_data .in_debounce_period = true
42+ vim .defer_fn (function ()
43+ local current_data = tracked_functions [id ]
44+ local _fn = current_data .fn
45+ current_data .fn = nil
46+ current_data .in_debounce_period = false
47+ if _fn ~= nil then
48+ M .debounce (id , _fn , current_data .frequency_in_ms , strategy )
49+ end
50+ end , frequency_in_ms )
51+ end
52+
4053 if fn_data == nil then
4154 -- first call for this id
4255 fn_data = {
4356 id = id ,
44- fn = nil ,
57+ in_debounce_period = false ,
58+ fn = fn ,
4559 frequency_in_ms = frequency_in_ms ,
46- postponed_callback = nil ,
47- in_debounce_period = true ,
4860 }
49- if strategy == M .debounce_strategy .CALL_LAST_ONLY then
50- fn_data .in_debounce_period = true
51- end
5261 tracked_functions [id ] = fn_data
53- else
54- if fn_data .in_debounce_period then
55- -- This id was called recently and can't be executed again yet.
56- -- Just keep track of the details for this request so it
57- -- can be executed at the end of the debounce period.
58- -- Last one in wins.
59- fn_data .fn = fn
60- fn_data .frequency_in_ms = frequency_in_ms
61- fn_data .postponed_callback = callback
62+ if strategy == M .debounce_strategy .CALL_LAST_ONLY then
63+ defer_function ()
6264 return
6365 end
66+ else
67+ fn_data .fn = fn
68+ fn_data .frequency_in_ms = frequency_in_ms
69+ end
70+
71+ if fn_data .in_debounce_period then
72+ -- This id was called recently and can't be executed again yet.
73+ -- Last one in wins.
74+ return
6475 end
6576
6677 -- Run the requested function normally.
6778 -- Use a pcall to ensure the debounce period is still respected even if
6879 -- this call throws an error.
6980 fn_data .in_debounce_period = true
7081 local success , result = pcall (fn )
82+ fn_data .fn = nil
83+ fn = nil
7184
7285 if not success then
73- log .error (" Error in neo-tree.utils.debounce: " , result )
86+ log .error (result )
7487 end
7588
76- -- Now schedule the next earliest execution.
77- -- If there are no calls to run the same function between now
78- -- and when this deferred executes, nothing will happen.
79- -- If there are several calls, only the last one in will run.
80- vim .defer_fn (function ()
81- local current_data = tracked_functions [id ]
82- local _callback = current_data .postponed_callback
83- local _fn = current_data .fn
84- current_data .postponed_callback = nil
85- current_data .fn = nil
86- current_data .in_debounce_period = false
87- if _fn ~= nil then
88- M .debounce (id , _fn , current_data .frequency_in_ms , strategy , _callback )
89- end
90- end , frequency_in_ms )
91-
92- -- The callback function is outside the scope of the debounce period
93- if type (callback ) == " function" then
94- callback (success , result )
89+ if strategy == M .debounce_strategy .CALL_LAST_ONLY then
90+ -- We are done with this debounce
91+ tracked_functions [id ] = nil
92+ else
93+ -- Now schedule the next earliest execution.
94+ -- If there are no calls to run the same function between now
95+ -- and when this deferred executes, nothing will happen.
96+ -- If there are several calls, only the last one in will run.
97+ strategy = M .debounce_strategy .CALL_LAST_ONLY
98+ defer_function ()
9599 end
96100end
97101
0 commit comments