-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServiceLocator.js
211 lines (185 loc) · 4.21 KB
/
ServiceLocator.js
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import CommandBus from './CommandBus.js';
import Dispatcher from './Dispatcher.js';
import EventBus from './EventBus.js';
import ExceptionHandler from './ExceptionHandler.js';
import HandlerNotFound from './exceptions/HandlerNotFound.js';
import InMemoryTransport from './transports/InMemoryTransport.js';
import Pbjx from './Pbjx.js';
import RequestBus from './RequestBus.js';
const pbjx = Symbol('pbjx');
const dispatcher = Symbol('dispatcher');
const commandBus = Symbol('commandBus');
const eventBus = Symbol('eventBus');
const requestBus = Symbol('requestBus');
const exceptionHandler = Symbol('exceptionHandler');
const defaultTransport = Symbol('defaultTransport');
/**
* This class is meant to be extended so location of all services works as expected.
*
* @see ContainerAwareServiceLocator - used for most apps
* @see RegisteringServiceLocator - typically used for unit testing
*
* @link https://martinfowler.com/articles/injection.html#UsingAServiceLocator
*/
export default class ServiceLocator {
constructor() {
this[pbjx] = null;
this[dispatcher] = null;
this[commandBus] = null;
this[eventBus] = null;
this[requestBus] = null;
this[exceptionHandler] = null;
this[defaultTransport] = null;
}
/**
* @returns {Pbjx}
*/
async getPbjx() {
if (this[pbjx] === null) {
this[pbjx] = await this.doGetPbjx();
}
return this[pbjx];
}
/**
* @package
*
* @returns {Pbjx}
*/
async doGetPbjx() {
return new Pbjx(this);
}
/**
* @returns {Dispatcher}
*/
async getDispatcher() {
if (this[dispatcher] === null) {
this[dispatcher] = await this.doGetDispatcher();
}
return this[dispatcher];
}
/**
* @package
*
* @returns {Dispatcher}
*/
async doGetDispatcher() {
return new Dispatcher();
}
/**
* @returns {CommandBus}
*/
async getCommandBus() {
if (this[commandBus] === null) {
this[commandBus] = await this.doGetCommandBus();
}
return this[commandBus];
}
/**
* @package
*
* @returns {CommandBus}
*/
async doGetCommandBus() {
return new CommandBus(this, await this.getDefaultTransport());
}
/**
* @returns {EventBus}
*/
async getEventBus() {
if (this[eventBus] === null) {
this[eventBus] = await this.doGetEventBus();
}
return this[eventBus];
}
/**
* @package
*
* @returns {EventBus}
*/
async doGetEventBus() {
return new EventBus(this, await this.getDefaultTransport());
}
/**
* @returns {RequestBus}
*/
async getRequestBus() {
if (this[requestBus] === null) {
this[requestBus] = await this.doGetRequestBus();
}
return this[requestBus];
}
/**
* @package
*
* @returns {RequestBus}
*/
async doGetRequestBus() {
return new RequestBus(this, await this.getDefaultTransport());
}
/**
* @returns {ExceptionHandler}
*/
async getExceptionHandler() {
if (this[exceptionHandler] === null) {
this[exceptionHandler] = await this.doGetExceptionHandler();
}
return this[exceptionHandler];
}
/**
* @package
*
* @returns {ExceptionHandler}
*/
async doGetExceptionHandler() {
return new ExceptionHandler(await this.getDispatcher());
}
/**
* Returns the handler for the provided command.
*
* @param {SchemaCurie} curie
*
* @returns {{handleCommand: function}}
*
* @throws {HandlerNotFound}
*/
async getCommandHandler(curie) {
throw new HandlerNotFound(curie);
}
/**
* Returns the handler for the provided request.
*
* @param {SchemaCurie} curie
*
* @returns {{handleRequest: function}}
*
* @throws {HandlerNotFound}
*/
async getRequestHandler(curie) {
throw new HandlerNotFound(curie);
}
/**
* @package
*
* @returns {Transport}
*/
async getDefaultTransport() {
if (this[defaultTransport] === null) {
this[defaultTransport] = await this.doGetDefaultTransport();
}
return this[defaultTransport];
}
/**
* @param {Transport} transport
*/
setDefaultTransport(transport) {
this[defaultTransport] = transport;
}
/**
* @package
*
* @returns {Transport}
*/
async doGetDefaultTransport() {
return new InMemoryTransport(this);
}
}