Skip to content

Commit

Permalink
Merge pull request #4 from myty:bugfix/npm-mediator-private-variable-…
Browse files Browse the repository at this point in the history
…access

Mediator private member access
  • Loading branch information
myty authored Mar 3, 2022
2 parents b521cda + 7c269ba commit dec38bd
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions mediator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,34 @@ interface MediatorConfig {
}

export class Mediator {
#notificationHandlers: NotificationHandlerStore =
new NotificationHandlerStore();
#requestHandlers: RequestHandlerStore = new RequestHandlerStore();
#publishStrategy: IPublisher;
private _notificationHandlers: NotificationHandlerStore;
private _requestHandlers: RequestHandlerStore;
private _publishStrategy: IPublisher;

constructor(config?: MediatorConfig) {
this.#publishStrategy = PublisherFactory.create(
this._publishStrategy = PublisherFactory.create(
config?.publishStratey ??
PublishStrategy.SyncContinueOnException,
);
this._notificationHandlers = new NotificationHandlerStore();
this._requestHandlers = new RequestHandlerStore();

this.handle = this.handle.bind(this);
this.publish = this.publish.bind(this);
this.send = this.send.bind(this);
}

public handle<TRequest extends (Request<AnyType> | Notification)>(
constructor: Constructor<TRequest>,
handler: Handler<TRequest>,
): void {
if (TypeGuards.isRequestConstructor(constructor)) {
this.#requestHandlers.add(constructor, handler);
this._requestHandlers.add(constructor, handler);
return;
}

if (TypeGuards.isNotificationConstructor(constructor)) {
this.#notificationHandlers.add(
this._notificationHandlers.add(
constructor,
handler as NotificationHandler,
);
Expand All @@ -56,7 +61,7 @@ export class Mediator {
): Promise<void> {
const publisher = publishStrategy != null
? PublisherFactory.create(publishStrategy)
: this.#publishStrategy;
: this._publishStrategy;

if (!TypeGuards.isNotification(notification)) {
throw new Error(
Expand All @@ -66,15 +71,15 @@ export class Mediator {

await publisher.publish(
notification,
this.#notificationHandlers.get(notification),
this._notificationHandlers.get(notification),
);
}

public send<TRequest extends Request>(
request: TRequest,
): Response<TRequest> {
if (TypeGuards.isRequest<TRequest>(request)) {
const handler = this.#requestHandlers.get<TRequest>(request);
const handler = this._requestHandlers.get<TRequest>(request);
return handler(request);
}

Expand Down

0 comments on commit dec38bd

Please sign in to comment.