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!: rename Handler to MessageHandler #1161

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 6 additions & 6 deletions packages/action-menu/src/ActionMenuApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class ActionMenuApi {
this.messageSender = messageSender
this.actionMenuService = actionMenuService
this.agentContext = agentContext
this.registerHandlers(dispatcher)
this.registerMessageHandlers(dispatcher)
}

/**
Expand Down Expand Up @@ -158,10 +158,10 @@ export class ActionMenuApi {
return actionMenuRecord ? await this.actionMenuService.clearMenu(this.agentContext, { actionMenuRecord }) : null
}

private registerHandlers(dispatcher: Dispatcher) {
dispatcher.registerHandler(new ActionMenuProblemReportHandler(this.actionMenuService))
dispatcher.registerHandler(new MenuMessageHandler(this.actionMenuService))
dispatcher.registerHandler(new MenuRequestMessageHandler(this.actionMenuService))
dispatcher.registerHandler(new PerformMessageHandler(this.actionMenuService))
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))
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { ActionMenuService } from '../services'
import type { Handler, HandlerInboundMessage } from '@aries-framework/core'
import type { MessageHandler, MessageHandlerInboundMessage } from '@aries-framework/core'

import { ActionMenuProblemReportMessage } from '../messages'

export class ActionMenuProblemReportHandler implements Handler {
export class ActionMenuProblemReportHandler implements MessageHandler {
private actionMenuService: ActionMenuService
public supportedMessages = [ActionMenuProblemReportMessage]

public constructor(actionMenuService: ActionMenuService) {
this.actionMenuService = actionMenuService
}

public async handle(messageContext: HandlerInboundMessage<ActionMenuProblemReportHandler>) {
public async handle(messageContext: MessageHandlerInboundMessage<ActionMenuProblemReportHandler>) {
await this.actionMenuService.processProblemReport(messageContext)
}
}
6 changes: 3 additions & 3 deletions packages/action-menu/src/handlers/MenuMessageHandler.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { ActionMenuService } from '../services'
import type { Handler, HandlerInboundMessage } from '@aries-framework/core'
import type { MessageHandler, MessageHandlerInboundMessage } from '@aries-framework/core'

import { MenuMessage } from '../messages'

export class MenuMessageHandler implements Handler {
export class MenuMessageHandler implements MessageHandler {
private actionMenuService: ActionMenuService
public supportedMessages = [MenuMessage]

public constructor(actionMenuService: ActionMenuService) {
this.actionMenuService = actionMenuService
}

public async handle(inboundMessage: HandlerInboundMessage<MenuMessageHandler>) {
public async handle(inboundMessage: MessageHandlerInboundMessage<MenuMessageHandler>) {
inboundMessage.assertReadyConnection()

await this.actionMenuService.processMenu(inboundMessage)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { ActionMenuService } from '../services'
import type { Handler, HandlerInboundMessage } from '@aries-framework/core'
import type { MessageHandler, MessageHandlerInboundMessage } from '@aries-framework/core'

import { MenuRequestMessage } from '../messages'

export class MenuRequestMessageHandler implements Handler {
export class MenuRequestMessageHandler implements MessageHandler {
private actionMenuService: ActionMenuService
public supportedMessages = [MenuRequestMessage]

public constructor(actionMenuService: ActionMenuService) {
this.actionMenuService = actionMenuService
}

public async handle(inboundMessage: HandlerInboundMessage<MenuRequestMessageHandler>) {
public async handle(inboundMessage: MessageHandlerInboundMessage<MenuRequestMessageHandler>) {
inboundMessage.assertReadyConnection()

await this.actionMenuService.processRequest(inboundMessage)
Expand Down
6 changes: 3 additions & 3 deletions packages/action-menu/src/handlers/PerformMessageHandler.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { ActionMenuService } from '../services'
import type { Handler, HandlerInboundMessage } from '@aries-framework/core'
import type { MessageHandler, MessageHandlerInboundMessage } from '@aries-framework/core'

import { PerformMessage } from '../messages'

export class PerformMessageHandler implements Handler {
export class PerformMessageHandler implements MessageHandler {
private actionMenuService: ActionMenuService
public supportedMessages = [PerformMessage]

public constructor(actionMenuService: ActionMenuService) {
this.actionMenuService = actionMenuService
}

public async handle(inboundMessage: HandlerInboundMessage<PerformMessageHandler>) {
public async handle(inboundMessage: MessageHandlerInboundMessage<PerformMessageHandler>) {
inboundMessage.assertReadyConnection()

await this.actionMenuService.processPerform(inboundMessage)
Expand Down
28 changes: 14 additions & 14 deletions packages/core/src/agent/Dispatcher.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AgentMessage } from './AgentMessage'
import type { AgentMessageProcessedEvent } from './Events'
import type { Handler } from './Handler'
import type { MessageHandler } from './MessageHandler'
import type { InboundMessageContext } from './models/InboundMessageContext'

import { InjectionSymbols } from '../constants'
Expand All @@ -17,7 +17,7 @@ import { OutboundMessageContext } from './models'

@injectable()
class Dispatcher {
private handlers: Handler[] = []
private messageHandlers: MessageHandler[] = []
private messageSender: MessageSender
private eventEmitter: EventEmitter
private logger: Logger
Expand All @@ -32,22 +32,22 @@ class Dispatcher {
this.logger = logger
}

public registerHandler(handler: Handler) {
this.handlers.push(handler)
public registerMessageHandler(handler: MessageHandler) {
this.messageHandlers.push(handler)
}

public async dispatch(messageContext: InboundMessageContext): Promise<void> {
const { agentContext, connection, senderKey, recipientKey, message } = messageContext
const handler = this.getHandlerForType(message.type)
const messageHandler = this.getMessageHandlerForType(message.type)

if (!handler) {
if (!messageHandler) {
throw new AriesFrameworkError(`No handler for message type "${message.type}" found`)
}

let outboundMessage: OutboundMessageContext<AgentMessage> | void

try {
outboundMessage = await handler.handle(messageContext)
outboundMessage = await messageHandler.handle(messageContext)
} catch (error) {
const problemReportMessage = error.problemReport

Expand Down Expand Up @@ -90,21 +90,21 @@ class Dispatcher {
})
}

private getHandlerForType(messageType: string): Handler | undefined {
private getMessageHandlerForType(messageType: string): MessageHandler | undefined {
const incomingMessageType = parseMessageType(messageType)

for (const handler of this.handlers) {
for (const MessageClass of handler.supportedMessages) {
if (canHandleMessageType(MessageClass, incomingMessageType)) return handler
for (const messageHandler of this.messageHandlers) {
for (const MessageClass of messageHandler.supportedMessages) {
if (canHandleMessageType(MessageClass, incomingMessageType)) return messageHandler
}
}
}

public getMessageClassForType(messageType: string): typeof AgentMessage | undefined {
const incomingMessageType = parseMessageType(messageType)

for (const handler of this.handlers) {
for (const MessageClass of handler.supportedMessages) {
for (const messageHandler of this.messageHandlers) {
for (const MessageClass of messageHandler.supportedMessages) {
if (canHandleMessageType(MessageClass, incomingMessageType)) return MessageClass
}
}
Expand All @@ -115,7 +115,7 @@ class Dispatcher {
* Message type format is MTURI specified at https://github.com/hyperledger/aries-rfcs/blob/main/concepts/0003-protocols/README.md#mturi.
*/
public get supportedMessageTypes() {
return this.handlers
return this.messageHandlers
.reduce<typeof AgentMessage[]>((all, cur) => [...all, ...cur.supportedMessages], [])
.map((m) => m.type)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ConstructableAgentMessage } from './AgentMessage'
import type { InboundMessageContext, OutboundMessageContext } from './models'

export interface Handler {
export interface MessageHandler {
readonly supportedMessages: readonly ConstructableAgentMessage[]

handle(messageContext: InboundMessageContext): Promise<OutboundMessageContext | void>
Expand All @@ -12,8 +12,8 @@ export interface Handler {
* of a handler. It takes all possible types from `supportedMessageTypes`
*
* @example
* async handle(messageContext: HandlerInboundMessage<BasicMessageHandler>) {}
* async handle(messageContext: MessageHandlerInboundMessage<BasicMessageHandler>) {}
*/
export type HandlerInboundMessage<H extends Handler> = InboundMessageContext<
export type MessageHandlerInboundMessage<H extends MessageHandler> = InboundMessageContext<
InstanceType<H['supportedMessages'][number]>
>
16 changes: 8 additions & 8 deletions packages/core/src/agent/__tests__/Dispatcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Handler } from '../Handler'
import type { MessageHandler } from '../MessageHandler'

import { Subject } from 'rxjs'

Expand Down Expand Up @@ -34,7 +34,7 @@ class CustomProtocolMessage extends AgentMessage {
public static readonly type = parseMessageType('https://didcomm.org/fake-protocol/1.5/message')
}

class TestHandler implements Handler {
class TestHandler implements MessageHandler {
// We want to pass various classes to test various behaviours so we dont need to strictly type it.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public constructor(classes: any[]) {
Expand Down Expand Up @@ -62,10 +62,10 @@ describe('Dispatcher', () => {

const dispatcher = new Dispatcher(new MessageSenderMock(), eventEmitter, agentConfig.logger)

dispatcher.registerHandler(connectionHandler)
dispatcher.registerHandler(new TestHandler([NotificationAckTestMessage]))
dispatcher.registerHandler(new TestHandler([CredentialProposalTestMessage]))
dispatcher.registerHandler(fakeProtocolHandler)
dispatcher.registerMessageHandler(connectionHandler)
dispatcher.registerMessageHandler(new TestHandler([NotificationAckTestMessage]))
dispatcher.registerMessageHandler(new TestHandler([CredentialProposalTestMessage]))
dispatcher.registerMessageHandler(fakeProtocolHandler)

describe('supportedMessageTypes', () => {
test('return all supported message types URIs', async () => {
Expand Down Expand Up @@ -146,7 +146,7 @@ describe('Dispatcher', () => {
const inboundMessageContext = new InboundMessageContext(customProtocolMessage, { agentContext })

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

await dispatcher.dispatch(inboundMessageContext)

Expand All @@ -159,7 +159,7 @@ describe('Dispatcher', () => {
const inboundMessageContext = new InboundMessageContext(customProtocolMessage, { agentContext })

const mockHandle = jest.fn()
dispatcher.registerHandler({ supportedMessages: [], handle: mockHandle })
dispatcher.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
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export * from './agent'
export type { ModulesMap, DefaultAgentModules, EmptyModuleMap } from './agent/AgentModules'
export { EventEmitter } from './agent/EventEmitter'
export { FeatureRegistry } from './agent/FeatureRegistry'
export { Handler, HandlerInboundMessage } from './agent/Handler'
export { MessageHandler, MessageHandlerInboundMessage } from './agent/MessageHandler'
export * from './agent/models'
export { AgentConfig } from './agent/AgentConfig'
export { AgentMessage } from './agent/AgentMessage'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class BasicMessagesApi {
this.messageSender = messageSender
this.connectionService = connectionService
this.agentContext = agentContext
this.registerHandlers(dispatcher)
this.registerMessageHandlers(dispatcher)
}

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

private registerHandlers(dispatcher: Dispatcher) {
dispatcher.registerHandler(new BasicMessageHandler(this.basicMessageService))
private registerMessageHandlers(dispatcher: Dispatcher) {
dispatcher.registerMessageHandler(new BasicMessageHandler(this.basicMessageService))
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { Handler, HandlerInboundMessage } from '../../../agent/Handler'
import type { MessageHandler, MessageHandlerInboundMessage } from '../../../agent/MessageHandler'
import type { BasicMessageService } from '../services/BasicMessageService'

import { BasicMessage } from '../messages'

export class BasicMessageHandler implements Handler {
export class BasicMessageHandler implements MessageHandler {
private basicMessageService: BasicMessageService
public supportedMessages = [BasicMessage]

public constructor(basicMessageService: BasicMessageService) {
this.basicMessageService = basicMessageService
}

public async handle(messageContext: HandlerInboundMessage<BasicMessageHandler>) {
public async handle(messageContext: MessageHandlerInboundMessage<BasicMessageHandler>) {
const connection = messageContext.assertReadyConnection()
await this.basicMessageService.save(messageContext, connection)
}
Expand Down
20 changes: 10 additions & 10 deletions packages/core/src/modules/connections/ConnectionsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class ConnectionsApi {
this.agentContext = agentContext
this.config = connectionsModuleConfig

this.registerHandlers(dispatcher)
this.registerMessageHandlers(dispatcher)
}

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

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

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

dispatcher.registerHandler(
dispatcher.registerMessageHandler(
new DidExchangeResponseHandler(
this.didExchangeProtocol,
this.outOfBandService,
Expand All @@ -397,6 +397,6 @@ export class ConnectionsApi {
this.config
)
)
dispatcher.registerHandler(new DidExchangeCompleteHandler(this.didExchangeProtocol, this.outOfBandService))
dispatcher.registerMessageHandler(new DidExchangeCompleteHandler(this.didExchangeProtocol, this.outOfBandService))
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { Handler, HandlerInboundMessage } from '../../../agent/Handler'
import type { MessageHandler, MessageHandlerInboundMessage } from '../../../agent/MessageHandler'
import type { ConnectionService } from '../services/ConnectionService'

import { AckMessage } from '../../common'

export class AckMessageHandler implements Handler {
export class AckMessageHandler implements MessageHandler {
private connectionService: ConnectionService
public supportedMessages = [AckMessage]

public constructor(connectionService: ConnectionService) {
this.connectionService = connectionService
}

public async handle(inboundMessage: HandlerInboundMessage<AckMessageHandler>) {
public async handle(inboundMessage: MessageHandlerInboundMessage<AckMessageHandler>) {
await this.connectionService.processAck(inboundMessage)
}
}
Loading