-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEventManager.ttslua
executable file
·100 lines (73 loc) · 2.96 KB
/
EventManager.ttslua
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
require('ge_tts.License')
local TableUtils = require('ge_tts.TableUtils')
local Logger = require('ge_tts.Logger')
---@type table<string, any>
local EVENT_DEFAULT_RETURN_VALUES = {
filterObjectEnterContainer = true,
}
---@type table<string, function[]>
local eventHandlers = {}
local globalHandlers = --[[---@type {[string]: nil | function}]] _G
---@param event string
local function listen(event)
local previousGlobalHandler = globalHandlers[event]
;(--[[---@type table]] _G)[event] = function(...)
local handlers = TableUtils.copy(eventHandlers[event]) -- Copied in case we add/remove handlers during a handler callback
---@type std__Packed<any>
local finalResult = --[[---@type std__Packed<any>]] {n = 0}
for _, handler in ipairs(handlers) do
local result = table.pack(handler(...))
if result.n > 0 then
finalResult = result
end
end
if finalResult.n > 0 then
return table.unpack(finalResult, 1, finalResult.n)
else
local defaultValue = EVENT_DEFAULT_RETURN_VALUES[event]
if defaultValue ~= nil then
return defaultValue
end
end
end
---@type function[]
local handlers = {}
eventHandlers[event] = handlers
Logger.log('EventManager now listening for ' .. event, Logger.VERBOSE)
if previousGlobalHandler then
table.insert(handlers, --[[---@not nil]] previousGlobalHandler)
Logger.log('Pre-existing global ' .. event .. ' handler preserved as the first handler', Logger.VERBOSE)
end
return handlers
end
local SAVE_MANAGER_EVENTS = {'onSave', 'onLoad'}
---@class ge_tts__EventManager
local EventManager = {}
---@param event string @Event name
---@param handler function @Function that will be called when the event fires. Parameters vary depending on the event.
function EventManager.addHandler(event, handler)
assert(not TableUtils.find(SAVE_MANAGER_EVENTS, event), 'EventManager cannot handle ' .. event .. '. Please use SaveManager instead.')
local handlers = eventHandlers[event] or listen(event)
if not TableUtils.find(handlers, handler) then
table.insert(handlers, handler)
end
end
---@param event string @Event name
---@param handler function @A previously registered handler that you wish to remove.
function EventManager.removeHandler(event, handler)
assert(not TableUtils.find(SAVE_MANAGER_EVENTS, event), 'EventManager cannot handle ' .. event .. '. Please use SaveManager instead.')
local handlers = eventHandlers[event]
local handlerIndex = handlers and TableUtils.find(handlers, handler)
if handlerIndex then
table.remove(handlers, --[[---@not nil]] handlerIndex)
end
end
---@param event string @Event name
---@vararg any
function EventManager.triggerEvent(event, ...)
local handler = globalHandlers[event]
if handler then
(--[[---@not nil]] handler)(...)
end
end
return EventManager