import { StoreRepositoryInterface } from '../StoreRepositoryInterface'; import { Identity } from 'ts-eventsourcing/ValueObject/Identity'; import { ServerGatewayInterface } from '../../Gateway/ServerGatewayInterface'; import { ProjectorGatewayInterface } from '../ProjectorGatewayInterface'; import { ReadModelAction, ReadModelMetadata } from '../ReadModelAction'; export class SimpleProjectorGateway< State, Id extends Identity = Identity, Metadata extends ReadModelMetadata = ReadModelMetadata, Action extends ReadModelAction = ReadModelAction> implements ProjectorGatewayInterface { constructor(protected readonly repository: StoreRepositoryInterface, protected readonly gateway: ServerGatewayInterface) { } public async dispatchActionAndSave( id: Id, action: Action, ) { let model = await this.repository.find(id); if (!model) { model = await this.repository.create(id); } model.getStore().dispatch(action); await this.repository.save(model); await this.gateway.emit(action); } }