forked from Grymskvll/Postal-Returned
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.lua
150 lines (112 loc) · 2.63 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
Postal.control = {}
local event_listeners = {}
local update_listeners = {}
local function listener_filter(listener)
return not listener.deleted
end
function Postal.control.on_event()
for listener, _ in pairs(event_listeners) do
if event == listener.event and not listener.deleted then
listener.action()
end
end
end
function Postal.control.on_update()
Postal.util.set_filter(event_listeners, listener_filter)
Postal.util.set_filter(update_listeners, listener_filter)
for listener, _ in pairs(update_listeners) do
if not listener.deleted then
listener.action()
end
end
end
function Postal.control.event_listener(event, action)
local self = {}
local listener = { event=event, action=action }
function self.set_action(action)
listener.action = action
end
function self.start()
Postal.util.set_add(event_listeners, listener)
if not Postal.util.any(event_listeners, function(l) return l.event == event end) then
PostalControlFrame:RegisterEvent(event)
end
return self
end
function self.stop()
listener.deleted = true
if not Postal.util.any(event_listeners, function(l) return l.event == event end) then
PostalControlFrame:UnregisterEvent(event)
end
return self
end
return self
end
function Postal.control.update_listener(action)
local self = {}
local listener = { action=action }
function self.set_action(action)
listener.action = action
end
function self.start()
Postal.util.set_add(update_listeners, listener)
return self
end
function self.stop()
listener.deleted = true
return self
end
return self
end
function Postal.control.on_next_update(callback)
local listener = Postal.control.update_listener()
listener.set_action(function()
listener:stop()
return callback()
end)
listener.start()
end
function Postal.control.on_next_event(event, callback)
local listener = Postal.control.event_listener(event)
listener.set_action(function()
listener:stop()
return callback()
end)
listener.start()
end
function Postal.control.as_soon_as(p, callback)
local listener = Postal.control.update_listener()
listener.set_action(function()
if p() then
listener.stop()
return callback()
end
end)
listener.start()
end
function Postal.control.controller()
local self = {}
local state
local listener = Postal.control.update_listener()
listener.set_action(function()
if state and state.p() then
local k = state.k
state = nil
return k()
end
end)
listener.start()
function self.wait(p, k)
state = {
k = k,
p = p,
}
end
function self.reset()
state = nil
end
function self.cleanup()
listener.stop()
end
return self
end