lib/src/support/bus/serial/NamedEventPayload.ts

37 lines
1.2 KiB
TypeScript

import {Event, SerialPayload} from '../types'
import {ObjectSerializer} from './decorators'
import {BaseSerializer} from './BaseSerializer'
import {JSONState} from '../../../util'
export class NamedEventPayload {
constructor(
public readonly eventName: string,
public readonly event: Event,
) {}
}
@ObjectSerializer()
export class NamedEventPayloadSerializer extends BaseSerializer<NamedEventPayload, { eventName: string, event: SerialPayload<Event, JSONState> }> {
protected async decodeSerial(serial: { eventName: string; event: SerialPayload<Event, JSONState> }): Promise<NamedEventPayload> {
return new NamedEventPayload(
serial.eventName,
await this.getSerialization().decode(serial.event),
)
}
protected async encodeActual(actual: NamedEventPayload): Promise<{ eventName: string; event: SerialPayload<Event, JSONState> }> {
return {
eventName: actual.eventName,
event: await this.getSerialization().encode(actual.event),
}
}
protected getName(): string {
return '@extollo/lib:NamedEventPayloadSerializer'
}
matchActual(some: NamedEventPayload): boolean {
return some instanceof NamedEventPayload
}
}