FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
diffview.nvim/lua/diffview/debounce.lua at main · vramana/diffview.nvim · GitHub
vramana
/
diffview.nvim
Public
forked from
sindrets/diffview.nvim
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
diffview.nvim
/
lua
/
diffview
/
debounce.lua
Copy path
More file actions
More file actions
Latest commit
History
History
History
246 lines (203 loc) · 5.33 KB
Breadcrumbs
diffview.nvim
/
lua
/
diffview
/
debounce.lua
Copy path
File metadata and controls
246 lines (203 loc) · 5.33 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
local
async
=
require
(
"
diffview.async
"
)
local
utils
=
require
(
"
diffview.utils
"
)
local
await
=
async
.
await
local
uv
=
vim
.
loop
local
M
=
{}
---
@class
Closeable
---
@field
close
fun
()
# Perform cleanup and release the associated handle.
---
@class
ManagedFn
:
Closeable
---
@operator
call
:
unknown ...
---
@param
...
uv_handle_t
function
M
.
try_close
(...)
local
args
=
{
...
}
for
i
=
1
,
select
(
"
#
"
,
...
)
do
local
handle
=
args
[
i
]
if
handle
and
not
handle
:
is_closing
()
then
handle
:
close
()
end
end
end
---
@return
ManagedFn
local
function
wrap
(
timer
,
fn
)
return
setmetatable
({}, {
__call
=
function
(
_
, ...)
fn
(
...
)
end
,
__index
=
{
close
=
function
()
timer
:
stop
()
M
.
try_close
(
timer
)
end
,
},
})
end
---
Debounces a function on the leading edge.
---
@param
ms
integer Timeout in ms
---
@param
fn
function Function to debounce
---
@return
ManagedFn
# Debounced function.
function
M
.
debounce_leading
(
ms
,
fn
)
local
timer
=
assert
(
uv
.
new_timer
())
local
lock
=
false
return
wrap
(
timer
,
function
(...)
timer
:
start
(
ms
,
0
,
function
()
timer
:
stop
()
lock
=
false
end
)
if
not
lock
then
lock
=
true
fn
(
...
)
end
end
)
end
---
Debounces a function on the trailing edge.
---
@param
ms
integer Timeout in ms
---
@param
rush_first
boolean If the managed fn is called and it
'
s not recovering from a debounce: call the fn immediately.
---
@param
fn
function Function to debounce
---
@return
ManagedFn
# Debounced function.
function
M
.
debounce_trailing
(
ms
,
rush_first
,
fn
)
local
timer
=
assert
(
uv
.
new_timer
())
local
lock
=
false
local
debounced_fn
,
args
debounced_fn
=
wrap
(
timer
,
function
(...)
if
not
lock
and
rush_first
and
args
==
nil
then
lock
=
true
fn
(
...
)
else
args
=
utils
.
tbl_pack
(
...
)
end
timer
:
start
(
ms
,
0
,
function
()
lock
=
false
timer
:
stop
()
if
args
then
local
a
=
args
args
=
nil
fn
(
utils
.
tbl_unpack
(
a
))
end
end
)
end
)
return
debounced_fn
end
---
Throttles a function on the leading edge.
---
@param
ms
integer Timeout in ms
---
@param
fn
function Function to throttle
---
@return
ManagedFn
# throttled function.
function
M
.
throttle_leading
(
ms
,
fn
)
local
timer
=
assert
(
uv
.
new_timer
())
local
lock
=
false
return
wrap
(
timer
,
function
(...)
if
not
lock
then
timer
:
start
(
ms
,
0
,
function
()
lock
=
false
timer
:
stop
()
end
)
lock
=
true
fn
(
...
)
end
end
)
end
---
Throttles a function on the trailing edge.
---
@param
ms
integer Timeout in ms
---
@param
rush_first
boolean If the managed fn is called and it
'
s not recovering from a throttle: call the fn immediately.
---
@param
fn
function Function to throttle
---
@return
ManagedFn
# throttled function.
function
M
.
throttle_trailing
(
ms
,
rush_first
,
fn
)
local
timer
=
assert
(
uv
.
new_timer
())
local
lock
=
false
local
throttled_fn
,
args
throttled_fn
=
wrap
(
timer
,
function
(...)
if
lock
or
(
not
rush_first
and
args
==
nil
)
then
args
=
utils
.
tbl_pack
(
...
)
end
if
lock
then
return
end
lock
=
true
if
rush_first
then
fn
(
...
)
end
timer
:
start
(
ms
,
0
,
function
()
lock
=
false
if
args
then
local
a
=
args
args
=
nil
if
rush_first
then
throttled_fn
(
utils
.
tbl_unpack
(
a
))
else
fn
(
utils
.
tbl_unpack
(
a
))
end
end
end
)
end
)
return
throttled_fn
end
---
Throttle a function against a target framerate. The function will always be
---
called when the editor is unlocked and writing to buffers is possible.
---
@param
framerate
integer
# Target framerate. Set to <= 0 to render whenever the scheduler is ready.
---
@param
fn
function
function
M
.
throttle_render
(
framerate
,
fn
)
local
lock
=
false
local
use_framerate
=
framerate
>
0
local
period
=
use_framerate
and
(
1000
/
framerate
)
*
1E6
or
0
local
throttled_fn
local
args
,
last
throttled_fn
=
async
.
void
(
function
(...)
args
=
utils
.
tbl_pack
(
...
)
if
lock
then
return
end
lock
=
true
await
(
async
.
schedule_now
())
fn
(
utils
.
tbl_unpack
(
args
))
args
=
nil
if
use_framerate
then
local
now
=
uv
.
hrtime
()
if
last
and
now
-
last
<
period
then
local
wait
=
period
-
(
now
-
last
)
await
(
async
.
timeout
(
wait
/
1E6
))
last
=
last
+
period
else
last
=
now
end
end
lock
=
false
if
args
~=
nil
then
throttled_fn
(
utils
.
tbl_unpack
(
args
))
end
end
)
return
throttled_fn
end
---
Repeatedly call `func` with a fixed time delay.
---
@param
func
function
---
@param
delay
integer
# Delay between executions (ms)
---
@return
Closeable
function
M
.
set_interval
(
func
,
delay
)
local
timer
=
assert
(
uv
.
new_timer
())
local
ret
=
{
close
=
function
()
timer
:
stop
()
M
.
try_close
(
timer
)
end
,
}
timer
:
start
(
delay
,
delay
,
function
()
local
should_close
=
func
()
if
type
(
should_close
)
==
"
boolean
"
and
should_close
then
ret
.
close
()
end
end
)
return
ret
end
---
Call `func` after a fixed time delay.
---
@param
func
function
---
@param
delay
integer
# Delay until execution (ms)
---
@return
Closeable
function
M
.
set_timeout
(
func
,
delay
)
local
timer
=
assert
(
uv
.
new_timer
())
local
ret
=
{
close
=
function
()
timer
:
stop
()
M
.
try_close
(
timer
)
end
,
}
timer
:
start
(
delay
,
0
,
function
()
func
()
ret
.
close
()
end
)
return
ret
end
return
M
Back
|
FazBrowse Home
|
New Git URL