Back to Home

ESO Lua File v101041

libraries/zo_mostrecenteventhandler/zo_mostrecenteventhandler.lua

[◄ back to folders ]
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
ZO_MostRecentEventHandler = ZO_Object:Subclass()
function ZO_MostRecentEventHandler:New(...)
    local object = ZO_Object.New(self)
    object:Initialize(...)
    return object
end
--equalityFunction: determines if a new event should replace and old event with new info
--handlerFunction: executes code for the most recent events
function ZO_MostRecentEventHandler:Initialize(namespace, event, equalityFunction, handlerFunction)
    self.namespace = namespace
    self.event = event
    self.events = {}
    EVENT_MANAGER:RegisterForEvent(namespace, event, function(_, ...) self:OnEvent(...) end)
    EVENT_MANAGER:RegisterForUpdate(namespace, 0, function() self:OnUpdate() end)
end
function ZO_MostRecentEventHandler:AddFilterForEvent(...)
    EVENT_MANAGER:AddFilterForEvent(self.namespace, self.event, ...)
end
function ZO_MostRecentEventHandler:OnEvent(...)
    for _, eventInfo in ipairs(self.events) do
        if self.equalityFunction(eventInfo, ...) then
            local numEventFields = select("#", ...)
            for fieldIndex = 1, numEventFields do
                eventInfo[fieldIndex] = select(fieldIndex, ...)
            end
            return
        end
    end
    table.insert(self.events, { ... })
end
function ZO_MostRecentEventHandler:OnUpdate()
    for i, eventInfo in ipairs(self.events) do
        self.handlerFunction(unpack(eventInfo))
    end
end