Hooks
hooks
is an array of objects. Single object contains event
and handler
properties. For example:
{
event: 'preGetItem',
handler: ({ cacheInstance, key }) => {
// do something with key for example
const alteredKey = ...
return alteredKey;
}
}
Above example is a hook for preGetItem
event (name), and it will trigger handler
for this event, which will alter somehow passed key. Once that's done, getting an item from storage will be somewhat different.
event
event
is a name of the event for which given handler will be fired. For stash-it, events are preMethodName
and postMethodName
tl;dr: event’s name is a prefix + method name, for instance
preSetItem
orpostRemoveItem
(mind the camel case name structure).
Why pre and post?
It’s about data control throughout the lifecycle of each method.
handler
handler
is a function that takes certain arguments and does something with them. Most of the time it should return something. But not always. If, for example, some plugin should throw when certain criteria are met, then this handler will throw, not return a value.
What arguments are passed to each handler?
Depending on method for which you create the handler for, you will get different arguments. All of them are described in detail in cacheInstance's API.
What about 3rd party plugins and arguments for their methods (and events).
It solely depends on those plugins. You need to read their docs in order to find out.