@polylith/core 0.1.10 → 0.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Defer.js +11 -0
- package/Registry.js +42 -14
- package/index.js +2 -1
- package/package.json +1 -1
package/Defer.js
ADDED
package/Registry.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ServiceOject } from "./ServiceObject.js";
|
|
2
2
|
import { makeEventable} from "./Eventable.js";
|
|
3
|
+
import Defer from "./Defer.js";
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* This calls is the implementation of the serices registry. There will be a
|
|
@@ -9,6 +10,7 @@ export class Registry {
|
|
|
9
10
|
/** Constructor for the registry */
|
|
10
11
|
constructor () {
|
|
11
12
|
this.services = {};
|
|
13
|
+
this.deferreds = {};
|
|
12
14
|
|
|
13
15
|
makeEventable(this);
|
|
14
16
|
}
|
|
@@ -131,26 +133,39 @@ export class Registry {
|
|
|
131
133
|
}
|
|
132
134
|
|
|
133
135
|
/**
|
|
134
|
-
* Call this method to invoke
|
|
136
|
+
* Call this method to invoke an event on every service in the list
|
|
135
137
|
*
|
|
136
|
-
* @param {
|
|
137
|
-
* @param {
|
|
138
|
-
* @param {
|
|
139
|
-
*
|
|
140
|
-
* @param
|
|
138
|
+
* @param {*} serviceNames the list of services to act on
|
|
139
|
+
* @param {*} event the event to fire
|
|
140
|
+
* @param {*} [preprocess] if provided, this method will be called on all
|
|
141
|
+
* services before firing the events
|
|
142
|
+
* @param {*} [processResult] if provided, this method will be called,
|
|
143
|
+
* passing the result for every event call
|
|
141
144
|
* @returns {Promise.<any>} an array of any promises that were returned from
|
|
142
145
|
* the called methods.
|
|
143
146
|
*/
|
|
144
|
-
callAll(serviceNames,
|
|
147
|
+
callAll(serviceNames, event, preprocess, processResult, ...args ) {
|
|
145
148
|
var promises = [];
|
|
146
149
|
|
|
150
|
+
// run the preprocess callback on each service
|
|
151
|
+
if (preprocess) {
|
|
152
|
+
serviceNames.forEach(function (name) {
|
|
153
|
+
var serviceObject = this.services[name];
|
|
154
|
+
preprocess(serviceObject);
|
|
155
|
+
}, this);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// invoke the event on all services
|
|
147
159
|
serviceNames.forEach(function (name) {
|
|
148
160
|
var serviceObject = this.services[name];
|
|
149
161
|
if (!serviceObject) return;
|
|
150
|
-
var result = serviceObject.invoke.apply(serviceObject, [which, ...args]);
|
|
151
162
|
|
|
163
|
+
var result = serviceObject.invoke.apply(serviceObject, [event, ...args]);
|
|
164
|
+
|
|
165
|
+
// now thta we have a result, let the post process have it
|
|
152
166
|
if (processResult) processResult(serviceObject, result)
|
|
153
167
|
|
|
168
|
+
// if the result is a promise, then we add ot the list to
|
|
154
169
|
if (result && result.then) {
|
|
155
170
|
promises.push(result);
|
|
156
171
|
}
|
|
@@ -160,18 +175,30 @@ export class Registry {
|
|
|
160
175
|
}
|
|
161
176
|
|
|
162
177
|
/**
|
|
163
|
-
* Call this method
|
|
178
|
+
* Call this method handle the service start deferreds
|
|
164
179
|
*
|
|
165
180
|
* @param {ServiceOject} serviceObject the service object to add the method
|
|
166
181
|
* to
|
|
167
182
|
* @param {*} result the result of the start method
|
|
168
183
|
*/
|
|
169
|
-
|
|
184
|
+
resolveDeferred(serviceObject, result) {
|
|
170
185
|
var promise = result?.then ? result : Promise.resolve(true);
|
|
171
186
|
|
|
187
|
+
// wait for the promise to finish to resolve the deferred
|
|
188
|
+
promise.then(function(result) {
|
|
189
|
+
this.deferreds[serviceObject.name].resolve(result)
|
|
190
|
+
}.bind(this)).catch(function(error) {
|
|
191
|
+
this.deferreds[serviceObject.name].reject(result)
|
|
192
|
+
}.bind(this))
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
addWaitMethod(serviceObject) {
|
|
196
|
+
this.deferreds[serviceObject.name] = new Defer()
|
|
197
|
+
|
|
172
198
|
serviceObject.waitStarted = function() {
|
|
173
|
-
|
|
174
|
-
|
|
199
|
+
// wait on
|
|
200
|
+
return this.deferreds[serviceObject.name].promise;
|
|
201
|
+
}.bind(this)
|
|
175
202
|
}
|
|
176
203
|
|
|
177
204
|
/**
|
|
@@ -224,14 +251,15 @@ export class Registry {
|
|
|
224
251
|
|
|
225
252
|
this.checkRequirements();
|
|
226
253
|
|
|
254
|
+
|
|
227
255
|
var services = names.filter(function(name) {
|
|
228
256
|
return name.indexOf(prefix) === 0;
|
|
229
257
|
}, this);
|
|
230
258
|
|
|
231
|
-
var promises = this.callAll(services, 'start', this.addWaitMethod.bind(this));
|
|
259
|
+
var promises = this.callAll(services, 'start', this.addWaitMethod.bind(this), this.resolveDeferred.bind(this));
|
|
232
260
|
return Promise.allSettled(promises)
|
|
233
261
|
.then(function () {
|
|
234
|
-
this.callAll(services, 'ready',
|
|
262
|
+
this.callAll(services, 'ready', null, null);
|
|
235
263
|
this.fire('ready', prefix);
|
|
236
264
|
}.bind(this));
|
|
237
265
|
}
|
package/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { registry, Registry } from './Registry.js';
|
|
2
2
|
import { makeEventable } from './Eventable.js';
|
|
3
3
|
import { Service } from './Service.js';
|
|
4
|
+
import { ServiceOject } from './ServiceObject.js';
|
|
4
5
|
|
|
5
|
-
export {registry, Registry, Service, makeEventable}
|
|
6
|
+
export {registry, Registry, Service, makeEventable, EventBus, ServiceOject}
|