Skip to content

Commit 61f0ac7

Browse files
author
craig
committed
2.0.0 / 2022-03-23
================== * Initial esm support - @craigparra
1 parent 3be5e27 commit 61f0ac7

9 files changed

+147
-150
lines changed

ApplicationContext.js

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import {
66
Context, Component, Property, Scopes,
77
} from './context/index.js';
88

9-
const logger = { LoggerFactory }.getLogger('@alt-javascript/cdi/ApplicationContext');
10-
119
export default class ApplicationContext {
1210
// eslint-disable-next-line
1311
static DEFAULT_CONTEXT_NAME = 'default';
@@ -49,9 +47,9 @@ export default class ApplicationContext {
4947
this.profiles = options?.profiles;
5048
this.name = options?.name || ApplicationContext.DEFAULT_CONTEXT_NAME;
5149
this.configContextPath = options?.configContextPath
52-
|| process.env.NODE_CONFIG_CONTEXT_PATH
50+
|| (typeof (process) !== 'undefined' && process?.env?.NODE_CONFIG_CONTEXT_PATH)
5351
|| ApplicationContext.DEFAULT_CONFIG_CONTEXT_PATH;
54-
this.config = options?.config || { ConfigFactory }.getConfig({});
52+
this.config = options?.config || ConfigFactory.getConfig({});
5553
if (options?.config) {
5654
// eslint-disable-next-line no-param-reassign
5755
delete options.config;
@@ -64,14 +62,15 @@ export default class ApplicationContext {
6462
// eslint-disable-next-line no-param-reassign
6563
delete options.configContextPath;
6664
}
65+
this.logger = LoggerFactory.getLogger('@alt-javascript/cdi/ApplicationContext', this.config);
6766
}
6867

6968
async start() {
7069
return this.lifeCycle();
7170
}
7271

7372
async lifeCycle() {
74-
logger.verbose(`ApplicationContext (${this.name}) lifecycle started.`);
73+
this.logger.verbose(`ApplicationContext (${this.name}) lifecycle started.`);
7574
this.parseContexts();
7675
this.createSingletons();
7776
this.injectSingletonDependencies();
@@ -81,18 +80,18 @@ export default class ApplicationContext {
8180
}
8281

8382
detectConfigContext() {
84-
logger.verbose('Detecting config contexts started.');
83+
this.logger.verbose('Detecting config contexts started.');
8584
if (this.config) {
8685
if (this.config.has(this.configContextPath)) {
87-
logger.verbose(`Detected config context at ${this.configContextPath}, adding context.`);
86+
this.logger.verbose(`Detected config context at ${this.configContextPath}, adding context.`);
8887
this.contexts.push(this.config.get(this.configContextPath));
8988
}
9089
}
91-
logger.verbose('Detecting config contexts completed.');
90+
this.logger.verbose('Detecting config contexts completed.');
9291
}
9392

9493
detectGlobalContextComponents() {
95-
logger.verbose('Detecting global context components started.');
94+
this.logger.verbose('Detecting global context components started.');
9695

9796
if (!this.components.config && ApplicationContext.getGlobalRoot('config')) {
9897
this.deriveContextComponent({
@@ -127,11 +126,11 @@ export default class ApplicationContext {
127126
});
128127
}
129128

130-
logger.verbose('Detecting global context components completed.');
129+
this.logger.verbose('Detecting global context components completed.');
131130
}
132131

133132
parseContexts() {
134-
logger.verbose('Parsing configured contexts started.');
133+
this.logger.verbose('Parsing configured contexts started.');
135134
this.detectConfigContext();
136135
for (let i = 0; i < this.contexts.length; i++) {
137136
if (this.contexts[i]) {
@@ -142,12 +141,12 @@ export default class ApplicationContext {
142141
}
143142
} else {
144143
const msg = `ApplicationContext (${this.name}) received a nullish context.`;
145-
logger.error(msg);
144+
this.logger.error(msg);
146145
throw new Error(msg);
147146
}
148147
}
149148
this.detectGlobalContextComponents();
150-
logger.verbose('Parsing configured contexts completed.');
149+
this.logger.verbose('Parsing configured contexts completed.');
151150
}
152151

153152
deriveContextComponent(contextComponent) {
@@ -165,15 +164,15 @@ export default class ApplicationContext {
165164
}
166165

167166
parseContextComponents(context) {
168-
logger.verbose('Processing context components started');
167+
this.logger.verbose('Processing context components started');
169168
if (context.components) {
170169
if (Array.isArray(context.components)) {
171170
for (let i = 0; i < context.components.length; i++) {
172171
this.deriveContextComponent(context.components[i]);
173172
}
174173
}
175174
}
176-
logger.verbose('Processing context components completed');
175+
this.logger.verbose('Processing context components completed');
177176
}
178177

179178
parseContextComponent(componentArg) {
@@ -201,7 +200,7 @@ export default class ApplicationContext {
201200
$component.factoryFunction = component.factoryFunction;
202201
$component.factoryArgs = component.factoryArgs;
203202
$component.wireFactory = component.wireFactory;
204-
//TODO - dynamic import (async)
203+
// TODO - dynamic import (async)
205204
if (component.require) {
206205
// eslint-disable-next-line
207206
// import(component.require).then(
@@ -234,19 +233,19 @@ export default class ApplicationContext {
234233
if ($component.isActive) {
235234
if (!this.components[$component.name]) {
236235
this.components[$component.name] = $component;
237-
logger.verbose(`Added application context component (${$component.name}) with ${$component.scope} scope`);
236+
this.logger.verbose(`Added application context component (${$component.name}) with ${$component.scope} scope`);
238237
} else {
239238
const msg = `Duplicate definition of application context component (${$component.name})`;
240-
logger.error(msg);
239+
this.logger.error(msg);
241240
throw new Error(msg);
242241
}
243242
} else {
244-
logger.verbose(`Skipped inactive application context component (${$component.name}), with scope ${$component.scope}`);
243+
this.logger.verbose(`Skipped inactive application context component (${$component.name}), with scope ${$component.scope}`);
245244
}
246245
}
247246

248247
createSingletons() {
249-
logger.verbose('Creating singletons started');
248+
this.logger.verbose('Creating singletons started');
250249
const keys = Object.keys(this.components);
251250
for (let i = 0; i < keys.length; i++) {
252251
const component = this.components[keys[i]];
@@ -263,10 +262,10 @@ export default class ApplicationContext {
263262
} else {
264263
component.instance = component.Reference;
265264
}
266-
logger.verbose(`Created singleton (${component.name})`);
265+
this.logger.verbose(`Created singleton (${component.name})`);
267266
}
268267
}
269-
logger.verbose('Creating singletons completed');
268+
this.logger.verbose('Creating singletons completed');
270269
}
271270

272271
resolveConfigPlaceHolder(placeholderArg) {
@@ -279,7 +278,7 @@ export default class ApplicationContext {
279278
returnValue = this.config.get(path, defaultValue ? JSON.parse(defaultValue) : defaultValue);
280279
} catch (e) {
281280
const msg = `Failed to resolve placeholder component property value (${path}) from config.`;
282-
logger.error(msg);
281+
this.logger.error(msg);
283282
throw new Error(msg);
284283
}
285284
return returnValue;
@@ -294,23 +293,23 @@ export default class ApplicationContext {
294293
if (autowire) {
295294
// eslint-disable-next-line no-param-reassign
296295
instance[insKeys[j]] = this.get(insKeys[j], undefined, component);
297-
logger.verbose(`Explicitly autowired component (${component.name}) property (${insKeys[j]}) from context.`);
296+
this.logger.verbose(`Explicitly autowired component (${component.name}) property (${insKeys[j]}) from context.`);
298297
} else if (instance[insKeys[j]] == null) {
299298
// eslint-disable-next-line no-param-reassign
300299
instance[insKeys[j]] = this.get(insKeys[j], (instance[insKeys[j]] || null), component);
301300
if (instance[insKeys[j]] != null) {
302-
logger.verbose(`Implicitly autowired null component (${component.name}) property (${insKeys[j]}) from context.`);
301+
this.logger.verbose(`Implicitly autowired null component (${component.name}) property (${insKeys[j]}) from context.`);
303302
}
304303
} else if (typeof instance[insKeys[j]] === 'string' && instance[insKeys[j]].startsWith('${')) {
305304
try {
306305
// eslint-disable-next-line no-param-reassign
307306
instance[insKeys[j]] = this.resolveConfigPlaceHolder(instance[insKeys[j]]);
308307
} catch (e) {
309308
const msg = `Failed to explicitly autowired placeholder component (${component.name}) property value (${insKeys[j]}) from config.`;
310-
logger.error(msg);
309+
this.logger.error(msg);
311310
throw new Error(msg);
312311
}
313-
logger.verbose(`Explicitly autowired placeholder component (${component.name}) property value (${insKeys[j]}) from config.`);
312+
this.logger.verbose(`Explicitly autowired placeholder component (${component.name}) property value (${insKeys[j]}) from config.`);
314313
}
315314
}
316315
}
@@ -332,17 +331,17 @@ export default class ApplicationContext {
332331
if (property.reference) {
333332
// eslint-disable-next-line no-param-reassign
334333
component.instance[property.name] = this.get(property.reference, undefined, component);
335-
logger.verbose(`Explicitly wired component (${component.name}) property (${property.name}) with context reference (${property.reference}).`);
334+
this.logger.verbose(`Explicitly wired component (${component.name}) property (${property.name}) with context reference (${property.reference}).`);
336335
}
337336
if (property.value) {
338337
// eslint-disable-next-line no-param-reassign
339338
component.instance[property.name] = property.value;
340-
logger.verbose(`Explicitly wired component (${component.name}) property (${property.name}) with value (${property.value}).`);
339+
this.logger.verbose(`Explicitly wired component (${component.name}) property (${property.name}) with value (${property.value}).`);
341340
}
342341
if (property.path) {
343342
// eslint-disable-next-line no-param-reassign
344343
component.instance[property.name] = this.config.get(property.path, property.defaultValue);
345-
logger.verbose(`Explicitly wired component (${component.name}) property (${property.name}) from config path (${property.path}).`);
344+
this.logger.verbose(`Explicitly wired component (${component.name}) property (${property.name}) from config path (${property.path}).`);
346345
}
347346
}
348347
}
@@ -360,7 +359,7 @@ export default class ApplicationContext {
360359
}
361360

362361
injectSingletonDependencies() {
363-
logger.verbose('Injecting singletons dependencies started');
362+
this.logger.verbose('Injecting singletons dependencies started');
364363
const keys = Object.keys(this.components);
365364
for (let i = 0; i < keys.length; i++) {
366365
const component = this.components[keys[i]];
@@ -369,11 +368,11 @@ export default class ApplicationContext {
369368
this.wireComponentDependencies(component);
370369
}
371370
}
372-
logger.verbose('Injecting singleton dependencies completed');
371+
this.logger.verbose('Injecting singleton dependencies completed');
373372
}
374373

375374
initialiseSingletons() {
376-
logger.verbose('Initialising singletons started');
375+
this.logger.verbose('Initialising singletons started');
377376
const keys = Object.keys(this.components);
378377
for (let i = 0; i < keys.length; i++) {
379378
const component = this.components[keys[i]];
@@ -383,14 +382,14 @@ export default class ApplicationContext {
383382
} else if (typeof component.init === 'string') {
384383
component.instance[component.init]();
385384
}
386-
logger.verbose(`Initialised singleton (${component.name})`);
385+
this.logger.verbose(`Initialised singleton (${component.name})`);
387386
}
388387
}
389-
logger.verbose('Initialising singletons completed');
388+
this.logger.verbose('Initialising singletons completed');
390389
}
391390

392391
static registerDestroyer(destroyer) {
393-
if (destroyer) {
392+
if (typeof (process) !== 'undefined' && destroyer) {
394393
// process.on('exit', destroyer?.bind());
395394
// catches ctrl+c event
396395
process.on('SIGINT', destroyer?.bind());
@@ -403,7 +402,7 @@ export default class ApplicationContext {
403402
}
404403

405404
async registerSingletonDestroyers() {
406-
logger.verbose('Registering singleton destroyers started');
405+
this.logger.verbose('Registering singleton destroyers started');
407406
const keys = Object.keys(this.components);
408407
for (let i = 0; i < keys.length; i++) {
409408
const component = this.components[keys[i]];
@@ -415,13 +414,13 @@ export default class ApplicationContext {
415414
destroyer = () => component.instance[component.destroy](component.instance);
416415
}
417416
ApplicationContext.registerDestroyer(destroyer);
418-
logger.verbose(`Registering singleton (${component.name}) destroyer`);
417+
this.logger.verbose(`Registering singleton (${component.name}) destroyer`);
419418
}
420419
}
421420
ApplicationContext.registerDestroyer(() => {
422-
logger.verbose(`ApplicationContext (${this.name}) lifecycle completed.`);
421+
this.logger.verbose(`ApplicationContext (${this.name}) lifecycle completed.`);
423422
});
424-
logger.verbose('Registering singleton destroyers completed');
423+
this.logger.verbose('Registering singleton destroyers completed');
425424
}
426425

427426
async run() {
@@ -436,19 +435,19 @@ export default class ApplicationContext {
436435
}
437436
}
438437
}
439-
logger.verbose('Application context started');
438+
this.logger.verbose('Application context started');
440439
}
441440

442441
get(reference, defaultValue, targetArgs) {
443442
if (this.components[reference]) {
444-
logger.verbose(`Found component (${reference})`);
443+
this.logger.verbose(`Found component (${reference})`);
445444
if (this.components[reference].scope === Scopes.SINGLETON) {
446-
logger.verbose(`Component (${reference}) is scoped as (${Scopes.SINGLETON}), returning existing instance.`);
445+
this.logger.verbose(`Component (${reference}) is scoped as (${Scopes.SINGLETON}), returning existing instance.`);
447446
return this.components[reference].instance;
448447
}
449448
let prototype = null;
450449
if (this.components[reference].isClass) {
451-
logger.verbose(`Component (${reference}) is scoped as (${Scopes.PROTOTYPE}), returning new instance.`);
450+
this.logger.verbose(`Component (${reference}) is scoped as (${Scopes.PROTOTYPE}), returning new instance.`);
452451
prototype = new this.components[reference].Reference();
453452
} else if (typeof this.components[reference].Reference === 'function') {
454453
let args = targetArgs || this.components[reference].factoryArgs;
@@ -484,15 +483,15 @@ export default class ApplicationContext {
484483
const factory = this.get(this.components[reference].wireFactory);
485484
prototype = factory[this.components[reference].factoryFunction](...args);
486485
} else {
487-
logger.verbose(`Component (${reference}) is scoped as (${Scopes.PROTOTYPE}), returning deep clone.`);
486+
this.logger.verbose(`Component (${reference}) is scoped as (${Scopes.PROTOTYPE}), returning deep clone.`);
488487
prototype = _.cloneDeep(this.components[reference].Reference);
489488
}
490489
this.autowireComponentDependencies(prototype, this.components[reference]);
491490
return prototype;
492491
}
493492
if (typeof defaultValue === 'undefined') {
494493
const msg = `Failed component reference lookup for (${reference})`;
495-
logger.error(msg);
494+
this.logger.error(msg);
496495
throw new Error(msg);
497496
}
498497
return defaultValue;

0 commit comments

Comments
 (0)