Hooks
Lifecycle hooks let you run code before or after every action and handle errors centrally.
beforeAction
Runs before each action. Return false to cancel the action.
const attractive = Attractive.activate();
attractive.beforeAction(({ name, element, options, event }) => {
if (!element.classList.contains("enabled")) {
return false;
}
});
afterAction
Runs after each successful action.
attractive.afterAction(({ name, element, options, event, result }) => {
console.log(`Action ${name} completed`);
});
onError
Runs when an action throws an error.
attractive.onError(({ name, element, options, event, error }) => {
console.warn(`Action ${name} failed:`, error.message);
});
Global onError
Set a fallback for all instances:
Attractive.onError = (error, message, detail) => {
ErrorTracker.captureException(error, { extra: detail });
};
By default it logs to console and delegates to window.onerror.
Error propagation
When an action throws, Attractive catches it and forwards to two layers:
- Instance
onErrorhooks (per-component UI feedback) - Global
Attractive.onErrorfallback (monitoring services like Appsignal, Honeybadger)
The error is contained. No unhandled rejection. Remaining actions in a chain still run.