-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
261f636
commit 72216eb
Showing
26 changed files
with
568 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export * from './mediatr/extensions-di/ServiceCollectionExtensions'; | ||
export * from './mediatr/notification-publishers/ForOfAwaitPublisher'; | ||
export * from './mediatr/notification-publishers/PromiseAllPublisher'; | ||
export * from './mediatr/registrar/ServiceRegistrar'; | ||
export * from './mediatr/IMediator'; | ||
export * from './mediatr/INotificationHandler'; | ||
export * from './mediatr/IPublisher'; | ||
export * from './mediatr/IRequestHandler'; | ||
export * from './mediatr/ISender'; | ||
export * from './mediatr.contracts/INotification'; | ||
export * from './mediatr.contracts/IRequest'; |
2 changes: 2 additions & 0 deletions
2
packages/third-party.mediatr/src/mediatr.contracts/INotification.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// https://github.com/jbogard/MediatR/blob/a533cf288a50201aba3087ee7b521ec37f4337fd/src/MediatR.Contracts/INotification.cs#L6 | ||
export interface INotification {} |
5 changes: 5 additions & 0 deletions
5
packages/third-party.mediatr/src/mediatr.contracts/IRequest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// https://github.com/jbogard/MediatR/blob/761fb0b1b420f5a8c2cb4a751617dce7ab9c3fe3/src/MediatR.Contracts/IRequest.cs#L17 | ||
export interface IBaseRequest {} | ||
|
||
// https://github.com/jbogard/MediatR/blob/761fb0b1b420f5a8c2cb4a751617dce7ab9c3fe3/src/MediatR.Contracts/IRequest.cs#L12 | ||
export interface IRequest<TResponse> extends IBaseRequest {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { IPublisher } from './IPublisher'; | ||
import { ISender } from './ISender'; | ||
|
||
export const IMediator = Symbol.for('IMediator'); | ||
// https://github.com/jbogard/MediatR/blob/c4f1a918b4cb90030f2df0878f5930b9ed7baf16/src/MediatR/IMediator.cs#L6 | ||
export interface IMediator extends ISender, IPublisher {} |
19 changes: 19 additions & 0 deletions
19
packages/third-party.mediatr/src/mediatr/INotificationHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { INotification } from '../mediatr.contracts/INotification'; | ||
|
||
// https://github.com/jbogard/MediatR/blob/c4f1a918b4cb90030f2df0878f5930b9ed7baf16/src/MediatR/INotificationHandler.cs#L10 | ||
export interface INotificationHandler<TNotification extends INotification> { | ||
handle(notification: TNotification): Promise<void>; | ||
} | ||
|
||
// https://github.com/jbogard/MediatR/blob/c4f1a918b4cb90030f2df0878f5930b9ed7baf16/src/MediatR/INotificationHandler.cs#L25 | ||
export abstract class NotificationHandler<TNotification extends INotification> | ||
implements INotificationHandler<TNotification> | ||
{ | ||
protected abstract handleCore(notification: TNotification): void; | ||
|
||
handle(notification: TNotification): Promise<void> { | ||
this.handleCore(notification); | ||
|
||
return Promise.resolve(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/third-party.mediatr/src/mediatr/INotificationPublisher.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { INotification } from '../mediatr.contracts/INotification'; | ||
import { NotificationHandlerExecutor } from './NotificationHandlerExecutor'; | ||
|
||
export const INotificationPublisher = Symbol.for('INotificationPublisher'); | ||
// https://github.com/jbogard/MediatR/blob/838a8e12b62ee95f2f1caa503d282a8d9bce6047/src/MediatR/INotificationPublisher.cs#L7 | ||
export interface INotificationPublisher { | ||
publish( | ||
handlerExecutors: NotificationHandlerExecutor[], | ||
notification: INotification, | ||
): Promise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { INotification } from '../mediatr.contracts/INotification'; | ||
|
||
// https://github.com/jbogard/MediatR/blob/c4f1a918b4cb90030f2df0878f5930b9ed7baf16/src/MediatR/IPublisher.cs#L9 | ||
export interface IPublisher { | ||
publish<TNotification extends INotification>( | ||
notification: TNotification, | ||
): Promise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { IRequest } from '../mediatr.contracts/IRequest'; | ||
|
||
// https://github.com/jbogard/MediatR/blob/761fb0b1b420f5a8c2cb4a751617dce7ab9c3fe3/src/MediatR/IRequestHandler.cs#L11 | ||
export interface IRequestHandler< | ||
TRequest extends IRequest<TResponse>, | ||
TResponse, | ||
> { | ||
handle(request: TRequest): Promise<TResponse>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { IRequest } from '../mediatr.contracts/IRequest'; | ||
|
||
// https://github.com/jbogard/MediatR/blob/761fb0b1b420f5a8c2cb4a751617dce7ab9c3fe3/src/MediatR/ISender.cs#L10 | ||
export interface ISender { | ||
send<TResponse>(request: IRequest<TResponse>): Promise<TResponse>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Ctor, IServiceProvider, getOrAdd } from '@yohira/base'; | ||
import { inject } from '@yohira/extensions.dependency-injection.abstractions'; | ||
|
||
import { INotification } from '../mediatr.contracts/INotification'; | ||
import { IRequest } from '../mediatr.contracts/IRequest'; | ||
import { IMediator } from './IMediator'; | ||
import { INotificationPublisher } from './INotificationPublisher'; | ||
import { NotificationHandlerExecutor } from './NotificationHandlerExecutor'; | ||
import { | ||
NotificationHandlerWrapper, | ||
NotificationHandlerWrapperImpl, | ||
} from './wrappers/NotificationHandlerWrapper'; | ||
|
||
// https://github.com/jbogard/MediatR/blob/f4de8196adafd37faff274ce819ada93a3d7531b/src/MediatR/Mediator.cs#L16 | ||
export class Mediator implements IMediator { | ||
private readonly notificationHandlers = new Map< | ||
Ctor<INotification>, | ||
NotificationHandlerWrapper | ||
>(); | ||
|
||
constructor( | ||
@inject(IServiceProvider) | ||
private readonly serviceProvider: IServiceProvider, | ||
@inject(INotificationPublisher) | ||
private readonly publisher: INotificationPublisher, | ||
) {} | ||
|
||
send<TResponse>(request: IRequest<TResponse>): Promise<TResponse> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
/** | ||
* Override in a derived class to control how the tasks are awaited. By default the implementation calls the {@link INotificationPublisher}. | ||
* @param handlerExecutors Enumerable of tasks representing invoking each notification handler | ||
* @param notification The notification being published | ||
* @returns A task representing invoking all handlers | ||
*/ | ||
protected publishCore( | ||
handlerExecutors: NotificationHandlerExecutor[], | ||
notification: INotification, | ||
): Promise<void> { | ||
return this.publisher.publish(handlerExecutors, notification); | ||
} | ||
|
||
private publishNotification(notification: INotification): Promise<void> { | ||
const handler = getOrAdd( | ||
this.notificationHandlers, | ||
notification.constructor as Ctor<INotification>, | ||
(notificationCtor) => { | ||
const wrapper = new NotificationHandlerWrapperImpl( | ||
notificationCtor, | ||
); | ||
return wrapper; | ||
}, | ||
); | ||
|
||
return handler.handle( | ||
notification, | ||
this.serviceProvider, | ||
(executors, notification) => | ||
this.publishCore(executors, notification), | ||
); | ||
} | ||
|
||
publish<TNotification extends INotification>( | ||
notification: TNotification, | ||
): Promise<void> { | ||
return this.publishNotification(notification); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/third-party.mediatr/src/mediatr/NotificationHandlerExecutor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { INotification } from '../mediatr.contracts/INotification'; | ||
|
||
// https://github.com/jbogard/MediatR/blob/838a8e12b62ee95f2f1caa503d282a8d9bce6047/src/MediatR/NotificationHandlerExecutor.cs#L7 | ||
export class NotificationHandlerExecutor { | ||
constructor( | ||
readonly handlerInstance: object, | ||
readonly handlerCallback: ( | ||
notification: INotification, | ||
) => Promise<void>, | ||
) {} | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/third-party.mediatr/src/mediatr/extensions-di/MediatRServiceConfig.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Ctor } from '@yohira/base'; | ||
import { ServiceLifetime } from '@yohira/extensions.dependency-injection.abstractions'; | ||
|
||
import { INotificationPublisher } from '../INotificationPublisher'; | ||
import { Mediator } from '../Mediatr'; | ||
import { ForOfAwaitPublisher } from '../notification-publishers/ForOfAwaitPublisher'; | ||
|
||
// https://github.com/jbogard/MediatR/blob/f4de8196adafd37faff274ce819ada93a3d7531b/src/MediatR/MicrosoftExtensionsDI/MediatrServiceConfiguration.cs#L9 | ||
export class MediatRServiceConfig { | ||
/** | ||
* Mediator implementation type to register. Default is {@link Mediator} | ||
*/ | ||
mediatorImplCtor: Ctor<object> = Mediator; | ||
/** | ||
* Strategy for publishing notifications. Defaults to {@link ForeachAwaitPublisher} | ||
*/ | ||
notificationPublisher: INotificationPublisher = new ForOfAwaitPublisher(); | ||
/** | ||
* Type of notification publisher strategy to register. If set, overrides {@link NotificationPublisher} | ||
*/ | ||
notificationPublisherCtor?: Ctor<object>; | ||
/** | ||
* Service lifetime to register services under. Default value is {@link ServiceLifetime.Transient} | ||
*/ | ||
lifetime: ServiceLifetime = ServiceLifetime.Transient; | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/third-party.mediatr/src/mediatr/extensions-di/ServiceCollectionExtensions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { IServiceCollection } from '@yohira/extensions.dependency-injection.abstractions'; | ||
|
||
import { addRequiredServices } from '../registrar/ServiceRegistrar'; | ||
import { MediatRServiceConfig } from './MediatRServiceConfig'; | ||
|
||
function addMediatRCore( | ||
services: IServiceCollection, | ||
config: MediatRServiceConfig, | ||
): IServiceCollection { | ||
// TODO: addMediatRClasses(services, config); | ||
|
||
addRequiredServices(services, config); | ||
|
||
return services; | ||
} | ||
|
||
// https://github.com/jbogard/MediatR/blob/f28cdc331faea401479d8e765b6f4dd536b2b085/src/MediatR/MicrosoftExtensionsDI/ServiceCollectionExtensions.cs#L26 | ||
export function addMediatR( | ||
services: IServiceCollection, | ||
config: (serviceConfig: MediatRServiceConfig) => void, | ||
): IServiceCollection { | ||
const serviceConfig = new MediatRServiceConfig(); | ||
|
||
config(serviceConfig); | ||
|
||
return addMediatRCore(services, serviceConfig); | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/third-party.mediatr/src/mediatr/notification-publishers/ForOfAwaitPublisher.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { INotification } from '../../mediatr.contracts/INotification'; | ||
import { INotificationPublisher } from '../INotificationPublisher'; | ||
import { NotificationHandlerExecutor } from '../NotificationHandlerExecutor'; | ||
|
||
// https://github.com/jbogard/MediatR/blob/838a8e12b62ee95f2f1caa503d282a8d9bce6047/src/MediatR/NotificationPublishers/ForeachAwaitPublisher.cs#L17 | ||
export class ForOfAwaitPublisher implements INotificationPublisher { | ||
async publish( | ||
handlerExecutors: NotificationHandlerExecutor[], | ||
notification: INotification, | ||
): Promise<void> { | ||
for (const handler of handlerExecutors) { | ||
await handler.handlerCallback(notification); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/third-party.mediatr/src/mediatr/notification-publishers/PromiseAllPublisher.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { INotification } from '../../mediatr.contracts/INotification'; | ||
import { INotificationPublisher } from '../INotificationPublisher'; | ||
import { NotificationHandlerExecutor } from '../NotificationHandlerExecutor'; | ||
|
||
// https://github.com/jbogard/MediatR/blob/40afa9fc6ec7ddcfc8fac2584861916fb571f817/src/MediatR/NotificationPublishers/TaskWhenAllPublisher.cs#L20 | ||
export class PromiseAllPublisher implements INotificationPublisher { | ||
async publish( | ||
handlerExecutors: NotificationHandlerExecutor[], | ||
notification: INotification, | ||
): Promise<void> { | ||
const tasks = handlerExecutors.map((handler) => | ||
handler.handlerCallback(notification), | ||
); | ||
|
||
await Promise.all(tasks); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
packages/third-party.mediatr/src/mediatr/registrar/ServiceRegistrar.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { | ||
IServiceCollection, | ||
ServiceDescriptor, | ||
ServiceLifetime, | ||
tryAddServiceDescriptor, | ||
} from '@yohira/extensions.dependency-injection.abstractions'; | ||
|
||
import { IMediator } from '../IMediator'; | ||
import { INotificationPublisher } from '../INotificationPublisher'; | ||
import { MediatRServiceConfig } from '../extensions-di/MediatRServiceConfig'; | ||
|
||
// https://github.com/jbogard/MediatR/blob/43fb46f39020ab4880fefe75fa2315351f347742/src/MediatR/Registration/ServiceRegistrar.cs#L300 | ||
export function addRequiredServices( | ||
services: IServiceCollection, | ||
serviceConfig: MediatRServiceConfig, | ||
): void { | ||
tryAddServiceDescriptor( | ||
services, | ||
ServiceDescriptor.fromCtor( | ||
serviceConfig.lifetime, | ||
IMediator, | ||
serviceConfig.mediatorImplCtor, | ||
), | ||
); | ||
// TODO | ||
|
||
const notificationPublisherServiceDescriptor = | ||
serviceConfig.notificationPublisherCtor !== undefined | ||
? ServiceDescriptor.fromCtor( | ||
serviceConfig.lifetime, | ||
INotificationPublisher, | ||
serviceConfig.notificationPublisherCtor, | ||
) | ||
: ServiceDescriptor.fromInstance( | ||
ServiceLifetime.Singleton, | ||
INotificationPublisher, | ||
serviceConfig.notificationPublisher, | ||
); | ||
|
||
tryAddServiceDescriptor(services, notificationPublisherServiceDescriptor); | ||
} |
Oops, something went wrong.