Skip to content

Commit de6faa6

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

File tree

8 files changed

+125
-102
lines changed

8 files changed

+125
-102
lines changed

ApplicationContext.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ export default class ApplicationContext {
6666
}
6767

6868
async start() {
69-
return this.lifeCycle();
69+
await this.lifeCycle();
7070
}
7171

7272
async lifeCycle() {
7373
this.logger.verbose(`ApplicationContext (${this.name}) lifecycle started.`);
74-
this.parseContexts();
74+
await this.parseContexts();
7575
this.createSingletons();
7676
this.injectSingletonDependencies();
7777
this.initialiseSingletons();
@@ -129,15 +129,15 @@ export default class ApplicationContext {
129129
this.logger.verbose('Detecting global context components completed.');
130130
}
131131

132-
parseContexts() {
132+
async parseContexts() {
133133
this.logger.verbose('Parsing configured contexts started.');
134134
this.detectConfigContext();
135135
for (let i = 0; i < this.contexts.length; i++) {
136136
if (this.contexts[i]) {
137137
if (this.contexts[i]?.constructor?.name === 'Context') {
138-
this.parseContextComponents(this.contexts[i]);
138+
await this.parseContextComponents(this.contexts[i]);
139139
} else {
140-
this.parseContextComponents(new Context(this.contexts[i]));
140+
await this.parseContextComponents(new Context(this.contexts[i]));
141141
}
142142
} else {
143143
const msg = `ApplicationContext (${this.name}) received a nullish context.`;
@@ -149,33 +149,33 @@ export default class ApplicationContext {
149149
this.logger.verbose('Parsing configured contexts completed.');
150150
}
151151

152-
deriveContextComponent(contextComponent) {
152+
async deriveContextComponent(contextComponent) {
153153
if (contextComponent.name || contextComponent.Reference || contextComponent.factory) {
154-
this.parseContextComponent(contextComponent);
154+
await this.parseContextComponent(contextComponent);
155155
} else {
156156
const contextKeys = Object.keys(contextComponent);
157157
for (let i = 0; i < contextKeys.length; i++) {
158158
const name = contextKeys[i];
159159
const component = contextComponent[name];
160160
component.name = name;
161-
this.parseContextComponent(component);
161+
await this.parseContextComponent(component);
162162
}
163163
}
164164
}
165165

166-
parseContextComponents(context) {
166+
async parseContextComponents(context) {
167167
this.logger.verbose('Processing context components started');
168168
if (context.components) {
169169
if (Array.isArray(context.components)) {
170170
for (let i = 0; i < context.components.length; i++) {
171-
this.deriveContextComponent(context.components[i]);
171+
await this.deriveContextComponent(context.components[i]);
172172
}
173173
}
174174
}
175175
this.logger.verbose('Processing context components completed');
176176
}
177177

178-
parseContextComponent(componentArg) {
178+
async parseContextComponent(componentArg) {
179179
let component = componentArg;
180180
if (component?.constructor?.name !== 'Component'
181181
&& component?.constructor?.name !== 'Singleton'
@@ -202,13 +202,17 @@ export default class ApplicationContext {
202202
$component.wireFactory = component.wireFactory;
203203
// TODO - dynamic import (async)
204204
if (component.require) {
205+
try{
205206
// eslint-disable-next-line
206-
// import(component.require).then(
207-
// (module) => { $component.Reference = module.default(); },
208-
// );
209-
// $component.isClass = ($component?.Reference?.prototype?.constructor !== undefined);
207+
let module = await import(component.require);
208+
$component.Reference = module.default;
209+
$component.isClass = ($component?.Reference?.prototype?.constructor !== undefined);
210+
} catch (err) {
211+
this.logger.error(err);
212+
}
210213
}
211214

215+
212216
$component.properties = component.properties || constructr?.properties;
213217
$component.profiles = component.profiles || constructr?.profiles;
214218
if (!$component.profiles) {

config/local-development.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"^^^ set a format to json for JSON output" : "doco",
55
"level": {
66
"/" : "info",
7-
"@alt-javascript/cdi" : "info",
7+
"@alt-javascript/cdi" : "verbose",
88
"^^^ set a path level to enable log lines at that folder, down to file/module" : "doco"
99
},
1010
"test" : {

server.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { config } from '@alt-javascript/config';
2+
import { test } from '@alt-javascript/boot';
3+
import { LoggerFactory } from '@alt-javascript/logger';
4+
import {ApplicationContext} from "./index.js";
5+
6+
test({ config });
7+
8+
const logger = LoggerFactory.getLogger('@alt-javascript/cdi/server');
9+
10+
const context = { name: 'SimpleClass',
11+
require: './test/service/SimpleClass.js',
12+
};
13+
14+
const applicationContext = new ApplicationContext(context,config);
15+
await applicationContext.start();
16+
17+
const simpleClass = applicationContext.get('simpleClass');
18+
19+
logger.info(`Simple class is ${simpleClass}`);

test/ApplicationContext.spec.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,70 +63,70 @@ describe('ApplicationContext Specification', () => {
6363
await assert.isRejected(applicationContext.start(), Error, 'Duplicate definition of application context component (simpleClass)');
6464
});
6565

66-
it('ApplicationContext accepts Context array', () => {
66+
it('ApplicationContext accepts Context array', async () => {
6767
const context = new Context([new Component(SimpleClass)]);
6868

6969
const applicationContext = new ApplicationContext([context]);
70-
applicationContext.start();
70+
await applicationContext.start();
7171

7272
const simpleClass = applicationContext.get('simpleClass');
7373
assert.exists(simpleClass, 'simpleClass exists');
7474
});
7575

76-
it('ApplicationContext accepts Context object', () => {
76+
it('ApplicationContext accepts Context object', async () => {
7777
const context = new Context(new Component(SimpleClass));
7878

7979
const applicationContext = new ApplicationContext(context);
80-
applicationContext.start();
80+
await applicationContext.start();
8181

8282
const simpleClass = applicationContext.get('simpleClass');
8383
assert.exists(simpleClass, 'simpleClass exists');
8484
});
8585

86-
it('ApplicationContext accepts Component object', () => {
86+
it('ApplicationContext accepts Component object', async () => {
8787
const context = new Component(SimpleClass);
8888

8989
const applicationContext = new ApplicationContext(context);
90-
applicationContext.start();
90+
await applicationContext.start();
9191

9292
const simpleClass = applicationContext.get('simpleClass');
9393
assert.exists(simpleClass, 'simpleClass exists');
9494
});
9595

96-
it('ApplicationContext accepts plain old class', () => {
96+
it('ApplicationContext accepts plain old class', async () => {
9797
const context = SimpleClass;
9898

9999
const applicationContext = new ApplicationContext(context);
100-
applicationContext.start();
100+
await applicationContext.start();
101101

102102
const simpleClass = applicationContext.get('simpleClass');
103103
assert.exists(simpleClass, 'simpleClass exists');
104104
});
105105

106-
it('ApplicationContext accepts plain old object', () => {
106+
it('ApplicationContext accepts plain old object', async () => {
107107
const context = { name: 'SimpleClass', uuid: uuidv4() };
108108

109109
const applicationContext = new ApplicationContext(context);
110-
applicationContext.start();
110+
await applicationContext.start();
111111

112112
const simpleClass = applicationContext.get('simpleClass');
113113
assert.exists(simpleClass, 'simpleClass exists');
114114
});
115115

116-
// it('ApplicationContext accepts plain old object, with require', () => {
117-
// const context = { name: 'SimpleClass',
118-
// require: './test/service/SimpleClass',
119-
// };
120-
//
121-
// const applicationContext = new ApplicationContext(context);
122-
// applicationContext.start();
123-
//
124-
// const simpleClass = applicationContext.get('simpleClass');
125-
// assert.exists(simpleClass, 'simpleClass exists');
126-
// assert.exists(simpleClass.uuid, 'simpleClass.uuid exists');
127-
// });
128-
129-
it('ApplicationContext accepts config context', () => {
116+
it('ApplicationContext accepts plain old object, with require', async() => {
117+
const context = { name: 'SimpleClass',
118+
require: './test/service/SimpleClass.js',
119+
};
120+
121+
const applicationContext = new ApplicationContext(context);
122+
await applicationContext.start();
123+
124+
const simpleClass = applicationContext.get('simpleClass');
125+
assert.exists(simpleClass, 'simpleClass exists');
126+
assert.exists(simpleClass.uuid, 'simpleClass.uuid exists');
127+
});
128+
129+
it('ApplicationContext accepts config context', async() => {
130130
const ephemeralConfig = new EphemeralConfig(
131131
{
132132
context: {
@@ -136,7 +136,7 @@ describe('ApplicationContext Specification', () => {
136136
);
137137

138138
const applicationContext = new ApplicationContext({ config: ephemeralConfig });
139-
applicationContext.start();
139+
await applicationContext.start();
140140

141141
const simpleClass = applicationContext.get('simpleClass');
142142
assert.exists(simpleClass, 'simpleClass exists');

test/Profiles.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ beforeEach(async () => {
3030
});
3131

3232
describe('Profile Specification', () => {
33-
it('Component with no profile is active when active profiles set', () => {
33+
it('Component with no profile is active when active profiles set', async () => {
3434
const context = { name: 'SimpleClass', attr: 'value' };
3535

3636
const applicationContext = new ApplicationContext({ contexts: context, profiles: 'default' });
37-
applicationContext.start();
37+
await applicationContext.start();
3838

3939
const simpleClass = applicationContext.get('simpleClass');
4040
assert.exists(simpleClass, 'simpleClass exists');

test/Prototype.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ describe('Prototypes Specification', () => {
163163
assert.equal(funcy.attr, 'value', 'funcy.attr == value');
164164
});
165165

166-
it('Simple Prototype using singleton factory function', () => {
166+
it('Simple Prototype using singleton factory function', async() => {
167167
const context = new Context([
168168
{
169169
name: 'singletonFactory',
@@ -177,13 +177,13 @@ describe('Prototypes Specification', () => {
177177
})]);
178178

179179
const applicationContext = new ApplicationContext([context]);
180-
applicationContext.start();
180+
await applicationContext.start();
181181
const factoryProto = applicationContext.get('factoryProto');
182182
assert.exists(factoryProto, 'factoryProto exists');
183183
assert.equal(factoryProto.attr, 'one', 'factoryProto.attr == one');
184184
});
185185

186-
it('Simple Singleton wires logger prototype with wireFactory', () => {
186+
it('Simple Singleton wires logger prototype with wireFactory', async () => {
187187
const context = new Context(
188188
{
189189
name: 'simpleSingleton',
@@ -193,7 +193,7 @@ describe('Prototypes Specification', () => {
193193
);
194194

195195
const applicationContext = new ApplicationContext([context]);
196-
applicationContext.start();
196+
await applicationContext.start();
197197
const simpleSingleton = applicationContext.get('simpleSingleton');
198198
assert.exists(simpleSingleton, 'simpleSingleton exists');
199199
assert.exists(simpleSingleton.logger, 'simpleSingleton.logger exists');

test/Singleton.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,30 @@ beforeEach(async () => {
3434
});
3535

3636
describe('Singleton Specification', () => {
37-
it('Simple Component is a singleton', () => {
37+
it('Simple Component is a singleton', async() => {
3838
const context = new Context([new Component(SimpleClass)]);
3939

4040
const applicationContext = new ApplicationContext([context]);
41-
applicationContext.start();
41+
await applicationContext.start();
4242
const simpleClass = applicationContext.get('simpleClass');
4343
const simpleClass2 = applicationContext.get('simpleClass');
4444
assert.exists(simpleClass, 'simpleClass exists');
4545
assert.equal(simpleClass.uuid, simpleClass2.uuid, 'simpleClass.uuid === simpleClass2.uuid');
4646
});
4747

48-
it('Simple Singleton is a singleton', () => {
48+
it('Simple Singleton is a singleton', async () => {
4949
const context = new Context([new Service(SimpleClass)]);
5050

5151
const applicationContext = new ApplicationContext([context]);
52-
applicationContext.start();
52+
await applicationContext.start();
5353
const simpleClass = applicationContext.get('simpleClass');
5454
assert.exists(simpleClass, 'simpleClass exists');
5555
});
56-
it('Simple Singleton is a singleton using Reference', () => {
56+
it('Simple Singleton is a singleton using Reference', async () => {
5757
const context = new Context([new Singleton({ Reference: SimpleClass })]);
5858

5959
const applicationContext = new ApplicationContext([context]);
60-
applicationContext.start();
60+
await applicationContext.start();
6161
const simpleClass = applicationContext.get('simpleClass');
6262
assert.exists(simpleClass, 'simpleClass exists');
6363
});

0 commit comments

Comments
 (0)