forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepeat-util.lua
More file actions
46 lines (39 loc) · 912 Bytes
/
Copy pathrepeat-util.lua
File metadata and controls
46 lines (39 loc) · 912 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
-- lua/plugins/repeatUtil.lua
-- author expwnent
-- tools for registering callbacks periodically
-- vaguely based on a script by Putnam
local _ENV = mkmodule("repeat-util")
repeating = repeating or {}
dfhack.onStateChange.repeatUtilStateChange = function(code)
if code == SC_WORLD_UNLOADED then
repeating = {}
end
end
function cancel(name)
if not repeating[name] then
return false
end
if repeating[name] ~= -1 then
dfhack.timeout_active(repeating[name],nil)
end
repeating[name] = nil
return true
end
function scheduleEvery(name,time,timeUnits,func)
cancel(name)
local function helper()
func()
if repeating[name] then
repeating[name] = dfhack.timeout(time,timeUnits,helper)
end
end
repeating[name] = -1
helper()
end
function scheduleUnlessAlreadyScheduled(name,time,timeUnits,func)
if repeating[name] then
return
end
scheduleEvery(name,time,timeUnits,func)
end
return _ENV