Skip to content

Commit

Permalink
feat(core)!: added basic message sent event (#507)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `BasicMessageReceivedEvent` has been replaced by the more general `BasicMessageStateChanged` event which triggers when a basic message is received or sent.

Signed-off-by: NeilSMyers <[email protected]>
  • Loading branch information
NeilSMyers authored Nov 1, 2021
1 parent b948d4c commit d2c04c3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import type { BasicMessage } from './messages'
import type { BasicMessageRecord } from './repository'

export enum BasicMessageEventTypes {
BasicMessageReceived = 'BasicMessageReceived',
BasicMessageStateChanged = 'BasicMessageStateChanged',
}

export interface BasicMessageReceivedEvent extends BaseEvent {
type: typeof BasicMessageEventTypes.BasicMessageReceived
export interface BasicMessageStateChangedEvent extends BaseEvent {
type: typeof BasicMessageEventTypes.BasicMessageStateChanged
payload: {
message: BasicMessage
basicMessageRecord: BasicMessageRecord
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AgentConfig } from '../../../agent/AgentConfig'
import type { StorageService } from '../../../storage/StorageService'
import type { BasicMessageReceivedEvent } from '../BasicMessageEvents'
import type { BasicMessageStateChangedEvent } from '../BasicMessageEvents'

import { getAgentConfig, getMockConnection } from '../../../../tests/helpers'
import { EventEmitter } from '../../../agent/EventEmitter'
Expand Down Expand Up @@ -50,7 +50,7 @@ describe('BasicMessageService', () => {

it(`emits newMessage with message and basic message record`, async () => {
const eventListenerMock = jest.fn()
eventEmitter.on<BasicMessageReceivedEvent>(BasicMessageEventTypes.BasicMessageReceived, eventListenerMock)
eventEmitter.on<BasicMessageStateChangedEvent>(BasicMessageEventTypes.BasicMessageStateChanged, eventListenerMock)

const basicMessage = new BasicMessage({
id: '123',
Expand All @@ -65,7 +65,7 @@ describe('BasicMessageService', () => {
await basicMessageService.save(messageContext, mockConnectionRecord)

expect(eventListenerMock).toHaveBeenCalledWith({
type: 'BasicMessageReceived',
type: 'BasicMessageStateChanged',
payload: {
basicMessageRecord: expect.objectContaining({
connectionId: mockConnectionRecord.id,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { InboundMessageContext } from '../../../agent/models/InboundMessageContext'
import type { ConnectionRecord } from '../../connections/repository/ConnectionRecord'
import type { BasicMessageReceivedEvent } from '../BasicMessageEvents'
import type { BasicMessageStateChangedEvent } from '../BasicMessageEvents'
import type { BasicMessageTags } from '../repository'

import { Lifecycle, scoped } from 'tsyringe'
Expand Down Expand Up @@ -33,6 +33,10 @@ export class BasicMessageService {
})

await this.basicMessageRepository.save(basicMessageRecord)
this.eventEmitter.emit<BasicMessageStateChangedEvent>({
type: BasicMessageEventTypes.BasicMessageStateChanged,
payload: { message: basicMessage, basicMessageRecord },
})

return basicMessage
}
Expand All @@ -50,8 +54,8 @@ export class BasicMessageService {
})

await this.basicMessageRepository.save(basicMessageRecord)
this.eventEmitter.emit<BasicMessageReceivedEvent>({
type: BasicMessageEventTypes.BasicMessageReceived,
this.eventEmitter.emit<BasicMessageStateChangedEvent>({
type: BasicMessageEventTypes.BasicMessageStateChanged,
payload: { message, basicMessageRecord },
})
}
Expand Down
8 changes: 4 additions & 4 deletions packages/core/tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { SubjectMessage } from '../../../tests/transport/SubjectInboundTran
import type {
AutoAcceptProof,
BasicMessage,
BasicMessageReceivedEvent,
BasicMessageStateChangedEvent,
ConnectionRecordProps,
CredentialDefinitionTemplate,
CredentialOfferTemplate,
Expand Down Expand Up @@ -184,17 +184,17 @@ export async function waitForCredentialRecord(

export async function waitForBasicMessage(agent: Agent, { content }: { content?: string }): Promise<BasicMessage> {
return new Promise((resolve) => {
const listener = (event: BasicMessageReceivedEvent) => {
const listener = (event: BasicMessageStateChangedEvent) => {
const contentMatches = content === undefined || event.payload.message.content === content

if (contentMatches) {
agent.events.off<BasicMessageReceivedEvent>(BasicMessageEventTypes.BasicMessageReceived, listener)
agent.events.off<BasicMessageStateChangedEvent>(BasicMessageEventTypes.BasicMessageStateChanged, listener)

resolve(event.payload.message)
}
}

agent.events.on<BasicMessageReceivedEvent>(BasicMessageEventTypes.BasicMessageReceived, listener)
agent.events.on<BasicMessageStateChangedEvent>(BasicMessageEventTypes.BasicMessageStateChanged, listener)
})
}

Expand Down

0 comments on commit d2c04c3

Please sign in to comment.