From 7a8e997f12642f32e277fb8b9e1033c200cd97ad Mon Sep 17 00:00:00 2001 From: troiganto Date: Sat, 25 Jan 2025 22:26:23 +0100 Subject: [PATCH 1/2] fix(events): harmonize return values from EventManager.init() --- lua/orgmode/events/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/orgmode/events/init.lua b/lua/orgmode/events/init.lua index 8bb8cfa48..047825612 100644 --- a/lua/orgmode/events/init.lua +++ b/lua/orgmode/events/init.lua @@ -32,7 +32,7 @@ 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 From d287b8d734c6cbd9987c789334e2e1ad06dc472c Mon Sep 17 00:00:00 2001 From: troiganto Date: Sat, 25 Jan 2025 22:24:43 +0100 Subject: [PATCH 2/2] refactor(events): use vim.defaulttable() --- lua/orgmode/events/init.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lua/orgmode/events/init.lua b/lua/orgmode/events/init.lua index 047825612..b80ea0d52 100644 --- a/lua/orgmode/events/init.lua +++ b/lua/orgmode/events/init.lua @@ -6,7 +6,9 @@ local Listeners = require('orgmode.events.listeners') ---@field private _listeners table local EventManager = { initialized = false, - _listeners = {}, + _listeners = vim.defaulttable(function() + return {} + end), event = Events, } @@ -22,11 +24,9 @@ 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