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 prefixedKey = `prefix${key}`;

        return { cacheInstance, key: prefixedKey };
    }
}

Above example is a hook for preGetItem event, and it will trigger handler for this event, which will alter (add prefix) passed key. From now on, such prefixed key will be used to store / get / remove items.

event

event is a name of the event for which given handler will be fired. For stash-it, events are: preMethodName and postMethodName

event’s name is constructed from prefix + method name, for instance preSetItem or postRemoveItem (mind the camel case name structure)

Why pre and post?

It’s about data control (before and after) of each method.

handler

handler is a function that takes certain arguments (in an object), does something with them and returns an object. 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.

Last updated