Initializes a new instance of the Emit
class configured for event handling.
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 eventevent
: The name of the event emitted by the handlerschema
: 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.Optional
name?: stringPrivate
idPrivate
paramsAn 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 eventevent
: The name of the event emitted by the handlerschema
: 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.Optional
name?: stringAccesses 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.
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 an error if the data returned by the handler does not conform to the schema.
Provides the unique identifier for this event emitter instance. This identifier is a composite of the event name and a UUID.
A string that represents the unique identifier of this instance.
Static
copyCreates 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.
A new Emit
instance with identical configuration but a different unique identifier.
Generated using TypeDoc
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:
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.