-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEventListener.lua
More file actions
71 lines (65 loc) · 1.75 KB
/
Copy pathEventListener.lua
File metadata and controls
71 lines (65 loc) · 1.75 KB
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
EventListener = {}
-- events :: [Event -> IO ()]
-- events :: A list of functions that take an event as the parameter and
-- do something on that event.
function EventListener.init()
events = {
key = {},
char = {},
disk = {},
paste = {},
timer = {},
alarm = {},
redstone = {},
terminate = {},
disk_eject = {},
peripheral = {},
term_resize = {},
mouse_click = {},
mouse_scroll = {},
http_success = {},
http_failure = {},
modem_message = {},
monitor_touch = {},
monitor_resize = {},
rednet_message = {},
turtle_inventory = {},
peripheral_detach = {}
}
end
function EventListener.add(eventType, eventName, eventFunction)
if not events then
EventListener.init()
end
if events[eventType] then
table.insert(events[eventType], {name = eventName, func = eventFunction})
else
print("Invalid event type: " .. eventType)
end
end
function EventListener.remove(eventType, eventName)
for i, event in ipairs(events[eventType]) do
if event["name"] == eventName then
table.remove(events[eventType], i)
end
end
end
-- Recieves an os.pullEvent() and calls the event function on each function in
-- the eventType table
-- for example:
-- EventListener.add("redstone", "redstoneInput", function() print("rs in") end)
-- EventListener.runEvent(os.pullEvent())
-- This will only call events on the redstone table if a redstone event is
-- captured.
function EventListener.runEvent(event)
for _, functionTable in pairs(events[event[1]]) do
functionTable["func"](event)
end
end
function EventListener.updateLoop(updateFequency, stop, fun)
while not stop do
fun()
os.startTimer(updateFequency)
EventListener.runEvent({os.pullEvent()})
end
end