Created
April 25, 2023 02:07
-
-
Save Acen/73ec7e2a132b10de7d242bdb8b3c198a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.options = { | |
priorities = { | |
low = { | |
minimumTickLength = 1000, | |
}, | |
medium = { | |
minimumTickLength = 500, | |
}, | |
high = { | |
minimumTickLength = 250, | |
}, | |
} | |
} | |
self.eventCache = {} | |
local function fire(eventName, eventArguments) | |
if(eventArguments ~= nil and #eventArguments > 0) then | |
QueueEvent(eventName, unpack(eventArguments)) | |
else | |
QueueEvent(eventName, "") | |
end | |
end | |
local function validateEventArguments(eventArguments) | |
-- check event arguments table values are strings | |
for _, v in pairs(eventArguments) do | |
if(type(v) ~= 'string') then | |
logError('Event arguments must be strings') | |
return false | |
end | |
end | |
return true | |
end | |
function self.dispatch(eventName, eventArguments, priority) | |
if(not validateEventArguments(eventArguments)) then | |
return | |
end | |
if(priority == nil) then | |
priority = 'low' | |
end | |
local priorityTickLength = self.options.priorities[priority].minimumTickLength | |
local currentTick = Now() | |
if(self.eventCache[eventName] == nil) then | |
self.eventCache[eventName] = { | |
lastTick = currentTick, | |
arguments = eventArguments, | |
} | |
fire(eventName, eventArguments) | |
else | |
local eventCache = self.eventCache[eventName] | |
-- Last time this event was fired, was more than the minimum tick length ago | |
if(eventCache.lastTick + priorityTickLength < currentTick) then | |
eventCache.lastTick = currentTick | |
eventCache.arguments = eventArguments | |
fire(eventName, eventArguments) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment