Skip to content

Event Bus (HookSystem)

The HookSystem is the primary mechanism for cross-module communication in the Nexical Ecosystem. It allows modules to publish events and subscribe to actions without direct coupling.

HookSystem.dispatch(event, data, context?)

Section titled “HookSystem.dispatch(event, data, context?)”

Fire and forget event. Useful for asynchronous side effects (e.g., sending an email after registration).

import { HookSystem } from '@/lib/modules/hooks';
// Dispatching an event
await HookSystem.dispatch('user.registered', { userId: '123' });

Subscribe to an event. Handlers are executed in parallel when dispatched.

// Listening to an event
HookSystem.on('user.registered', async (data) => {
console.log(`User registered: ${data.userId}`);
});

Pass data through a chain of listeners, allowing each to modify it. Useful for plugins that need to enrich data or enforce policy.

// Applying filters
const enrichedUser = await HookSystem.filter('user.view', userFromDb);
// Registering a filter
HookSystem.on('user.view', (user) => {
return { ...user, canAccessPro: true };
});