Skip to content

Commit

Permalink
feat: emit User autocmd events in addition to hooks (#33)
Browse files Browse the repository at this point in the history
* feat: emit `ResssionLoadPost` User event on loading a session

* feat: emit `User` autocmd for more functions

* docs: update docs

* feat: emit `User` autocommands on hook dispatch

* feat: add `post_detach` hook and autocmd

* docs: update docs

* feat: include data in detach event payload

* feat: add additional options to detach method

* refactor: use `remove_tabpage_session` to remove tabpage sessions

* docs: add `target_tabpage` parameter

* fix: always clear tab session when leaving tab

* Revert "refactor: use `remove_tabpage_session` to remove tabpage sessions"

This reverts commit 1747ca2.

* fix: issues with retrieving name in tab_sessions

* docs: fix types

* Revert "fix: always clear tab session when leaving tab"

This reverts commit 4581337.

* throw errors if no session is found to detach from

* add `hook_to_event` mapping and check `has_setup` before applying event hooks

* remove unecessary delay parameter for `event` function

* move detach hook into separate pr

* remove hardcoded "Resession" prefix from `event` util method
  • Loading branch information
Subjective authored Sep 20, 2023
1 parent d1831b3 commit 959256c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lua/resession/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,27 @@ local hooks = setmetatable({
error(string.format('Unrecognized hook "%s"', key))
end,
})
local hook_to_event = {
pre_load = "ResessionLoadPre",
post_load = "ResessionLoadPost",
pre_save = "ResessionSavePre",
post_save = "ResessionSavePost",
}

local function do_setup()
if pending_config then
local conf = pending_config
pending_config = nil
require("resession.config").setup(conf)
has_setup = true

if not has_setup then
for hook, _ in pairs(hooks) do
M.add_hook(hook, function()
require("resession.util").event(hook_to_event[hook])
end)
end
has_setup = true
end
end
end

Expand Down
9 changes: 9 additions & 0 deletions lua/resession/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,13 @@ M.shorten_path = function(path)
end
end

--- Trigger a `User` event
---@param event string The event name to be emitted
M.event = function(event)
local emit_event = function()
vim.api.nvim_exec_autocmds("User", { pattern = event, modeline = false })
end
vim.schedule(emit_event)
end

return M

0 comments on commit 959256c

Please sign in to comment.