Skip to content

Added setup event manager class #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions LucasTerra.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7075,4 +7075,46 @@ function usetrainer()
return false
end


SetupEventManager = { __class = 'SetupEventManager' }
SetupEventManagerMT = { __index = SetupEventManager }

-- @name SetupEventManager.new
-- @desc Creates new instance responsible for handling setup events.
-- @returns SetupEventManager
function SetupEventManager.new()
local newObj = {
events = {}
}

setmetatable(newObj, SetupEventManagerMT)
return newObj
end

-- @name SetupEventManager:register
-- @desc Registers new event handler for a specified user options item with name 'id'. If the event is triggered it will execute given callback.
-- @param id The user option item name.
-- @param callback Function to be called.
-- @returns callback
function SetupEventManager:register(id, callback)
self.events[id] = callback

local opt = getuseroption(id)
if opt then
return callback(opt)
end
end

-- @name SetupEventManager:handle
-- @desc Handles event given.
-- @param e An event to be handled.
-- @returns callback
function SetupEventManager:handle(e)
for id, callback in pairs(self.events) do
if e.name == id then
return callback(e.value)
end
end
end

printf('Lucas Terra Library Version: %s', LIBS.LUCAS)