From 5b2cdc599222231b3e1ed45a7b0eee327b20f444 Mon Sep 17 00:00:00 2001 From: danielefongo Date: Wed, 24 Apr 2024 20:40:32 +0200 Subject: [PATCH] feat(events): make event istances independent --- lua/microscope/api/error.lua | 4 ++-- lua/microscope/events.lua | 17 +++++++++++++++-- lua/microscope/finder.lua | 18 +++++++++--------- lua/microscope/ui/results.lua | 6 +++--- lua/tests/ui/preview_spec.lua | 16 ++++++++-------- 5 files changed, 37 insertions(+), 24 deletions(-) diff --git a/lua/microscope/api/error.lua b/lua/microscope/api/error.lua index 0bca044..4e2e96c 100644 --- a/lua/microscope/api/error.lua +++ b/lua/microscope/api/error.lua @@ -2,11 +2,11 @@ local events = require("microscope.events") local error = {} function error.generic(message) - events.fire(events.global, events.event.error, { message = message, critical = false }) + events.global:fire(events.event.error, { message = message, critical = false }) end function error.critical(message) - events.fire(events.global, events.event.error, { message = message, critical = true }) + events.global:fire(events.event.error, { message = message, critical = true }) end function error.show(data) diff --git a/lua/microscope/events.lua b/lua/microscope/events.lua index f481ade..bcc15df 100644 --- a/lua/microscope/events.lua +++ b/lua/microscope/events.lua @@ -1,5 +1,7 @@ local uv = vim.loop local events = {} +events.gen = 0 +events.__index = events events.event = { resize = "VimResized", buf_leave = "BufLeave", @@ -72,9 +74,17 @@ function events:clear_module(module) self.handlers[module] = nil end +function events:clear_all() + for module, _ in pairs(self.handlers or {}) do + self:clear_module(module) + end + self.handlers = {} +end + function events:fire(evt, data, delay) self:cancel(evt) self.timers[evt] = vim.defer_fn(function() + self.timers[evt] = nil vim.api.nvim_exec_autocmds("User", { group = self.group, pattern = evt, data = data }) end, delay or 0) end @@ -82,6 +92,7 @@ end function events:fire_native(evt, delay) self:cancel(evt) self.timers[evt] = vim.defer_fn(function() + self.timers[evt] = nil vim.api.nvim_exec_autocmds(evt, { group = self.group }) end, delay or 0) end @@ -96,9 +107,11 @@ function events:cancel(evt) end function events.new() - local self = setmetatable(events, { __index = events }) + local self = setmetatable({}, events) + + events.gen = events.gen + 1 - self.group = vim.api.nvim_create_augroup("Microscope", { clear = false }) + self.group = vim.api.nvim_create_augroup("Microscope" .. tostring(events.gen), { clear = false }) self.timers = {} self.handlers = {} diff --git a/lua/microscope/finder.lua b/lua/microscope/finder.lua index bbaee61..daa0716 100644 --- a/lua/microscope/finder.lua +++ b/lua/microscope/finder.lua @@ -24,8 +24,8 @@ function finder:bind_action(fun) end function finder:close() - events:clear_module(self) - events.clear_module(events.global, self) + self.events:clear_all() + events.global:clear_all() resume_old_position(self) @@ -62,12 +62,12 @@ function finder:search(text) buf = self.old_buf, win = self.old_win, } - events:fire(events.event.new_request, self.request) + self.events:fire(events.event.new_request, self.request) self.scope:search(self.request, self.opts.args) end function finder:update() - events:clear(self, events.event.win_leave) + self.events:clear(self, events.event.win_leave) if not self.opts.hidden then local layout = self.opts.layout({ @@ -81,7 +81,7 @@ function finder:update() self.results:show(layout.results, layout.input == nil) self.input:show(layout.input, true) - events:native(self, events.event.win_leave, finder.close) + self.events:native(self, events.event.win_leave, finder.close) else self.preview:show() self.results:show() @@ -132,7 +132,7 @@ function finder:set_opts(opts) self.opts = opts self.request = self.request or nil - events:fire(events.event.new_opts, self.opts) + self.events:fire(events.event.new_opts, self.opts) for lhs, action in pairs(self.opts.bindings) do vim.keymap.set("i", lhs, self:bind_action(action), { buffer = self.input.buf }) @@ -144,9 +144,9 @@ function finder:set_opts(opts) lens = opts.lens, callback = function(list) if #list > 0 then - events:fire(events.event.results_retrieved, list) + self.events:fire(events.event.results_retrieved, list) else - events:fire(events.event.empty_results_retrieved) + self.events:fire(events.event.empty_results_retrieved) end end, }) @@ -176,7 +176,7 @@ function finder.new(opts) self.events:on(self, events.event.results_opened, finder.open) self.events:on(self, events.event.input_changed, finder.search) - self.events.on(events.global, self, events.event.error, finder.close_with_err) + events.global:on(self, events.event.error, finder.close_with_err) self.events:native(self, events.event.resize, finder.update) self.events:on(self, events.event.win_leave, finder.close) diff --git a/lua/microscope/ui/results.lua b/lua/microscope/ui/results.lua index 680637a..0e86b30 100644 --- a/lua/microscope/ui/results.lua +++ b/lua/microscope/ui/results.lua @@ -29,7 +29,7 @@ local function get_focused(self) end local function on_empty_results_retrieved(self) - events:cancel(events.event.result_focused) + self.events:cancel(events.event.result_focused) self:set_title("", "center") self:clear() end @@ -136,7 +136,7 @@ function results:open(metadata) local selected = self:selected() if #selected > 0 then - events:fire(events.event.results_opened, { selected = selected, metadata = metadata }) + self.events:fire(events.event.results_opened, { selected = selected, metadata = metadata }) end self.selected_data = {} @@ -148,7 +148,7 @@ function results:set_cursor(cursor) local idx, focused = get_focused(self) if focused then self:set_title(idx .. " / " .. #self.results, "center") - events:fire(events.event.result_focused, focused, 100) + self.events:fire(events.event.result_focused, focused, 100) end end diff --git a/lua/tests/ui/preview_spec.lua b/lua/tests/ui/preview_spec.lua index 5b6ace6..183a2c9 100644 --- a/lua/tests/ui/preview_spec.lua +++ b/lua/tests/ui/preview_spec.lua @@ -23,12 +23,12 @@ describe("preview", function() it("calls the preview function", function() local preview_fn = helpers.spy_function() - events:fire(events.event.new_opts, { + my_events:fire(events.event.new_opts, { preview = function(data, _) preview_fn(data) end, }) - events:fire(events.event.result_focused, { text = "smth" }) + my_events:fire(events.event.result_focused, { text = "smth" }) helpers.wait(10) assert.spy(preview_fn).was.called_with({ text = "smth" }) @@ -41,7 +41,7 @@ describe("preview", function() assert.are.same(preview_window:read(), { "some", "text" }) - events:fire(events.event.empty_results_retrieved) + my_events:fire(events.event.empty_results_retrieved) helpers.wait(10) assert.are.same(preview_window:read(), { "" }) @@ -52,10 +52,10 @@ describe("preview", function() it("refresh window if the function is changed", function() local preview_fn = helpers.spy_function() - events:fire(events.event.result_focused, { text = "smth" }) + my_events:fire(events.event.result_focused, { text = "smth" }) helpers.wait(10) - events:fire(events.event.new_opts, { + my_events:fire(events.event.new_opts, { preview = function(data, _) preview_fn(data) end, @@ -71,13 +71,13 @@ describe("preview", function() preview_fn(data) end - events:fire(events.event.result_focused, { text = "smth" }) + my_events:fire(events.event.result_focused, { text = "smth" }) helpers.wait(10) - events:fire(events.event.new_opts, { preview = fun }) + my_events:fire(events.event.new_opts, { preview = fun }) helpers.wait(10) - events:fire(events.event.new_opts, { preview = fun }) + my_events:fire(events.event.new_opts, { preview = fun }) helpers.wait(10) assert.spy(preview_fn).was.called(1)