At the core of katnip are plugins that listen to different events. When you think of events, think of the standard EventTarget
available in the browser dom. In katnip, there are various event targets, or "event channels" instantiated at different phases
of the project. The core phases are:
- The CLI phase. This is when we run katnip from the command line.
- The server phase. This is when the server is up and running.
In the different phases, different events are dispatched. These are the events dispatched by default. Plugins can add and dispatch other events in different situations as they see fit.
Rather than using the standard EventTarget
class, katnip uses its own class called HookRunner
. The difference is:
Async
The listeners registered for HookRunner
are async
functions.
Listener priority
Listeners added to HookRunner
can have a priority deciding the order in which the listeners should be run. Setting
the priority of a listener is done by setting the priority
on the function added as event listener. For example:
fetch.priority=5;
export function fetch(ev) {
}
This function is listening to the fetch
event with priority 5. The default priority is 10, so setting a priority of 5 will register
a hook that runs before the "normal" tasks for that event.
Implicit adding of listener modules
When adding event listeners to an EventTarget
, we frequently need to call the addEventListener
function, like this:
function clickListener(ev) {
}
myElement.addEventListener("click",clickListener);
In katnip we generally don't need to do this. Instead, a plugin exports a function, and the system will look at the name of the exported function to determine what event it should listen to.
Return values
When using the DOM EventTarget
the event listeners generally don't return anything. In katnip event listeners can
return a value, with the effect that the event will stop its propagation. For example, if a listener of the fetch
events
returns anything, this will determine the response of the request, and subsequent listeners will not be called.
Events during the CLI phase
build
Builds the project.
dev
Starts a development server.
deploy
Deploys the project.
Events during the server phase
start
Dispatched at the start of the server.
fetch
This is the core of the server operation. Turns a Request into a
Response. The event passed has request
as a property, so
the event is similar to the FetchEvent used by the service worker
API.