Skip to content
New issue

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

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

Already on GitHub? # to your account

refactor(events): use vim.defaulttable in EventManager #886

Merged
Merged
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
14 changes: 7 additions & 7 deletions lua/orgmode/events/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ local Listeners = require('orgmode.events.listeners')
---@field private _listeners table<string, fun(...:any)[]>
local EventManager = {
initialized = false,
_listeners = {},
_listeners = vim.defaulttable(function()
return {}
end),
event = Events,
}

Expand All @@ -22,17 +24,15 @@ end
---@param event OrgEvent
---@param listener fun(...)
function EventManager.listen(event, listener)
if not EventManager._listeners[event.type] then
EventManager._listeners[event.type] = {}
end
if not vim.tbl_contains(EventManager._listeners[event.type], listener) then
table.insert(EventManager._listeners[event.type], listener)
local listeners = EventManager._listeners[event.type]
if not vim.tbl_contains(listeners, listener) then
table.insert(listeners, listener)
end
end

function EventManager.init()
if EventManager.initialized then
return
return EventManager
end
for event, listeners in pairs(Listeners) do
for _, listener in ipairs(listeners) do
Expand Down
Loading