debounce(wait, simulation?) → tick, pushUse Debounce when an OpenMW event or UI control can change repeatedly but the expensive work only matters after it settles.
wait must be a number greater than zero. simulation must be absent or boolean: absent/false uses real time; true uses simulation time. Invalid arguments raise an assertion.
Player-script example
local debounce = require 'scripts.s3.debounce'
local tick, push = debounce(0.25, true)
return {
eventHandlers = {
MyModFilterChanged = function(filterText)
push(filterText)
end,
},
engineHandlers = {
onUpdate = function()
local settled, filterText = tick()
if settled then rebuildList(filterText) end
end,
},
}
Register this as a player script using the bootstrap instructions. Send the player's MyModFilterChanged event whenever the filter changes. A burst of events produces one rebuildList call on a later update after at least 0.25 simulation seconds without another push.
Return and lifetime contract
push(value)stores the value and restarts the interval. It does not invoke a callback.tick()returnsfalse, nilwhile idle or waiting.- After the quiet interval,
tick()returnstrue, valueonce and clears the pending value. nilis a valid pushed value; testfired, not the truthiness ofvalue.- Tables are retained by reference, not copied. Construction creates closures holding the pending state; the helper does not serialize it into save data.
Do not replace the second argument with a function: this API returns two functions rather than accepting a completion callback.
Need a regular interval or an immediately ready rate limit instead? Compare Every and Cooldown.