-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediator.ts
158 lines (140 loc) · 4.13 KB
/
mediator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { Notification } from "./notification.ts";
import { PublishStrategy } from "./publish-strategy.ts";
import { Request } from "./request.ts";
import { RequestHandlerStore } from "./request-handler-store.ts";
import { NotificationHandlerStore } from "./notification-handler-store.ts";
import {
Constructor,
Handler,
NotificationConstructor,
NotificationHandler,
RequestConstructor,
RequestHandler,
Response,
} from "./types.ts";
import { PublisherFactory } from "./publisher-factory.ts";
import { TypeGuards } from "./type-guards.ts";
/**
* Configuration options for the mediator
*/
interface MediatorConfig {
publishStratey?: PublishStrategy;
}
export interface IMediator {
/**
* Register a request or notification handler
*/
handle<TRequest extends Request>(
constructor: RequestConstructor<TRequest>,
handler: RequestHandler<TRequest>,
): void;
handle<TNotification extends Notification>(
constructor: NotificationConstructor<TNotification>,
handler: NotificationHandler<TNotification>,
): void;
/**
* Unregister a request or notification handler
*/
unhandle<TRequest extends (Request | Notification)>(
constructor: Constructor<TRequest>,
handler: Handler<TRequest>,
): void;
/**
* Publish a notification
*/
publish<TNotification extends Notification>(
notification: TNotification,
publishStrategy?: PublishStrategy,
): Promise<void>;
/**
* Send a request
*/
send<TRequest extends Request>(
request: TRequest,
): Response<TRequest>;
}
/**
* Main mediator class that handles requests and notifications
*/
export class Mediator implements IMediator {
private _notificationHandlers: NotificationHandlerStore;
private _requestHandlers: RequestHandlerStore;
private _publisher;
constructor(config?: MediatorConfig) {
this._publisher = 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<TMessage extends Request | Notification>(
constructor: Constructor<TMessage>,
handler: Handler<TMessage>,
): void {
if (
TypeGuards.isRequestConstructor(constructor) &&
TypeGuards.isRequestHandler(handler)
) {
this._requestHandlers.add(constructor, handler);
return;
}
if (
TypeGuards.isNotificationConstructor(constructor) &&
TypeGuards.isNotificationHandler(handler)
) {
this._notificationHandlers.add(
constructor,
handler,
);
return;
}
throw new Error(`Invalid request or notification`);
}
public unhandle<TRequest extends (Request | Notification)>(
constructor: Constructor<TRequest>,
handler: Handler<TRequest>,
): void {
if (TypeGuards.isRequestConstructor(constructor)) {
this._requestHandlers.remove(constructor, handler);
return;
}
if (TypeGuards.isNotificationConstructor(constructor)) {
this._notificationHandlers.remove(
constructor,
handler as NotificationHandler,
);
return;
}
throw new Error(`Invalid request or notification`);
}
public async publish<TNotification extends Notification>(
notification: TNotification,
publishStrategy?: PublishStrategy,
): Promise<void> {
const publisher = publishStrategy != null
? PublisherFactory.create(publishStrategy)
: this._publisher;
if (!TypeGuards.isNotification(notification)) {
throw new Error(
`No handler found for notification, ${notification.constructor.name}`,
);
}
await publisher.publish(
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);
return handler(request);
}
throw new Error(`Invalid request`);
}
}