first commit
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import { Injectable, Type } from '@nestjs/common';
|
||||
import {
|
||||
EventClsRegistry,
|
||||
SerializableEvent,
|
||||
} from '@task-stream/api/shared/domain';
|
||||
|
||||
@Injectable()
|
||||
export class EventDeserializer {
|
||||
deserialize<T>(event: SerializableEvent): SerializableEvent<T> {
|
||||
const eventCls = EventClsRegistry.get(event.type);
|
||||
return {
|
||||
streamId: event.streamId,
|
||||
type: event.type,
|
||||
position: event.position,
|
||||
data: this.instantiateSerializedEvent(eventCls, event.data),
|
||||
};
|
||||
}
|
||||
|
||||
/** Rebuilds the event with its class prototype, without running the constructor. */
|
||||
instantiateSerializedEvent<T extends Type>(
|
||||
eventCls: T,
|
||||
data: Record<string, unknown>,
|
||||
) {
|
||||
return Object.assign(Object.create(eventCls.prototype), data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user