feat(api): add public API module (#1432)
This commit is contained in:
@@ -4,7 +4,7 @@ local M = {}
|
||||
|
||||
local global_handlers = {}
|
||||
|
||||
local Event = {
|
||||
M.Event = {
|
||||
Ready = "Ready",
|
||||
NodeRenamed = "NodeRenamed",
|
||||
TreeOpen = "TreeOpen",
|
||||
@@ -20,7 +20,7 @@ local function get_handlers(event_name)
|
||||
return global_handlers[event_name] or {}
|
||||
end
|
||||
|
||||
local function register_handler(event_name, handler)
|
||||
function M.subscribe(event_name, handler)
|
||||
local handlers = get_handlers(event_name)
|
||||
table.insert(handlers, handler)
|
||||
global_handlers[event_name] = handlers
|
||||
@@ -37,107 +37,92 @@ end
|
||||
|
||||
--@private
|
||||
function M._dispatch_ready()
|
||||
dispatch(Event.Ready)
|
||||
dispatch(M.Event.Ready)
|
||||
end
|
||||
|
||||
--@private
|
||||
function M._dispatch_node_renamed(old_name, new_name)
|
||||
dispatch(Event.NodeRenamed, { old_name = old_name, new_name = new_name })
|
||||
dispatch(M.Event.NodeRenamed, { old_name = old_name, new_name = new_name })
|
||||
end
|
||||
|
||||
--@private
|
||||
function M._dispatch_file_removed(fname)
|
||||
dispatch(Event.FileRemoved, { fname = fname })
|
||||
dispatch(M.Event.FileRemoved, { fname = fname })
|
||||
end
|
||||
|
||||
--@private
|
||||
function M._dispatch_file_created(fname)
|
||||
dispatch(Event.FileCreated, { fname = fname })
|
||||
dispatch(M.Event.FileCreated, { fname = fname })
|
||||
end
|
||||
|
||||
--@private
|
||||
function M._dispatch_folder_created(folder_name)
|
||||
dispatch(Event.FolderCreated, { folder_name = folder_name })
|
||||
dispatch(M.Event.FolderCreated, { folder_name = folder_name })
|
||||
end
|
||||
|
||||
--@private
|
||||
function M._dispatch_folder_removed(folder_name)
|
||||
dispatch(Event.FolderRemoved, { folder_name = folder_name })
|
||||
dispatch(M.Event.FolderRemoved, { folder_name = folder_name })
|
||||
end
|
||||
|
||||
--@private
|
||||
function M._dispatch_on_tree_open()
|
||||
dispatch(Event.TreeOpen, nil)
|
||||
dispatch(M.Event.TreeOpen, nil)
|
||||
end
|
||||
|
||||
--@private
|
||||
function M._dispatch_on_tree_close()
|
||||
dispatch(Event.TreeClose, nil)
|
||||
dispatch(M.Event.TreeClose, nil)
|
||||
end
|
||||
|
||||
--@private
|
||||
function M._dispatch_on_tree_resize(size)
|
||||
dispatch(Event.Resize, size)
|
||||
dispatch(M.Event.Resize, size)
|
||||
end
|
||||
|
||||
--Registers a handler for the Ready event.
|
||||
--@param handler (function) Handler with the signature `function()`
|
||||
--- @deprecated
|
||||
function M.on_nvim_tree_ready(handler)
|
||||
register_handler(Event.Ready, handler)
|
||||
M.subscribe(M.Event.Ready, handler)
|
||||
end
|
||||
|
||||
--Registers a handler for the NodeRenamed event.
|
||||
--@param handler (function) Handler with the signature function(payload), where payload is a table containing:
|
||||
-- - old_name (string) Absolute path to the old node location.
|
||||
-- - new_name (string) Absolute path to the new node location.
|
||||
--- @deprecated
|
||||
function M.on_node_renamed(handler)
|
||||
register_handler(Event.NodeRenamed, handler)
|
||||
M.subscribe(M.Event.NodeRenamed, handler)
|
||||
end
|
||||
|
||||
--Registers a handler for the FileCreated event.
|
||||
--@param handler (function) Handler with the signature function(payload), where payload is a table containing:
|
||||
-- - fname (string) Absolute path to the created file.
|
||||
--- @deprecated
|
||||
function M.on_file_created(handler)
|
||||
register_handler(Event.FileCreated, handler)
|
||||
M.subscribe(M.Event.FileCreated, handler)
|
||||
end
|
||||
|
||||
--Registers a handler for the FileRemoved event.
|
||||
--@param handler (function) Handler with the signature function(payload), where payload is a table containing:
|
||||
-- - fname (string) Absolute path to the removed file.
|
||||
--- @deprecated
|
||||
function M.on_file_removed(handler)
|
||||
register_handler(Event.FileRemoved, handler)
|
||||
M.subscribe(M.Event.FileRemoved, handler)
|
||||
end
|
||||
|
||||
--Registers a handler for the FolderCreated event.
|
||||
--@param handler (function) Handler with the signature function(payload), where payload is a table containing:
|
||||
-- - folder_name (string) Absolute path to the created folder.
|
||||
--- @deprecated
|
||||
function M.on_folder_created(handler)
|
||||
register_handler(Event.FolderCreated, handler)
|
||||
M.subscribe(M.Event.FolderCreated, handler)
|
||||
end
|
||||
|
||||
--Registers a handler for the FolderRemoved event.
|
||||
--@param handler (function) Handler with the signature function(payload), where payload is a table containing:
|
||||
-- - folder_name (string) Absolute path to the removed folder.
|
||||
--- @deprecated
|
||||
function M.on_folder_removed(handler)
|
||||
register_handler(Event.FolderRemoved, handler)
|
||||
M.subscribe(M.Event.FolderRemoved, handler)
|
||||
end
|
||||
|
||||
--Registers a handler for the TreeOpen event.
|
||||
--@param handler (function) Handler with the signature function(payload)
|
||||
--- @deprecated
|
||||
function M.on_tree_open(handler)
|
||||
register_handler(Event.TreeOpen, handler)
|
||||
M.subscribe(M.Event.TreeOpen, handler)
|
||||
end
|
||||
|
||||
--Registers a handler for the TreeClose event.
|
||||
--@param handler (function) Handler with the signature function(payload)
|
||||
--- @deprecated
|
||||
function M.on_tree_close(handler)
|
||||
register_handler(Event.TreeClose, handler)
|
||||
M.subscribe(M.Event.TreeClose, handler)
|
||||
end
|
||||
|
||||
--Registers a handler for the Resize event.
|
||||
--@param handler (function) Handler with the signature function(size)
|
||||
--- @deprecated
|
||||
function M.on_tree_resize(handler)
|
||||
register_handler(Event.Resize, handler)
|
||||
M.subscribe(M.Event.Resize, handler)
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Reference in New Issue
Block a user