Skip to content

Event.ts

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.

Since v4.0.0



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 const addError: <A extends Any, Error2 extends Schema.Top>(event: A, error: Error2) => AddError<A, Error2>

Source

Since v4.0.0

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 const make: <
Tag extends string,
Payload extends Schema.Top = Schema.Void,
Success extends Schema.Top = Schema.Void,
Error extends Schema.Top = Schema.Never
>(options: {
readonly tag: Tag
readonly primaryKey: (payload: Schema.Schema.Type<Payload>) => string
readonly payload?: Payload | undefined
readonly success?: Success | undefined
readonly error?: Error | undefined
}) => Event<Tag, Payload, Success, Error>

Source

Since v4.0.0

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

Signature

declare const isEvent: (u: unknown) => u is Event<any, any, any, any>

Source

Since v4.0.0

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

Signature

type AddError<A, Error> =
A extends Event<infer _Tag, infer _Payload, infer _Success, infer _Error>
? Event<_Tag, _Payload, _Success, _Error | Error>
: never

Source

Since v4.0.0

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

export interface Any {
readonly [TypeId]: TypeId
readonly tag: string
readonly primaryKey: (payload: any) => string
readonly payload: Schema.Top
readonly payloadMsgPack: Msgpack.schema<Schema.Top>
readonly success: Schema.Top
readonly error: Schema.Top
}

Source

Since v4.0.0

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

Signature

export interface AnyWithProps extends Any {}

Source

Since v4.0.0

Decoded error value type for an event definition.

Signature

type Error<A> = Schema.Schema.Type<ErrorSchema<A>>

Source

Since v4.0.0

Extracts the error schema from an event definition.

Signature

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

Source

Since v4.0.0

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

Signature

type ErrorWithTag<Events, Tag> = Error<WithTag<Events, Tag>>

Source

Since v4.0.0

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

export 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 [TypeId]: TypeId
readonly tag: Tag
readonly primaryKey: (payload: Schema.Schema.Type<Payload>) => string
readonly payload: Payload
readonly payloadMsgPack: Msgpack.schema<Payload>
readonly success: Success
readonly error: Error
}

Source

Since v4.0.0

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

export interface EventHandler<in out Tag extends string> {
readonly _: unique symbol
readonly tag: Tag
}

Source

Since v4.0.0

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

Signature

type ExcludeTag<Events, Tag> = Exclude<Events, { readonly tag: Tag }>

Source

Since v4.0.0

Decoded payload value type for an event definition.

Signature

type Payload<A> = Schema.Schema.Type<PayloadSchema<A>>

Source

Since v4.0.0

Extracts the payload schema from an event definition.

Signature

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

Source

Since v4.0.0

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

Signature

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

Source

Since v4.0.0

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

Signature

type PayloadWithTag<Events, Tag> = Payload<WithTag<Events, Tag>>

Source

Since v4.0.0

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

Source

Since v4.0.0

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

Source

Since v4.0.0

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

Signature

type ServicesClientWithTag<Events, Tag> = ServicesClient<WithTag<Events, Tag>>

Source

Since v4.0.0

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

Source

Since v4.0.0

Decoded success value type for an event definition.

Signature

type Success<A> = Schema.Schema.Type<SuccessSchema<A>>

Source

Since v4.0.0

Extracts the success schema from an event definition.

Signature

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

Source

Since v4.0.0

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

Signature

type SuccessWithTag<Events, Tag> = Success<WithTag<Events, Tag>>

Source

Since v4.0.0

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

Source

Since v4.0.0

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> =
A extends Event<infer _Tag, infer _Payload, infer _Success, infer _Error>
? {
readonly _tag: _Tag
readonly payload: Schema.Schema.Type<_Payload>
}
: never

Source

Since v4.0.0

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

Source

Since v4.0.0

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

Signature

type WithTag<Events, Tag> = Extract<Events, { readonly tag: Tag }>

Source

Since v4.0.0

Runtime type identifier used to mark event log event definitions.

Signature

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

Source

Since v4.0.0

Unique type identifier used to mark event log event definitions.

Signature

type TypeId = "~effect/eventlog/Event"

Source

Since v4.0.0