The function signature that listeners must match
Event emitter with register, invoke, invokeAsync, and clear methods
// Create emitter for specific function signature
const onUserLogin = OnFunc<(userId: string, timestamp: Date) => void>();
// Register listeners (returns unregister function)
const unsubscribe1 = onUserLogin((userId, timestamp) => {
console.log(`User ${userId} logged in at ${timestamp}`);
});
const unsubscribe2 = onUserLogin((userId) => {
trackAnalytics('login', userId);
});
// Trigger all listeners
onUserLogin.invoke('user123', new Date());
// Async invocation
await onUserLogin.invokeAsync('user456', new Date());
// Unregister specific listener
unsubscribe1();
// Clear all listeners
onUserLogin.clear();
Creates a type-safe event emitter for a specific function signature.
OnFunc provides a simple pub/sub pattern where multiple listeners can be registered for a specific function signature. All listeners are invoked when the event is triggered, with errors caught and ignored. Supports both synchronous and asynchronous invocation.