This class is design as a client for events.
It ensure that events are sended to the Dispatcher or save in the Redis,
and execute the provided eventCallback when a event concern this client.
await initRedis();
await initRedis({}, "subscriber");
const AVAILABLE_EVENTS = Object.freeze<Record<keyof Events, EventSubscribe>>(
([...eventsValidationFn.keys()].map((name) => {
return {
name,
delay: undefined,
horizontalScale: undefined
};
})).reduce((prev, curr) => Object.assign(prev, { [curr.name]: curr }), {}) as Record<keyof Events, EventSubscribe>
);
const incomer = new Incomer({
name: "foo",
eventsCast: [...Object.keys(AVAILABLE_EVENTS)],
eventsSubscribe: [...Object.values(AVAILABLE_EVENTS)],
eventCallback: (event) => {
console.log(event);
}
});
await incomer.initialize();
await incomer.close();
type Prefix = "test" | "development" | "staging" | "production";
type GenericEvent = Record<string, any> & { data: Record<string, any> };
type EventCast<T extends string | keyof Events = string> = T;
type EventSubscribe<T extends string | keyof Events = string> = {
name: T;
delay?: number;
horizontalScale?: boolean;
};
type CallBackEventMessage<
T extends GenericEvent = GenericEvent
> = T & {
name: string;
};
type EventMessage<
T extends GenericEvent = GenericEvent
> = T & {
name: string;
redisMetadata: IncomerTransactionMetadata;
};
type IncomerOptions<T extends GenericEvent = GenericEvent> = {
/* Service name */
name: string;
logger?: Partial<Logger> & Pick<Logger, "info" | "warn">;
standardLog?: StandardLog<T>;
eventsCast: EventCast[];
eventsSubscribe: EventSubscribe[];
eventCallback: (message: CallBackEventMessage<T>) => void;
prefix?: Prefix;
abortPublishTime?: number;
externalsInitialized?: boolean;
};
logger
Default logger is a pino logger.
⚠️ You can inject your own but you must ensure that the provided logger has those methodsinfo
|error
|warn
.
standardLog
Callback function use to formate logs related to custom events casting.
function standardLog<T extends GenericEvent = EventOptions<keyof Events>>
(event: T & { redisMetadata: { transactionId: string } }) {
const logs = `foo: ${event.foo}`;
function log(message: string) {
return `(${logs}) ${message}`;
}
return log;
}
abortPublishTime
Interval of time during which the
incomer
instance is going to wait to for a response from thedispatcher
next to the registration demand or any event publishing.
If there is no recent activity from thedispatcher
, those events are not publish and saved in Redis awaiting for the next iteration.
externalsInitialized
Use to initialize
externals
class. Asfalse
and with aprefix
with the valuetest
ordevelopment
, it will init adispatcher
and anincomer
in order to run tests without any other accessible APIs.
publish< K extends GenericEvent | null = null >(event: K extends null ? Omit< EventMessage< T >, "redisMetadata" >): Promise
Publish the given event on Redis pubsub.
If there is no dispatcher alive, the event isn't publish but saved in Redis awaiting for an incoming publish.