Skip to content

Latest commit

 

History

History
144 lines (106 loc) · 3.55 KB

incomer.md

File metadata and controls

144 lines (106 loc) · 3.55 KB

Incomer

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.

📚 Usage

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();

Types

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;
};

Options

logger

Default logger is a pino logger.
⚠️ You can inject your own but you must ensure that the provided logger has those methods info | 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 the dispatcher next to the registration demand or any event publishing.
If there is no recent activity from the dispatcher, those events are not publish and saved in Redis awaiting for the next iteration.


externalsInitialized

Use to initialize externals class. As false and with a prefix with the value test or development, it will init a dispatcher and an incomer in order to run tests without any other accessible APIs.

API

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.