@busly/events
Tiny NgRx-shaped event creators (
eventGroup,type). No dependencies.
For a walkthrough of how to use these creators, see Define events.
Install
pnpm add @busly/eventsThe package has no dependencies and adds about 200 bytes, minified and gzipped.
eventGroup
function eventGroup<Source extends string, Events extends Record<string, unknown>>(config: {
source: Source;
events: Events;
}): { readonly [Name in keyof Events]: EventCreator<`[${Source}] ${Name}`, Events[Name]> };Creates one creator for each key in events. Every creator's type has the form [source] eventName, and TypeScript keeps it as a literal type.
import { eventGroup, type } from '@busly/events';
export const counterPageEvents = eventGroup({
source: 'Counter Page',
events: {
increment: type<void>(),
set: type<number>(),
loadSuccess: type<{ count: number }>(),
},
});
counterPageEvents.set(42); // { type: '[Counter Page] set', payload: 42 }
counterPageEvents.set.type; // '[Counter Page] set'
counterPageEvents.increment(); // { type: '[Counter Page] increment', payload: undefined }type
function type<T>(): T;Captures a payload type for an event definition. At runtime it returns undefined, and the value is never used.
Types
type EventInstance<Type extends string, Payload> = {
type: Type;
payload: Payload;
};
type EventCreator<Type extends string, Payload> = ((payload: Payload) => EventInstance<Type, Payload>) & {
type: Type;
};Compatibility
These creators are interchangeable with the ones from @ngrx/signals/events. Every Busly adapter matches creators by their shape (the IBusEventCreator type from @busly/core), so you can use either library, or write your own creators by hand.
Credits
Adapted from NgRx Signals (@ngrx/signals and @ngrx/signals/events), MIT License: https://github.com/ngrx/platform. The functions were duplicated to keep the bundle small.