H3lp Yours3lfDocs
api

Debounce

Emit the latest value after a quiet interval, with caller-driven polling.

debounce(wait, simulation?) → tick, push

Use 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() returns false, nil while idle or waiting.
  • After the quiet interval, tick() returns true, value once and clears the pending value.
  • nil is a valid pushed value; test fired, not the truthiness of value.
  • 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.