-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContainerAwareServiceLocator.js
117 lines (102 loc) · 2.7 KB
/
ContainerAwareServiceLocator.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
import { serviceIds } from './constants.js';
import HandlerNotFound from './exceptions/HandlerNotFound.js';
import ServiceLocator from './ServiceLocator.js';
import CommandBus from './CommandBus.js';
import EventBus from './EventBus.js';
import RequestBus from './RequestBus.js';
import curieToHandlerServiceId from './utils/curieToHandlerServiceId.js';
export default class ContainerAwareServiceLocator extends ServiceLocator {
/**
* @param {{get: function, has: function, getParameter: function, hasParameter: function}} container
*/
constructor(container) {
super();
Object.defineProperty(this, 'container', { value: container });
}
/**
* @returns {Pbjx}
*/
async doGetPbjx() {
if (!this.container.has(serviceIds.PBJX)) {
return super.doGetPbjx();
}
return this.container.get(serviceIds.PBJX);
}
/**
* @returns {Dispatcher}
*/
async doGetDispatcher() {
if (!this.container.has(serviceIds.DISPATCHER)) {
return super.doGetDispatcher();
}
return this.container.get(serviceIds.DISPATCHER);
}
/**
* @returns {CommandBus}
*/
async doGetCommandBus() {
return new CommandBus(this, await this.getTransportForBus(serviceIds.COMMAND_BUS_TRANSPORT));
}
/**
* @returns {EventBus}
*/
async doGetEventBus() {
return new EventBus(this, await this.getTransportForBus(serviceIds.EVENT_BUS_TRANSPORT));
}
/**
* @returns {RequestBus}
*/
async doGetRequestBus() {
return new RequestBus(this, await this.getTransportForBus(serviceIds.REQUEST_BUS_TRANSPORT));
}
/**
* @returns {ExceptionHandler}
*/
async doGetExceptionHandler() {
if (!this.container.has(serviceIds.EXCEPTION_HANDLER)) {
return super.doGetExceptionHandler();
}
return this.container.get(serviceIds.EXCEPTION_HANDLER);
}
/**
* @returns {{handleCommand: function}}
*/
async getCommandHandler(curie) {
return this.getHandler(curie);
}
/**
* @returns {{handleRequest: function}}
*/
async getRequestHandler(curie) {
return this.getHandler(curie);
}
/**
* @private
*
* @param {SchemaCurie} curie
*
* @returns {{handleCommand: function}|{handleRequest: function}}
*
* @throws {HandlerNotFound}
*/
async getHandler(curie) {
try {
return await this.container.get(curieToHandlerServiceId(curie));
} catch (e) {
throw new HandlerNotFound(curie);
}
}
/**
* @private
*
* @param {string} name
*
* @returns {Transport}
*/
async getTransportForBus(name) {
if (!this.container.hasParameter(name)) {
return this.getDefaultTransport();
}
return this.container.get(`${serviceIds.TRANSPORT_PREFIX}${this.container.getParameter(name)}`);
}
}