Plugin API and Hook System
Define a plugin API table and register hooks for lifecycle events.
What Is a Hook System?
A hook system allows plugins to register callbacks for specific application lifecycle events. The app calls all registered hooks at the appropriate time.
The Application Object
The app object is passed to plugins during init. It exposes the hook registration API.
local App = {}
App.__index = App
App._hooks = {}
function App:on(event, fn)
self._hooks[event] = self._hooks[event] or {}
table.insert(self._hooks[event], fn)
end
function App:emit(event, ...)
for _, fn in ipairs(self._hooks[event] or {}) do
fn(...)
end
endAll lessons in this course
- Plugin Discovery and Loading
- Plugin API and Hook System
- Sandboxing Plugin Execution
- Versioning and Dependency Management