@@ -34,64 +34,68 @@ M.debounce_strategy = {
34
34
--- @param frequency_in_ms number Miniumum amount of time between invocations of fn.
35
35
--- @param strategy number The debounce_strategy to use , determines which calls to fn are not dropped.
36
36
--- @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 )
39
38
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
+
40
53
if fn_data == nil then
41
54
-- first call for this id
42
55
fn_data = {
43
56
id = id ,
44
- fn = nil ,
57
+ in_debounce_period = false ,
58
+ fn = fn ,
45
59
frequency_in_ms = frequency_in_ms ,
46
- postponed_callback = nil ,
47
- in_debounce_period = true ,
48
60
}
49
- if strategy == M .debounce_strategy .CALL_LAST_ONLY then
50
- fn_data .in_debounce_period = true
51
- end
52
61
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 ()
62
64
return
63
65
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
64
75
end
65
76
66
77
-- Run the requested function normally.
67
78
-- Use a pcall to ensure the debounce period is still respected even if
68
79
-- this call throws an error.
69
80
fn_data .in_debounce_period = true
70
81
local success , result = pcall (fn )
82
+ fn_data .fn = nil
83
+ fn = nil
71
84
72
85
if not success then
73
- log .error (" Error in neo-tree.utils.debounce: " , result )
86
+ log .error (result )
74
87
end
75
88
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 ()
95
99
end
96
100
end
97
101
0 commit comments