Skip to main content

intercept

Registers an interceptor that fires before the store's default handler for a given action. Returning false from the interceptor cancels the action and prevents it from reaching any downstream handlers. Use this to validate, mutate, or block action payloads before they take effect.

Usage

api.intercept(
action: keyof StoreActions,
handler: (data: StoreActions[action]) => void | boolean | Promise<boolean>,
config?: { tag?: string | number | symbol }
): void;
ParameterTypeDescription
actionkeyof StoreActionsAction name to intercept (e.g. "add-card", "move-card")
handler(data) => void | boolean | Promise<boolean>Interceptor function. Return false to cancel the action. Modify data properties in place to alter the payload before it reaches the store.
config{ tag? }Optional. Assign a tag so the interceptor can be removed later with detach.

Example

Block deletion of cards marked as locked:

api.intercept("delete-card", data => {
const state = api.getState();
const card = state.cards.byId(data.id);
if (card?.locked) return false;
});

Mutate the payload before the store processes it:

api.intercept("add-card", data => {
data.card.created = new Date();
});

Use a tag to remove the interceptor later:

api.intercept("move-card", handler, { tag: "move-guard" });
// later
api.detach("move-guard");
  • on - post-handler that fires after the store processes an action
  • detach - remove an interceptor by tag
  • exec - dispatch an action through the event bus