UNPKG

lever-ui-eventbus

Version:

A minimal TypeScript event bus: subscribe(Class, handler), async delivery, dead events, and polymorphic dispatch.

235 lines (231 loc) 6.52 kB
// Generated React integration types for lever-ui-eventbus import { ReactNode, DependencyList } from 'react'; import type { EventBus, Constructor } from './index'; // Context types /** * Props for the EventBusProvider component. */ export interface EventBusProviderProps { /** The EventBus instance to provide to child components */ bus: EventBus; /** Child components that will have access to the event bus */ children: ReactNode; } /** * Provider component that makes an EventBus available to all child components. * * @example * ```tsx * const bus = new EventBus(); * * function App() { * return ( * <EventBusProvider bus={bus}> * <UserComponent /> * <OrderComponent /> * </EventBusProvider> * ); * } * ``` */ export declare function EventBusProvider({ bus, children }: EventBusProviderProps): /** * Hook to access the EventBus from the context. * Must be used within an EventBusProvider. * * @returns The EventBus instance from the nearest provider * @throws Error if used outside of an EventBusProvider * * @example * ```tsx * function MyComponent() { * const bus = useEventBus(); * * const handleClick = () => { * bus.post(new ButtonClicked('my-button')); * }; * * return <button onClick={handleClick}>Click me</button>; * } * ``` */ export declare function useEventBus(): EventBus; //# sourceMappingURL=context.d.ts.map // Hooks types /** * Hook that subscribes to events of a specific type and automatically cleans up on unmount. * * @template T The event type to subscribe to * @param type The constructor/class of events to listen for * @param handler Function to call when events are received * @param deps Optional dependency array - when changed, will resubscribe * * @example * ```tsx * function UserComponent() { * const [userId, setUserId] = useState<string | null>(null); * * useEventSubscription(UserLoggedIn, (event) => { * setUserId(event.userId); * }); * * useEventSubscription(UserLoggedOut, () => { * setUserId(null); * }); * * return <div>User: {userId || 'Not logged in'}</div>; * } * ``` */ export declare function useEventSubscription<T>(type: Constructor<T>, handler: (event: T) => void, deps?: DependencyList): void; /** * Hook that provides a stable function to post events to the event bus. * * @returns Function to post events * * @example * ```tsx * function LoginButton() { * const postEvent = useEventPost(); * * const handleLogin = async () => { * const success = await login(); * if (success) { * postEvent(new UserLoggedIn(userId)); * } * }; * * return <button onClick={handleLogin}>Login</button>; * } * ``` */ export declare function useEventPost(): <T>(event: T) => number; /** * Hook that maintains reactive state based on events. * State is updated whenever the specified event type is received. * * @template T The event type to listen for * @template S The state type * @param type The constructor/class of events to listen for * @param initialValue Initial state value * @param reducer Function that updates state based on received events * * @example * ```tsx * function UserProfile() { * const user = useEventState( * UserUpdated, * null, * (currentUser, event) => event.user * ); * * return <div>{user ? user.name : 'Loading...'}</div>; * } * ``` */ export declare function useEventState<T, S>(type: Constructor<T>, initialValue: S, reducer: (currentState: S, event: T) => S): S; /** * Hook that tracks the latest event of a specific type. * * @template T The event type to track * @param type The constructor/class of events to track * @param initialValue Initial value (default: null) * * @example * ```tsx * function NotificationDisplay() { * const lastNotification = useLatestEvent(NotificationShown, null); * * if (!lastNotification) return null; * * return ( * <div className="notification"> * {lastNotification.message} * </div> * ); * } * ``` */ export declare function useLatestEvent<T>(type: Constructor<T>, initialValue?: T | null): T | null; /** * Hook that collects events into an array with optional size limit. * * @template T The event type to collect * @param type The constructor/class of events to collect * @param maxSize Maximum number of events to keep (default: 100) * * @example * ```tsx * function AuditLog() { * const actions = useEventCollection(UserAction, 50); * * return ( * <ul> * {actions.map((action, i) => ( * <li key={i}>{action.description}</li> * ))} * </ul> * ); * } * ``` */ export declare function useEventCollection<T>(type: Constructor<T>, maxSize?: number): T[]; /** * Hook that provides event bus management utilities. * * @returns Object with management functions and statistics * * @example * ```tsx * function DebugPanel() { * const { subscriptionCount, activeTypes, clear } = useEventBusManager(); * * return ( * <div> * <p>Active event types: {activeTypes.length}</p> * <p>Total subscriptions: {subscriptionCount}</p> * <button onClick={clear}>Clear All</button> * </div> * ); * } * ``` */ export declare function useEventBusManager(): { activeTypes: Constructor[]; subscriptionCount: number; clear: () => void; refresh: () => void; getSubscriptionCount: (type: Constructor) => number; unsubscribeAll: (type: Constructor) => number; }; //# sourceMappingURL=hooks.d.ts.map // Explicit exports export declare function EventBusProvider(props: EventBusProviderProps): JSX.Element; export declare function useEventBus(): EventBus; export declare function useEventSubscription<T>( type: Constructor<T>, handler: (event: T) => void, deps?: DependencyList ): void; export declare function useEventPost(): <T>(event: T) => number; export declare function useEventState<T, S>( type: Constructor<T>, initialValue: S, reducer: (currentState: S, event: T) => S ): S; export declare function useLatestEvent<T>( type: Constructor<T>, initialValue?: T | null ): T | null; export declare function useEventCollection<T>( type: Constructor<T>, maxSize?: number ): T[]; export declare function useEventBusManager(): { activeTypes: Constructor[]; subscriptionCount: number; clear: () => void; refresh: () => void; getSubscriptionCount: (type: Constructor) => number; unsubscribeAll: (type: Constructor) => number; };