|
| 1 | +-- Credit: https://gist.github.com/runiq/31aa5c4bf00f8e0843cd267880117201 |
| 2 | +-- |
| 3 | + |
| 4 | +local M = {} |
| 5 | + |
| 6 | +---Validates args for `throttle()` and `debounce()`. |
| 7 | +local function td_validate(fn, ms) |
| 8 | + vim.validate{ |
| 9 | + fn = { fn, 'f' }, |
| 10 | + ms = { |
| 11 | + ms, |
| 12 | + function(ms) |
| 13 | + return type(ms) == 'number' and ms > 0 |
| 14 | + end, |
| 15 | + "number > 0", |
| 16 | + }, |
| 17 | + } |
| 18 | +end |
| 19 | + |
| 20 | +--- Throttles a function on the leading edge. Automatically `schedule_wrap()`s. |
| 21 | +--- |
| 22 | +--@param fn (function) Function to throttle |
| 23 | +--@param timeout (number) Timeout in ms |
| 24 | +--@returns (function, timer) throttled function and timer. Remember to call |
| 25 | +---`timer:close()` at the end or you will leak memory! |
| 26 | +function M.throttle_leading(fn, ms) |
| 27 | + td_validate(fn, ms) |
| 28 | + local timer = vim.loop.new_timer() |
| 29 | + local running = false |
| 30 | + |
| 31 | + local function wrapped_fn(...) |
| 32 | + if not running then |
| 33 | + timer:start(ms, 0, function() |
| 34 | + running = false |
| 35 | + end) |
| 36 | + running = true |
| 37 | + pcall(vim.schedule_wrap(fn), select(1, ...)) |
| 38 | + end |
| 39 | + end |
| 40 | + return wrapped_fn, timer |
| 41 | +end |
| 42 | + |
| 43 | +--- Throttles a function on the trailing edge. Automatically |
| 44 | +--- `schedule_wrap()`s. |
| 45 | +--- |
| 46 | +--@param fn (function) Function to throttle |
| 47 | +--@param timeout (number) Timeout in ms |
| 48 | +--@param last (boolean, optional) Whether to use the arguments of the last |
| 49 | +---call to `fn` within the timeframe. Default: Use arguments of the first call. |
| 50 | +--@returns (function, timer) Throttled function and timer. Remember to call |
| 51 | +---`timer:close()` at the end or you will leak memory! |
| 52 | +function M.throttle_trailing(fn, ms, last) |
| 53 | + td_validate(fn, ms) |
| 54 | + local timer = vim.loop.new_timer() |
| 55 | + local running = false |
| 56 | + |
| 57 | + local wrapped_fn |
| 58 | + if not last then |
| 59 | + function wrapped_fn(...) |
| 60 | + if not running then |
| 61 | + local argv = {...} |
| 62 | + local argc = select('#', ...) |
| 63 | + |
| 64 | + timer:start(ms, 0, function() |
| 65 | + running = false |
| 66 | + pcall(vim.schedule_wrap(fn), unpack(argv, 1, argc)) |
| 67 | + end) |
| 68 | + running = true |
| 69 | + end |
| 70 | + end |
| 71 | + else |
| 72 | + local argv, argc |
| 73 | + function wrapped_fn(...) |
| 74 | + argv = {...} |
| 75 | + argc = select('#', ...) |
| 76 | + |
| 77 | + if not running then |
| 78 | + timer:start(ms, 0, function() |
| 79 | + running = false |
| 80 | + pcall(vim.schedule_wrap(fn), unpack(argv, 1, argc)) |
| 81 | + end) |
| 82 | + running = true |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | + return wrapped_fn, timer |
| 87 | +end |
| 88 | + |
| 89 | +--- Debounces a function on the leading edge. Automatically `schedule_wrap()`s. |
| 90 | +--- |
| 91 | +--@param fn (function) Function to debounce |
| 92 | +--@param timeout (number) Timeout in ms |
| 93 | +--@returns (function, timer) Debounced function and timer. Remember to call |
| 94 | +---`timer:close()` at the end or you will leak memory! |
| 95 | +function M.debounce_leading(fn, ms) |
| 96 | + td_validate(fn, ms) |
| 97 | + local timer = vim.loop.new_timer() |
| 98 | + local running = false |
| 99 | + |
| 100 | + local function wrapped_fn(...) |
| 101 | + timer:start(ms, 0, function() |
| 102 | + running = false |
| 103 | + end) |
| 104 | + |
| 105 | + if not running then |
| 106 | + running = true |
| 107 | + pcall(vim.schedule_wrap(fn), select(1, ...)) |
| 108 | + end |
| 109 | + end |
| 110 | + return wrapped_fn, timer |
| 111 | +end |
| 112 | + |
| 113 | +--- Debounces a function on the trailing edge. Automatically |
| 114 | +--- `schedule_wrap()`s. |
| 115 | +--- |
| 116 | +--@param fn (function) Function to debounce |
| 117 | +--@param timeout (number) Timeout in ms |
| 118 | +--@param first (boolean, optional) Whether to use the arguments of the first |
| 119 | +---call to `fn` within the timeframe. Default: Use arguments of the last call. |
| 120 | +--@returns (function, timer) Debounced function and timer. Remember to call |
| 121 | +---`timer:close()` at the end or you will leak memory! |
| 122 | +function M.debounce_trailing(fn, ms, first) |
| 123 | + td_validate(fn, ms) |
| 124 | + local timer = vim.loop.new_timer() |
| 125 | + local wrapped_fn |
| 126 | + |
| 127 | + if not first then |
| 128 | + function wrapped_fn(...) |
| 129 | + local argv = {...} |
| 130 | + local argc = select('#', ...) |
| 131 | + |
| 132 | + timer:start(ms, 0, function() |
| 133 | + pcall(vim.schedule_wrap(fn), unpack(argv, 1, argc)) |
| 134 | + end) |
| 135 | + end |
| 136 | + else |
| 137 | + local argv, argc |
| 138 | + function wrapped_fn(...) |
| 139 | + argv = argv or {...} |
| 140 | + argc = argc or select('#', ...) |
| 141 | + |
| 142 | + timer:start(ms, 0, function() |
| 143 | + pcall(vim.schedule_wrap(fn), unpack(argv, 1, argc)) |
| 144 | + end) |
| 145 | + end |
| 146 | + end |
| 147 | + return wrapped_fn, timer |
| 148 | +end |
| 149 | + |
| 150 | +--- Test deferment methods (`{throttle,debounce}_{leading,trailing}()`). |
| 151 | +--- |
| 152 | +--@param bouncer (string) Bouncer function to test |
| 153 | +--@param ms (number, optional) Timeout in ms, default 2000. |
| 154 | +--@param firstlast (bool, optional) Whether to use the 'other' fn call |
| 155 | +---strategy. |
| 156 | +function M.test_defer(bouncer, ms, firstlast) |
| 157 | + local bouncers = { |
| 158 | + tl = M.throttle_leading, |
| 159 | + tt = M.throttle_trailing, |
| 160 | + dl = M.debounce_leading, |
| 161 | + dt = M.debounce_trailing, |
| 162 | + } |
| 163 | + |
| 164 | + local timeout = ms or 2000 |
| 165 | + |
| 166 | + local bounced = bouncers[bouncer]( |
| 167 | + function(i) vim.cmd('echom "' .. bouncer .. ': ' .. i .. '"') end, |
| 168 | + timeout, |
| 169 | + firstlast |
| 170 | + ) |
| 171 | + |
| 172 | + for i, _ in ipairs{1,2,3,4,5} do |
| 173 | + bounced(i) |
| 174 | + vim.schedule(function () vim.cmd('echom ' .. i) end) |
| 175 | + vim.fn.call("wait", {1000, "v:false"}) |
| 176 | + end |
| 177 | +end |
| 178 | + |
| 179 | +return M |
0 commit comments