Class Emit<TContext, TEmit, TEmitData>

Emit class facilitates the creation and management of typed events within a state machine. It enables events to carry strongly-typed names and data, validated via Zod schemas, and handled by a custom function. It is ideal for complex state management scenarios where events must adhere to specific data structures and behaviors.

Example usage:

const emitter = new Emit({
name: 'cmd.book.fetch',
schema: zod.object({
name: zod.string(),
age: zod.number(),
}),
handler: (id, state, snapshot) => ({
name: 'book_name_1',
age: 150
})
});
console.log(emitter.ref); // Outputs the unique ID of the emitter

Typeparam

TContext The context type of the state machine, typically a record or an object describing the state.

Typeparam

TEmit A string literal type that specifies the unique name of the event.

Typeparam

TEmitData The type defining the structure of the data associated with the event, enforced by a Zod schema.

Type Parameters

  • TContext extends Record<string, any>

  • TEmit extends string

  • TEmitData extends zod.ZodObject<Record<string, any>>

Constructors

Properties

Accessors

Methods

Constructors

  • Initializes a new instance of the Emit class configured for event handling.

    Type Parameters

    • TContext extends Record<string, any>

    • TEmit extends string

    • TEmitData extends ZodObject<Record<string, any>, UnknownKeysParam, ZodTypeAny, {}, {}>

    Parameters

    • params: {
          event: TEmit;
          handler: ((id, state, snapshot) => TypeOf<TEmitData>);
          name?: string;
          schema: TEmitData;
      }

      An object containing the necessary configurations:

      • name: [Optional] The name of the event handler, which is included in the unique identifier. By default, it is the same as the event
      • event: The name of the event emitted by the handler
      • schema: A Zod schema to validate the event's data structure.
      • handler: A function that processes the event using the provided state context and a snapshot of the state machine, returning data that matches the defined schema.

    Returns Emit<TContext, TEmit, TEmitData>

Properties

id: string
params: {
    event: TEmit;
    handler: ((id, state, snapshot) => TypeOf<TEmitData>);
    name?: string;
    schema: TEmitData;
}

An object containing the necessary configurations:

  • name: [Optional] The name of the event handler, which is included in the unique identifier. By default, it is the same as the event
  • event: The name of the event emitted by the handler
  • schema: A Zod schema to validate the event's data structure.
  • handler: A function that processes the event using the provided state context and a snapshot of the state machine, returning data that matches the defined schema.

Type declaration

Accessors

  • get handler(): OnOrchestrationStateEmit<TContext, TEmit>
  • Accesses the handler function for this event. This function encapsulates the event logic as defined during instantiation. It ensures the event data adheres to the schema before returning it, enhancing data integrity and error handling.

    Returns OnOrchestrationStateEmit<TContext, TEmit>

    A function that, when executed, processes the event using the defined handler, returning an object with 'type' and 'data' reflecting the event's outcome.

    Throws

    Throws an error if the data returned by the handler does not conform to the schema.

  • get ref(): string
  • Provides the unique identifier for this event emitter instance. This identifier is a composite of the event name and a UUID.

    Returns string

    A string that represents the unique identifier of this instance.

Methods

  • Creates a new Emit instance with a new unique identifier while maintaining the same configuration. This method is useful when the event configuration needs to be reused with a new identity, ensuring that event handling remains consistent across different instances.

    Type Parameters

    • TContext extends Record<string, any>

    • TEmit extends string

    • TEmitData extends ZodObject<any, UnknownKeysParam, ZodTypeAny, {}, {}>

    Parameters

    Returns Emit<TContext, TEmit, TEmitData>

    A new Emit instance with identical configuration but a different unique identifier.

Generated using TypeDoc