Skip to content

Event

Typed event definitions for the unstable event-log system.

An Event is the durable contract shared by writers, handlers, journals, and replicas. It gives an event a stable tag, derives the aggregate or entity primary key from the decoded payload, and records the schemas used for the payload, handler result, and handler errors. The payload schema is also used to derive the MessagePack encoding for journal entries and remote replication.

29 exports Added in v4.0.0 Source

Constructors

addError

Added in v4.0.0 Source

Adds another error schema to an event definition.

Details

The returned event keeps the same tag, primary key, payload, and success schema while replacing the error schema with a union of the existing and new errors.

Signature

declare function addError<A extends Any, Error2 extends Top>(
  event: A,
  error: Error2,
): AddError<A, Error2>;

make

Added in v4.0.0 Source

Creates an event log event definition.

Details

If omitted, the payload and success schemas default to Schema.Void, the error schema defaults to Schema.Never, and the MessagePack payload schema is derived from the payload schema.

Signature

declare function make<
  Tag extends string,
  Payload extends Top = Void,
  Success extends Top = Void,
  Error extends Top = Never,
>(options: {
  readonly error?: Error;
  readonly payload?: Payload;
  readonly primaryKey: (payload: Type<Payload>) => string;
  readonly success?: Success;
  readonly tag: Tag;
}): Event<Tag, Payload, Success, Error>;

Guards

isEvent

Added in v4.0.0 Source

Returns true when a value is an event log event definition.

Signature

declare function isEvent(u: unknown): u is Event<any, any, any, any>;

Models

Any interface

Added in v4.0.0 Source

Type-erased event log event definition.

Details

It preserves the runtime tag, primary-key function, payload schema, success schema, and error schema without retaining the original type parameters.

Signature

interface Any {
  readonly "~effect/eventlog/Event": "~effect/eventlog/Event";
  readonly error: Top;
  readonly payload: Top;
  readonly payloadMsgPack: schema<Top>;
  readonly primaryKey: (payload: any) => string;
  readonly success: Top;
  readonly tag: string;
}

AnyWithProps interface

Added in v4.0.0 Source

Type-erased event definition with its runtime properties available structurally.

Signature

interface AnyWithProps extends Any {}

Event interface

Added in v4.0.0 Source

Definition of an event type that can be written to an EventLog.

Details

An event definition contains its tag, primary-key function, payload schema, MessagePack payload schema, success schema, and error schema.

Signature

interface Event<
  out Tag extends string,
  in out Payload extends Schema.Top = typeof Schema.Void,
  in out Success extends Schema.Top = typeof Schema.Void,
  in out Error extends Schema.Top = typeof Schema.Never,
> {
  readonly "~effect/eventlog/Event": "~effect/eventlog/Event";
  readonly error: Error;
  readonly payload: Payload;
  readonly payloadMsgPack: schema<Payload>;
  readonly primaryKey: (payload: Type<Payload>) => string;
  readonly success: Success;
  readonly tag: Tag;
}

EventHandler interface

Added in v4.0.0 Source

Marker service associated with the handler for an event tag.

Details

ToService derives this service from an Event so handler layers can expose which events they implement.

Signature

interface EventHandler<in out Tag extends string> {
  readonly _: typeof _;
  readonly tag: Tag;
}

Type IDs

TypeId

Added in v4.0.0 Source

Runtime type identifier used to mark event log event definitions.

Signature

declare const TypeId: "~effect/eventlog/Event";

TypeId type

Added in v4.0.0 Source

Unique type identifier used to mark event log event definitions.

Signature

type TypeId = "~effect/eventlog/Event";

Utility Types

AddError type

Added in v4.0.0 Source

Returns an event definition type whose error schema also includes the provided error schema.

Signature

type AddError<A extends Any, Error extends Schema.Top> =
  A extends Event<infer _Tag, infer _Payload, infer _Success, infer _Error>
    ? Event<_Tag, _Payload, _Success, _Error | Error>
    : never;

Error type

Added in v4.0.0 Source

Decoded error value type for an event definition.

Signature

type Error<A extends Any> = Schema.Schema.Type<ErrorSchema<A>>;

ErrorSchema type

Added in v4.0.0 Source

Extracts the error schema from an event definition.

Signature

type ErrorSchema<A extends Any> =
  A extends Event<infer _Tag, infer _Payload, infer _Success, infer _Error> ? _Error : never;

ErrorWithTag type

Added in v4.0.0 Source

Decoded error value type for the event in a union with the specified tag.

Signature

type ErrorWithTag<Events extends Any, Tag extends string> = Error<WithTag<Events, Tag>>;

ExcludeTag type

Added in v4.0.0 Source

Removes event definitions with the specified tag from an event union.

Signature

type ExcludeTag<Events extends Any, Tag extends string> = Exclude<
  Events,
  {
    readonly tag: Tag;
  }
>;

Payload type

Added in v4.0.0 Source

Decoded payload value type for an event definition.

Signature

type Payload<A extends Any> = Schema.Schema.Type<PayloadSchema<A>>;

PayloadSchema type

Added in v4.0.0 Source

Extracts the payload schema from an event definition.

Signature

type PayloadSchema<A extends Any> =
  A extends Event<infer _Tag, infer _Payload, infer _Success, infer _Error> ? _Payload : never;

PayloadSchemaWithTag type

Added in v4.0.0 Source

Extracts the payload schema for the event in a union with the specified tag.

Signature

type PayloadSchemaWithTag<A extends Any, Tag extends string> =
  A extends Event<Tag, infer _Payload, infer _Success, infer _Error> ? _Payload : never;

PayloadWithTag type

Added in v4.0.0 Source

Decoded payload value type for the event in a union with the specified tag.

Signature

type PayloadWithTag<Events extends Any, Tag extends string> = Payload<WithTag<Events, Tag>>;

Services type

Added in v4.0.0 Source

All schema services required to encode and decode the payload, success, and error schemas for an event definition.

Signature

type Services<A> =
  A extends Event<infer _Tag, infer _Payload, infer _Success, infer _Error>
    ?
        | _Payload["DecodingServices"]
        | _Success["EncodingServices"]
        | _Error["EncodingServices"]
        | _Payload["EncodingServices"]
        | _Success["DecodingServices"]
        | _Error["DecodingServices"]
    : never;

ServicesClient type

Added in v4.0.0 Source

Schema services required by a client for an event definition.

Details

This includes payload encoding services plus success and error decoding services.

Signature

type ServicesClient<A> =
  A extends Event<infer _Tag, infer _Payload, infer _Success, infer _Error>
    ? _Payload["EncodingServices"] | _Success["DecodingServices"] | _Error["DecodingServices"]
    : never;

Client-side schema services required for the event in a union with the specified tag.

Signature

type ServicesClientWithTag<Events extends Any, Tag extends string> = ServicesClient<
  WithTag<Events, Tag>
>;

ServicesServer type

Added in v4.0.0 Source

Schema services required by a server for an event definition.

Details

This includes payload decoding services plus success and error encoding services.

Signature

type ServicesServer<A> =
  A extends Event<infer _Tag, infer _Payload, infer _Success, infer _Error>
    ? _Payload["DecodingServices"] | _Success["EncodingServices"] | _Error["EncodingServices"]
    : never;

Success type

Added in v4.0.0 Source

Decoded success value type for an event definition.

Signature

type Success<A extends Any> = Schema.Schema.Type<SuccessSchema<A>>;

SuccessSchema type

Added in v4.0.0 Source

Extracts the success schema from an event definition.

Signature

type SuccessSchema<A extends Any> =
  A extends Event<infer _Tag, infer _Payload, infer _Success, infer _Error> ? _Success : never;

SuccessWithTag type

Added in v4.0.0 Source

Decoded success value type for the event in a union with the specified tag.

Signature

type SuccessWithTag<Events extends Any, Tag extends string> = Success<WithTag<Events, Tag>>;

Tag type

Added in v4.0.0 Source

Extracts the tag string from an event definition.

Signature

type Tag<A> =
  A extends Event<infer _Tag, infer _Payload, infer _Success, infer _Error> ? _Tag : never;

TaggedPayload type

Added in v4.0.0 Source

Tagged payload value for an event definition.

Details

The result contains _tag set to the event tag and payload set to the decoded payload value.

Signature

type TaggedPayload<A extends Any> =
  A extends Event<infer _Tag, infer _Payload, infer _Success, infer _Error>
    ? {
        readonly _tag: _Tag;
        readonly payload: Schema.Schema.Type<_Payload>;
      }
    : never;

ToService type

Added in v4.0.0 Source

Derives the handler service marker for an event definition.

Signature

type ToService<A> =
  A extends Event<infer _Tag, infer _Payload, infer _Success, infer _Error>
    ? EventHandler<_Tag>
    : never;

WithTag type

Added in v4.0.0 Source

Extracts the event definition with the specified tag from an event union.

Signature

type WithTag<Events extends Any, Tag extends string> = Extract<
  Events,
  {
    readonly tag: Tag;
  }
>;