Skip to content

Commit

Permalink
defaults.lua: add an inactive parameter to timer constructors
Browse files Browse the repository at this point in the history
Added to the functions `mp.add_timeout` and `mp.add_periodic_timer`.

If the `inactive` argument is set to `true` or a truthy value, the
timer will wait to be manually started with a call to its `resume()`
method.
  • Loading branch information
myQwil committed Oct 8, 2023
1 parent acac614 commit e5cc258
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
8 changes: 6 additions & 2 deletions DOCS/man/lua.rst
Original file line number Diff line number Diff line change
Expand Up @@ -438,17 +438,21 @@ The ``mp`` module is preloaded, although it can be loaded manually with
that are equal to the ``fn`` parameter. This uses normal Lua ``==``
comparison, so be careful when dealing with closures.

``mp.add_timeout(seconds, fn)``
``mp.add_timeout(seconds, fn [, inactive])``
Call the given function fn when the given number of seconds has elapsed.
Note that the number of seconds can be fractional. For now, the timer's
resolution may be as low as 50 ms, although this will be improved in the
future.

If the ``inactive`` argument is set to ``true`` or a truthy value, the
timer will wait to be manually started with a call to its ``resume()``
method.

This is a one-shot timer: it will be removed when it's fired.

Returns a timer object. See ``mp.add_periodic_timer`` for details.

``mp.add_periodic_timer(seconds, fn)``
``mp.add_periodic_timer(seconds, fn [, inactive])``
Call the given function periodically. This is like ``mp.add_timeout``, but
the timer is re-added after the function fn is run.

Expand Down
10 changes: 6 additions & 4 deletions player/lua/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -265,20 +265,22 @@ local timers = {}
local timer_mt = {}
timer_mt.__index = timer_mt

function mp.add_timeout(seconds, cb)
local t = mp.add_periodic_timer(seconds, cb)
function mp.add_timeout(seconds, cb, inactive)
local t = mp.add_periodic_timer(seconds, cb, inactive)
t.oneshot = true
return t
end

function mp.add_periodic_timer(seconds, cb)
function mp.add_periodic_timer(seconds, cb, inactive)
local t = {
timeout = seconds,
cb = cb,
oneshot = false,
}
setmetatable(t, timer_mt)
t:resume()
if not inactive then
t:resume()
end
return t
end

Expand Down

0 comments on commit e5cc258

Please sign in to comment.