-
Notifications
You must be signed in to change notification settings - Fork 8
/
control.lua
123 lines (107 loc) · 2.24 KB
/
control.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
module 'aux'
local event_frame = CreateFrame'Frame'
local listeners, threads = T, T
local thread_id
function M.get_thread_id() return thread_id end
function LOAD()
event_frame:SetScript('OnUpdate', UPDATE)
event_frame:SetScript('OnEvent', EVENT)
end
function EVENT()
for id, listener in pairs(listeners) do
if listener.killed then
listeners[id] = nil
elseif event == listener.event then
listener.cb(listener.kill)
end
end
end
do
function UPDATE()
for _, listener in pairs(listeners) do
local event, needed = listener.event, false
for _, listener in pairs(listeners) do
needed = needed or listener.event == event and not listener.killed
end
if not needed then
event_frame:UnregisterEvent(event)
end
end
for id, thread in pairs(threads) do
if thread.killed or not thread.k then
threads[id] = nil
else
local k = thread.k
thread.k = nil
thread_id = id
k()
thread_id = nil
end
end
end
end
do
local id = 0
function get_unique_id()
id = id + 1
return id
end
end
function M.kill_listener(listener_id)
local listener = listeners[listener_id]
if listener then
listener.killed = true
end
end
function M.kill_thread(thread_id)
local thread = threads[thread_id]
if thread then
thread.killed = true
end
end
function M.event_listener(event, cb)
local listener_id = unique_id
listeners[listener_id] = O(
'event', event,
'cb', cb,
'kill', vararg-function(arg)
if arg.n == 0 or arg[1] then
kill_listener(listener_id)
end
end
)
event_frame:RegisterEvent(event)
return listener_id
end
function M.on_next_event(event, callback)
event_listener(event, function(kill) callback(); kill() end)
end
do
local mt = {
__call = function(self)
temp(self)
return self.f(unpack(self))
end,
}
M.thread = vararg-function(arg)
static(arg)
arg.f = tremove(arg, 1)
local thread_id = unique_id
threads[thread_id] = O('k', setmetatable(arg, mt))
return thread_id
end
M.wait = vararg-function(arg)
static(arg)
arg.f = tremove(arg, 1)
threads[thread_id].k = setmetatable(arg, mt)
end
end
M.when = vararg-function(arg)
local c = tremove(arg, 1)
local k = tremove(arg, 1)
if c() then
return k(unpack(arg))
else
return wait(when, c, k, unpack(arg))
end
end