-
Notifications
You must be signed in to change notification settings - Fork 4
API Reference
Asynchronously triggers an event, runs interceptors and middlewares, and executes all handlers with a chosen strategy.
async fire<K extends keyof E>(
event: K,
payload: E[K],
options?: FlareFireOptions
): Promise<string | void>-
event(K extends keyof E) Event name, constrained by your event mapE. -
payload(E[K]) Data associated with the event. The type is inferred fromE[event]. -
options(FlareFireOptions, optional) Controls execution strategy and error handling:-
strategy?: FlareFireStrategy→Parallel(default) orSequential. -
timeout?: number→ max time per handler (ms). -
haltOnError?: boolean→ stop on error (true), or continue (false, default).
-
-
Resolves to
voidif executed normally. -
Returns a message (
string) if:- event was stopped by middleware, or
- no handlers were found.
- Before interceptors → may stop event early.
- Middlewares → can mutate payload or cancel event.
- Handlers → executed in parallel or sequential order.
- After interceptors → always run after handlers.
// Fire event in parallel (default)
await fire("userLogin", { id: "u1" });
// Sequential execution with error halting
await fire("dataSync", { items: [1, 2, 3] }, {
strategy: FlareFireStrategy.Sequential,
haltOnError: true,
});
// With timeout
await fire("processJob", { jobId: "123" }, { timeout: 5000 });Registers a handler for an event. Optionally, the handler can run only once.
catch<K extends keyof E>(
event: K,
handler: FlareHandler<E[K]>,
options?: FlareCatchOptions
): () => void-
event(K extends keyof E) Event name, must exist in yourEevent map. -
handler((payload: E[K]) => void | Promise<void>) Function called when the event is fired. Can be synchronous or asynchronous. -
options(FlareCatchOptions, optional) Optional settings for handler registration:-
once?: boolean→ iftrue, the handler is automatically removed after the first execution.
-
- Returns a release function: calling it unregisters this handler from the event.
- If
once: trueis set, the handler is wrapped internally so it automatically unregisters after the first call. - Multiple handlers for the same event are supported — they are stored in a
Setto prevent duplicates. - Handlers can be removed anytime using the release function or the
release(event, handler)API.
// Register a normal handler
const releaseHandler = flare.catch("login", (payload) => {
console.log(`User logged in: ${payload.name}`);
});
// Register a handler that runs only once
flare.catch("signup", (payload) => {
console.log(`New signup: ${payload.name}`);
}, { once: true });
// Later, manually unregister a handler
releaseHandler();- Use
once: truefor event handlers that should only run once, e.g., onboarding steps. - Keep track of release functions to unregister handlers if they are no longer needed to avoid memory leaks.
- Multiple handlers can coexist for the same event; order of execution is not guaranteed.
Removes a specific handler from an event.
release<K extends keyof E>(
event: K,
handler: FlareHandler<E[K]>
): void-
event(K extends keyof E) The event name the handler was registered for. -
handler(FlareHandler<E[K]>) The exact handler function you want to remove. It must match the one passed tocatch.
- Deletes the handler from the event’s internal handler set.
- If the handler is not registered, nothing happens.
- Use this to clean up specific handlers without affecting others.
const handler = (payload: { name: string }) => console.log(payload.name);
// Register handler
flare.catch("login", handler);
// Remove handler manually
flare.release("login", handler);- Keep references to handler functions if you plan to release them manually.
- Prefer using the release function returned by
catchfor convenience.
Removes all handlers for all events.
releaseAll(): void- Clears all handlers across all events.
- Effectively resets the
Flareinstance’s handler storage. - Does not remove interceptors or middlewares.
// Clear all registered handlers
flare.releaseAll();- Useful for full cleanup before destroying a
Flareinstance or when resetting state. - Ensures no memory leaks from lingering handlers.
Registers a middleware to run before handlers of an event. Middlewares can modify the payload, stop the event, or perform side effects like logging.
use(middleware: FlareMiddleware<E>): void(context: {
event: K;
payload: E[K];
stop: () => void;
set: (newPayload: E[K]) => void;
}, next: () => Promise<void>) => void | Promise<void>-
context.event– the event name. -
context.payload– current payload for this event. -
context.stop()– stop event propagation; handlers will not run. -
context.set(newPayload)– replace the payload before handlers run. -
next()– call to continue to the next middleware or handlers.
- Middlewares are executed in registration order.
- If
stop()is called,fireresolves early with a cancellation message. - Can mutate the payload with
set(); handlers receive the new payload. - Works with async functions —
firewaits for all middlewares to complete before running handlers.
// Transform payload before handlers
flare.use(({ payload, set, next }) => {
set({ ...payload, name: payload.name.toUpperCase() });
return next();
});
// Stop event if condition fails
flare.use(({ payload, stop, next }) => {
if (!payload.userId) stop();
return next();
});- Use middlewares for validation, logging, or payload transformation.
- Keep middlewares small and composable.
- Middlewares cannot access handlers directly — they operate before handlers.
Registers an interceptor for an event. Interceptors wrap the event lifecycle and run before and/or after handlers.
in(interceptor: FlareInterceptor<E>): voidinterface FlareInterceptor<E> {
id?: string;
before?(event: keyof E, payload: E[keyof E]): boolean | void;
after?(event: keyof E, payload: E[keyof E]): void;
}-
before(event, payload)– runs before handlers.- Return
falseto cancel the event.
- Return
-
after(event, payload)– runs after all handlers have executed. -
id– optional identifier for logging or debugging.
-
beforeinterceptors run in registration order. - If any
beforereturnsfalse,fireresolves early with a cancellation message. -
afterinterceptors run after all handlers, regardless ofstrategy. - Does not modify payload unless combined with middleware.
// Log before and after events
flare.in({
id: "logger",
before(event, payload) {
console.log("Before", event, payload);
},
after(event, payload) {
console.log("After", event, payload);
}
});
// Cancel event if user is missing
flare.in({
before(event, payload) {
if (!payload.user) return false;
}
});- Use interceptors for cross-cutting concerns like logging, analytics, authentication.
- Prefer interceptors over middlewares if the logic is not related to payload transformation.
- Interceptors cannot prevent other interceptors from running — only
beforecan cancel the event.