Skip to content

Commit

Permalink
feat(events): make event istances independent
Browse files Browse the repository at this point in the history
  • Loading branch information
danielefongo committed Apr 24, 2024
1 parent 8eb58fd commit 5b2cdc5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 24 deletions.
4 changes: 2 additions & 2 deletions lua/microscope/api/error.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 15 additions & 2 deletions lua/microscope/events.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
local uv = vim.loop
local events = {}
events.gen = 0
events.__index = events
events.event = {
resize = "VimResized",
buf_leave = "BufLeave",
Expand Down Expand Up @@ -72,16 +74,25 @@ 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

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
Expand All @@ -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 = {}

Expand Down
18 changes: 9 additions & 9 deletions lua/microscope/finder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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({
Expand All @@ -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()
Expand Down Expand Up @@ -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 })
Expand All @@ -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,
})
Expand Down Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions lua/microscope/ui/results.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {}
Expand All @@ -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

Expand Down
16 changes: 8 additions & 8 deletions lua/tests/ui/preview_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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" })
Expand All @@ -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(), { "" })
Expand All @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit 5b2cdc5

Please sign in to comment.