跳转到内容
简体中文

EventKey

此内容尚不支持你的语言。

Defined in: src/EventKey.ts:64

Named, type-tagged identity for a kind-discriminated event — the EventStream counterpart to ServiceKey. Two keys are equal iff their kind matches; the type parameter is a compile-time marker that lets a subscription’s predicate see the event’s real shape without the caller restating it.

The field is kind, not id: it is compared against the event’s own kind, the project-wide discriminant, and a second name for the same string would be drift waiting to happen.

A type and a const of the same name give a plain event the call shape a class channel gets for free:

export type UserLoggedInEvent = { readonly kind: 'user-logged-in'; readonly userId: string };
export const UserLoggedInEvent = EventKey.of<UserLoggedInEvent>('user-logged-in');
eventStream.subscribe(self, UserLoggedInEvent, (event) => event.userId !== 'system');
eventStream.publish({ kind: 'user-logged-in', userId: 'user-42' });

The explicit type argument is load-bearing. EventKey.of('user-logged-in') infers EventKey<{ kind: string }> from the constraint, and every predicate written against that key sees { kind: string } instead of the event.

TEvent = unknown

new EventKey<TEvent>(kind): EventKey<TEvent>

Defined in: src/EventKey.ts:68

string

EventKey<TEvent>

readonly _event: TEvent

Defined in: src/EventKey.ts:66

Phantom field — retains TEvent so inference round-trips through the key.


readonly kind: string

Defined in: src/EventKey.ts:68

equals(other): boolean

Defined in: src/EventKey.ts:81

EventKey

boolean


toString(): string

Defined in: src/EventKey.ts:82

string


static of<TEvent>(kind): EventKey<TEvent>

Defined in: src/EventKey.ts:77

The parameter is KindOf<TEvent> rather than the free-form string that ServiceKey.of takes, because unlike a service id the kind is a field of the event type — so the relationship is checkable, and a mistyped kind fails here instead of at run time by never matching anything. The constructor keeps string as the raw door for a kind computed at run time.

TEvent extends object

KindOf<TEvent>

EventKey<TEvent>