Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: remove Dispatcher.registerMessageHandler #1354

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions packages/action-menu/src/ActionMenuApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
AgentContext,
AriesFrameworkError,
ConnectionService,
Dispatcher,
MessageSender,
OutboundMessageContext,
injectable,
Expand All @@ -36,7 +35,6 @@ export class ActionMenuApi {
private agentContext: AgentContext

public constructor(
dispatcher: Dispatcher,
connectionService: ConnectionService,
messageSender: MessageSender,
actionMenuService: ActionMenuService,
Expand All @@ -46,7 +44,13 @@ export class ActionMenuApi {
this.messageSender = messageSender
this.actionMenuService = actionMenuService
this.agentContext = agentContext
this.registerMessageHandlers(dispatcher)

this.agentContext.dependencyManager.registerMessageHandlers([
new ActionMenuProblemReportHandler(this.actionMenuService),
new MenuMessageHandler(this.actionMenuService),
new MenuRequestMessageHandler(this.actionMenuService),
new PerformMessageHandler(this.actionMenuService),
])
}

/**
Expand Down Expand Up @@ -160,11 +164,4 @@ export class ActionMenuApi {

return actionMenuRecord ? await this.actionMenuService.clearMenu(this.agentContext, { actionMenuRecord }) : null
}

private registerMessageHandlers(dispatcher: Dispatcher) {
dispatcher.registerMessageHandler(new ActionMenuProblemReportHandler(this.actionMenuService))
dispatcher.registerMessageHandler(new MenuMessageHandler(this.actionMenuService))
dispatcher.registerMessageHandler(new MenuRequestMessageHandler(this.actionMenuService))
dispatcher.registerMessageHandler(new PerformMessageHandler(this.actionMenuService))
}
}
8 changes: 0 additions & 8 deletions packages/core/src/agent/Dispatcher.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { AgentMessage } from './AgentMessage'
import type { AgentMessageProcessedEvent } from './Events'
import type { MessageHandler } from './MessageHandler'
import type { InboundMessageContext } from './models/InboundMessageContext'

import { InjectionSymbols } from '../constants'
Expand Down Expand Up @@ -35,13 +34,6 @@ class Dispatcher {
this.logger = logger
}

/**
* @deprecated Use {@link MessageHandlerRegistry.registerMessageHandler} directly
*/
public registerMessageHandler(messageHandler: MessageHandler) {
this.messageHandlerRegistry.registerMessageHandler(messageHandler)
}

public async dispatch(messageContext: InboundMessageContext): Promise<void> {
const { agentContext, connection, senderKey, recipientKey, message } = messageContext
const messageHandler = this.messageHandlerRegistry.getHandlerForMessageType(message.type)
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/agent/__tests__/Dispatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,26 @@ describe('Dispatcher', () => {

describe('dispatch()', () => {
it('calls the handle method of the handler', async () => {
const messageHandlerRegistry = new MessageHandlerRegistry()
const dispatcher = new Dispatcher(
new MessageSenderMock(),
eventEmitter,
new MessageHandlerRegistry(),
messageHandlerRegistry,
agentConfig.logger
)
const customProtocolMessage = new CustomProtocolMessage()
const inboundMessageContext = new InboundMessageContext(customProtocolMessage, { agentContext })

const mockHandle = jest.fn()
dispatcher.registerMessageHandler({ supportedMessages: [CustomProtocolMessage], handle: mockHandle })
messageHandlerRegistry.registerMessageHandler({ supportedMessages: [CustomProtocolMessage], handle: mockHandle })

await dispatcher.dispatch(inboundMessageContext)

expect(mockHandle).toHaveBeenNthCalledWith(1, inboundMessageContext)
})

it('throws an error if no handler for the message could be found', async () => {
const messageHandlerRegistry = new MessageHandlerRegistry()
const dispatcher = new Dispatcher(
new MessageSenderMock(),
eventEmitter,
Expand All @@ -50,7 +52,7 @@ describe('Dispatcher', () => {
const inboundMessageContext = new InboundMessageContext(customProtocolMessage, { agentContext })

const mockHandle = jest.fn()
dispatcher.registerMessageHandler({ supportedMessages: [], handle: mockHandle })
messageHandlerRegistry.registerMessageHandler({ supportedMessages: [], handle: mockHandle })

await expect(dispatcher.dispatch(inboundMessageContext)).rejects.toThrow(
'No handler for message type "https://didcomm.org/fake-protocol/1.5/message" found'
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/modules/basic-messages/BasicMessagesApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { BasicMessageRecord } from './repository/BasicMessageRecord'
import type { Query } from '../../storage/StorageService'

import { AgentContext } from '../../agent'
import { Dispatcher } from '../../agent/Dispatcher'
import { MessageHandlerRegistry } from '../../agent/MessageHandlerRegistry'
import { MessageSender } from '../../agent/MessageSender'
import { OutboundMessageContext } from '../../agent/models'
import { injectable } from '../../plugins'
Expand All @@ -19,7 +19,7 @@ export class BasicMessagesApi {
private agentContext: AgentContext

public constructor(
dispatcher: Dispatcher,
messageHandlerRegistry: MessageHandlerRegistry,
basicMessageService: BasicMessageService,
messageSender: MessageSender,
connectionService: ConnectionService,
Expand All @@ -29,7 +29,7 @@ export class BasicMessagesApi {
this.messageSender = messageSender
this.connectionService = connectionService
this.agentContext = agentContext
this.registerMessageHandlers(dispatcher)
this.registerMessageHandlers(messageHandlerRegistry)
}

/**
Expand Down Expand Up @@ -91,7 +91,7 @@ export class BasicMessagesApi {
await this.basicMessageService.deleteById(this.agentContext, basicMessageRecordId)
}

private registerMessageHandlers(dispatcher: Dispatcher) {
dispatcher.registerMessageHandler(new BasicMessageHandler(this.basicMessageService))
private registerMessageHandlers(messageHandlerRegistry: MessageHandlerRegistry) {
messageHandlerRegistry.registerMessageHandler(new BasicMessageHandler(this.basicMessageService))
}
Comment on lines +94 to 96
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking now, if we register these in the Api, wouldn't it mean they're registered every time a new agent is initialized? (in multi-tenancy). They share the same message handler registry

}
28 changes: 16 additions & 12 deletions packages/core/src/modules/connections/ConnectionsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Query } from '../../storage/StorageService'
import type { OutOfBandRecord } from '../oob/repository'

import { AgentContext } from '../../agent'
import { Dispatcher } from '../../agent/Dispatcher'
import { MessageHandlerRegistry } from '../../agent/MessageHandlerRegistry'
import { MessageSender } from '../../agent/MessageSender'
import { OutboundMessageContext } from '../../agent/models'
import { ReturnRouteTypes } from '../../decorators/transport/TransportDecorator'
Expand Down Expand Up @@ -55,7 +55,7 @@ export class ConnectionsApi {
private agentContext: AgentContext

public constructor(
dispatcher: Dispatcher,
messageHandlerRegistry: MessageHandlerRegistry,
didExchangeProtocol: DidExchangeProtocol,
connectionService: ConnectionService,
outOfBandService: OutOfBandService,
Expand All @@ -78,7 +78,7 @@ export class ConnectionsApi {
this.agentContext = agentContext
this.config = connectionsModuleConfig

this.registerMessageHandlers(dispatcher)
this.registerMessageHandlers(messageHandlerRegistry)
}

public async acceptOutOfBandInvitation(
Expand Down Expand Up @@ -407,8 +407,8 @@ export class ConnectionsApi {
return this.connectionService.findByInvitationDid(this.agentContext, invitationDid)
}

private registerMessageHandlers(dispatcher: Dispatcher) {
dispatcher.registerMessageHandler(
private registerMessageHandlers(messageHandlerRegistry: MessageHandlerRegistry) {
messageHandlerRegistry.registerMessageHandler(
new ConnectionRequestHandler(
this.connectionService,
this.outOfBandService,
Expand All @@ -417,14 +417,16 @@ export class ConnectionsApi {
this.config
)
)
dispatcher.registerMessageHandler(
messageHandlerRegistry.registerMessageHandler(
new ConnectionResponseHandler(this.connectionService, this.outOfBandService, this.didResolverService, this.config)
)
dispatcher.registerMessageHandler(new AckMessageHandler(this.connectionService))
dispatcher.registerMessageHandler(new TrustPingMessageHandler(this.trustPingService, this.connectionService))
dispatcher.registerMessageHandler(new TrustPingResponseMessageHandler(this.trustPingService))
messageHandlerRegistry.registerMessageHandler(new AckMessageHandler(this.connectionService))
messageHandlerRegistry.registerMessageHandler(
new TrustPingMessageHandler(this.trustPingService, this.connectionService)
)
messageHandlerRegistry.registerMessageHandler(new TrustPingResponseMessageHandler(this.trustPingService))

dispatcher.registerMessageHandler(
messageHandlerRegistry.registerMessageHandler(
new DidExchangeRequestHandler(
this.didExchangeProtocol,
this.outOfBandService,
Expand All @@ -434,7 +436,7 @@ export class ConnectionsApi {
)
)

dispatcher.registerMessageHandler(
messageHandlerRegistry.registerMessageHandler(
new DidExchangeResponseHandler(
this.didExchangeProtocol,
this.outOfBandService,
Expand All @@ -443,6 +445,8 @@ export class ConnectionsApi {
this.config
)
)
dispatcher.registerMessageHandler(new DidExchangeCompleteHandler(this.didExchangeProtocol, this.outOfBandService))
messageHandlerRegistry.registerMessageHandler(
new DidExchangeCompleteHandler(this.didExchangeProtocol, this.outOfBandService)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import type { RevocationNotificationReceivedEvent } from '../../../CredentialEve
import type { V1RevocationNotificationMessage } from '../messages/V1RevocationNotificationMessage'
import type { V2RevocationNotificationMessage } from '../messages/V2RevocationNotificationMessage'

import { Dispatcher } from '../../../../../agent/Dispatcher'
import { EventEmitter } from '../../../../../agent/EventEmitter'
import { MessageHandlerRegistry } from '../../../../../agent/MessageHandlerRegistry'
import { InjectionSymbols } from '../../../../../constants'
import { AriesFrameworkError } from '../../../../../error/AriesFrameworkError'
import { Logger } from '../../../../../logger'
Expand All @@ -22,21 +22,19 @@ import { v1ThreadRegex, v2IndyRevocationFormat, v2IndyRevocationIdentifierRegex
export class RevocationNotificationService {
private credentialRepository: CredentialRepository
private eventEmitter: EventEmitter
private dispatcher: Dispatcher
private logger: Logger

public constructor(
credentialRepository: CredentialRepository,
eventEmitter: EventEmitter,
dispatcher: Dispatcher,
messageHandlerRegistry: MessageHandlerRegistry,
@inject(InjectionSymbols.Logger) logger: Logger
) {
this.credentialRepository = credentialRepository
this.eventEmitter = eventEmitter
this.dispatcher = dispatcher
this.logger = logger

this.registerMessageHandlers()
this.registerMessageHandlers(messageHandlerRegistry)
}

private async processRevocationNotification(
Expand Down Expand Up @@ -147,8 +145,8 @@ export class RevocationNotificationService {
}
}

private registerMessageHandlers() {
this.dispatcher.registerMessageHandler(new V1RevocationNotificationHandler(this))
this.dispatcher.registerMessageHandler(new V2RevocationNotificationHandler(this))
private registerMessageHandlers(messageHandlerRegistry: MessageHandlerRegistry) {
messageHandlerRegistry.registerMessageHandler(new V1RevocationNotificationHandler(this))
messageHandlerRegistry.registerMessageHandler(new V2RevocationNotificationHandler(this))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { Subject } from 'rxjs'

import { CredentialExchangeRecord, CredentialState, InboundMessageContext } from '../../../../../..'
import { getAgentConfig, getAgentContext, getMockConnection, mockFunction } from '../../../../../../../tests/helpers'
import { Dispatcher } from '../../../../../../agent/Dispatcher'
import { EventEmitter } from '../../../../../../agent/EventEmitter'
import { MessageHandlerRegistry } from '../../../../../../agent/MessageHandlerRegistry'
import { DidExchangeState } from '../../../../../connections'
import { CredentialEventTypes } from '../../../../CredentialEvents'
import { CredentialRepository } from '../../../../repository/CredentialRepository'
Expand All @@ -18,9 +18,9 @@ jest.mock('../../../../repository/CredentialRepository')
const CredentialRepositoryMock = CredentialRepository as jest.Mock<CredentialRepository>
const credentialRepository = new CredentialRepositoryMock()

jest.mock('../../../../../../agent/Dispatcher')
const DispatcherMock = Dispatcher as jest.Mock<Dispatcher>
const dispatcher = new DispatcherMock()
jest.mock('../../../../../../agent/MessageHandlerRegistry')
const MessageHandlerRegistryMock = MessageHandlerRegistry as jest.Mock<MessageHandlerRegistry>
const messageHandlerRegistry = new MessageHandlerRegistryMock()

const connection = getMockConnection({
state: DidExchangeState.Completed,
Expand All @@ -40,7 +40,7 @@ describe('RevocationNotificationService', () => {
revocationNotificationService = new RevocationNotificationService(
credentialRepository,
eventEmitter,
dispatcher,
messageHandlerRegistry,
agentConfig.logger
)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import type {
DiscoverFeaturesProtocolMsgReturnType,
} from '../../DiscoverFeaturesServiceOptions'

import { Dispatcher } from '../../../../agent/Dispatcher'
import { EventEmitter } from '../../../../agent/EventEmitter'
import { FeatureRegistry } from '../../../../agent/FeatureRegistry'
import { MessageHandlerRegistry } from '../../../../agent/MessageHandlerRegistry'
import { Protocol } from '../../../../agent/models'
import { InjectionSymbols } from '../../../../constants'
import { AriesFrameworkError } from '../../../../error'
Expand All @@ -30,23 +30,23 @@ export class V1DiscoverFeaturesService extends DiscoverFeaturesService {
public constructor(
featureRegistry: FeatureRegistry,
eventEmitter: EventEmitter,
dispatcher: Dispatcher,
messageHandlerRegistry: MessageHandlerRegistry,
@inject(InjectionSymbols.Logger) logger: Logger,
discoverFeaturesConfig: DiscoverFeaturesModuleConfig
) {
super(featureRegistry, eventEmitter, dispatcher, logger, discoverFeaturesConfig)
super(featureRegistry, eventEmitter, logger, discoverFeaturesConfig)

this.registerMessageHandlers(dispatcher)
this.registerMessageHandlers(messageHandlerRegistry)
}

/**
* The version of the discover features protocol this service supports
*/
public readonly version = 'v1'

private registerMessageHandlers(dispatcher: Dispatcher) {
dispatcher.registerMessageHandler(new V1DiscloseMessageHandler(this))
dispatcher.registerMessageHandler(new V1QueryMessageHandler(this))
private registerMessageHandlers(messageHandlerRegistry: MessageHandlerRegistry) {
messageHandlerRegistry.registerMessageHandler(new V1DiscloseMessageHandler(this))
messageHandlerRegistry.registerMessageHandler(new V1QueryMessageHandler(this))
}

public async createQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import type { DiscoverFeaturesProtocolMsgReturnType } from '../../../DiscoverFea
import { Subject } from 'rxjs'

import { agentDependencies, getAgentContext, getMockConnection } from '../../../../../../tests/helpers'
import { Dispatcher } from '../../../../../agent/Dispatcher'
import { EventEmitter } from '../../../../../agent/EventEmitter'
import { FeatureRegistry } from '../../../../../agent/FeatureRegistry'
import { MessageHandlerRegistry } from '../../../../../agent/MessageHandlerRegistry'
import { Protocol } from '../../../../../agent/models'
import { InboundMessageContext } from '../../../../../agent/models/InboundMessageContext'
import { ConsoleLogger } from '../../../../../logger/ConsoleLogger'
Expand All @@ -19,8 +19,8 @@ import { DiscoverFeaturesModuleConfig } from '../../../DiscoverFeaturesModuleCon
import { V1DiscoverFeaturesService } from '../V1DiscoverFeaturesService'
import { V1DiscloseMessage, V1QueryMessage } from '../messages'

jest.mock('../../../../../agent/Dispatcher')
const DispatcherMock = Dispatcher as jest.Mock<Dispatcher>
jest.mock('../../../../../agent/MessageHandlerRegistry')
const MessageHandlerRegistryMock = MessageHandlerRegistry as jest.Mock<MessageHandlerRegistry>
const eventEmitter = new EventEmitter(agentDependencies, new Subject())
const featureRegistry = new FeatureRegistry()
featureRegistry.register(new Protocol({ id: 'https://didcomm.org/connections/1.0' }))
Expand All @@ -36,7 +36,7 @@ describe('V1DiscoverFeaturesService - auto accept queries', () => {
const discoverFeaturesService = new V1DiscoverFeaturesService(
featureRegistry,
eventEmitter,
new DispatcherMock(),
new MessageHandlerRegistryMock(),
new LoggerMock(),
discoverFeaturesModuleConfig
)
Expand Down Expand Up @@ -239,7 +239,7 @@ describe('V1DiscoverFeaturesService - auto accept disabled', () => {
const discoverFeaturesService = new V1DiscoverFeaturesService(
featureRegistry,
eventEmitter,
new DispatcherMock(),
new MessageHandlerRegistry(),
new LoggerMock(),
discoverFeaturesModuleConfig
)
Expand Down
Loading