-
Notifications
You must be signed in to change notification settings - Fork 1
/
timer.lua
93 lines (82 loc) · 2 KB
/
timer.lua
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
Timer = {}
local function getpid()
local handle = io.popen("echo $PPID", "r")
local vispid = handle:read("l")
handle:close()
return vispid
end
local vispid = getpid()
Kicker = {
active = false,
references = 0,
handle = false,
timers = {},
pid = 0,
}
local function on_kick(w)
local time = os.time()
for i, timer in pairs(Kicker.timers) do
timer(time)
end
end
function Kicker:ref()
self.references = self.references + 1
if not self.active then
self.handle = io.popen("echo $$ && watch -n 1 -t 'kill -28 "..vispid..
"'", "r")
self.pid = self.handle:read("l")
self.active = true
local e = vis.events
e.subscribe(e.WIN_HIGHLIGHT, on_kick)
end
end
function Kicker:unref()
self.references = self.references - 1
if self.references < 1 and self.active then
local e = vis.events
e.unsubscribe(e.WIN_HIGHLIGHT, on_kick)
self.active = false
os.execute('kill -2 '..self.pid)
self.handle:close()
end
end
function Timer:periodic(action, interval)
if self.active then return "Timer is already active!" end
if not interval then interval = 1 end
local nexttime = os.time() + interval
self.active = true
Kicker.timers[self.id] = function (time)
if time >= nexttime then
nexttime = time + interval
action()
end
end
Kicker:ref()
end
function Timer:touchafter(action, interval)
if self.active then return "Timer is already active!" end
self.active = true
if not interval then interval = 1 end
local nexttime = os.time() + interval
Kicker.timers[self.id] = function (time)
if time >= nexttime then
Kicker:unref()
self.active = false
action()
Kicker.timers[self.id] = nil
end
end
Kicker:ref()
end
function Timer:cancel()
Kicker.timers[self.id] = nil
self.active = false
Kicker:unref()
collectgarbage()
end
Timer.new = function ()
local t = setmetatable({}, {__index=Timer})
t.id = tostring(os.time())..tostring(math.random())
t.active = false
return t
end