lsyncd/mantle/useralarm.lua

97 lines
1.2 KiB
Lua
Raw Normal View History

2018-03-19 07:50:29 +00:00
--
2018-03-30 13:15:49 +00:00
-- useralarm.lua from Lsyncd -- the Live (Mirror) Syncing Demon
2018-03-19 07:50:29 +00:00
--
--
2018-03-30 13:15:49 +00:00
-- Lets userscripts make their own alarms.
2018-03-19 07:50:29 +00:00
--
--
-- License: GPLv2 (see COPYING) or any later version
-- Authors: Axel Kittenberger <axkibe@gmail.com>
--
if mantle
then
print( 'Error, Lsyncd mantle already loaded' )
os.exit( -1 )
end
local alarms = { }
--
-- Calls the user function at timestamp.
--
local function alarm
(
timestamp,
func,
extra
)
local idx
for k, v in ipairs( alarms )
do
if timestamp < v.timestamp
then
idx = k
break
end
end
local a =
{
timestamp = timestamp,
func = func,
extra = extra
}
if idx
then
table.insert( alarms, idx, a )
else
table.insert( alarms, a )
end
end
--
-- Retrieves the soonest alarm.
--
local function getAlarm
( )
if #alarms == 0
then
return false
else
return alarms[1].timestamp
end
end
--
-- Calls user alarms.
--
local function invoke
(
timestamp
)
while #alarms > 0
and alarms[ 1 ].timestamp <= timestamp
do
alarms[ 1 ].func( alarms[ 1 ].timestamp, alarms[ 1 ].extra )
table.remove( alarms, 1 )
end
end
--
-- Exported interface.
--
UserAlarms =
{
alarm = alarm,
getAlarm = getAlarm,
invoke = invoke
}