Skip to content
New issue

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

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

Already on GitHub? # to your account

✨ Get registered message types from mediator #29

Merged
merged 1 commit into from
Sep 4, 2024
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
18 changes: 18 additions & 0 deletions src/core/mediator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ describe('mediator.send', () => {
(handler as SinonSpy).calledOnceWith([expected]);
});

it('should return registered message types when getting message types from mediator', async () => {
// Arrange
const type1 = 'TYPE_1';
const type2 = 'TYPE_2';
const type3 = 'TYPE_3';
mediator.register(type3, async () => {});
mediator.register(type2, async () => {});
mediator.register(type1, async () => {});

// Act
const actual = mediator.getMessageTypes();

const expected = [type1, type2, type3];

// Assert
actual.should.be.deep.equal(expected);
});

it('should throw error when sending an unknown message through the mediator', async () => {
// Arrange
const type = 'CREATE_PERSON';
Expand Down
3 changes: 3 additions & 0 deletions src/core/mediator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { MessageResult } from './messageResult';
import { MultipleHandlersError } from './errors/multipleHandlers.error';
import { UnknownMessageError } from './errors/unknownMessage.error';
import { AddOptions, Middleware, middlewares } from './middlewares';

Check warning on line 6 in src/core/mediator.ts

View workflow job for this annotation

GitHub Actions / call-shared-workflow / lint

'Middleware' is defined but never used

const registry = new Map<string, Array<MessageHandler>>();

Expand Down Expand Up @@ -51,11 +51,14 @@
middlewares.add(options);
};

const getMessageTypes = () => [...registry.keys()].sort();

const mediator = {
register,
send,
publish,
use,
getMessageTypes,
} as const;

const clear = () => {
Expand Down
Loading