Skip to content

Commit

Permalink
fix: clone record before emitting event (#938)
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra authored Jul 7, 2022
1 parent 9e30bb2 commit f907fe9
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/core/src/storage/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { RecordSavedEvent, RecordUpdatedEvent, RecordDeletedEvent } from '.
import type { BaseRecordConstructor, Query, StorageService } from './StorageService'

import { RecordDuplicateError, RecordNotFoundError } from '../error'
import { JsonTransformer } from '../utils/JsonTransformer'

import { RepositoryEventTypes } from './RepositoryEvents'

Expand All @@ -26,32 +27,41 @@ export class Repository<T extends BaseRecord<any, any, any>> {
/** @inheritDoc {StorageService#save} */
public async save(record: T): Promise<void> {
await this.storageService.save(record)

// Record in event should be static
const clonedRecord = JsonTransformer.clone(record)
this.eventEmitter.emit<RecordSavedEvent<T>>({
type: RepositoryEventTypes.RecordSaved,
payload: {
record,
record: clonedRecord,
},
})
}

/** @inheritDoc {StorageService#update} */
public async update(record: T): Promise<void> {
await this.storageService.update(record)

// Record in event should be static
const clonedRecord = JsonTransformer.clone(record)
this.eventEmitter.emit<RecordUpdatedEvent<T>>({
type: RepositoryEventTypes.RecordUpdated,
payload: {
record,
record: clonedRecord,
},
})
}

/** @inheritDoc {StorageService#delete} */
public async delete(record: T): Promise<void> {
await this.storageService.delete(record)

// Record in event should be static
const clonedRecord = JsonTransformer.clone(record)
this.eventEmitter.emit<RecordDeletedEvent<T>>({
type: RepositoryEventTypes.RecordDeleted,
payload: {
record,
record: clonedRecord,
},
})
}
Expand Down

0 comments on commit f907fe9

Please sign in to comment.