-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
146 lines (129 loc) · 4.67 KB
/
index.js
File metadata and controls
146 lines (129 loc) · 4.67 KB
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
/**
* @alt-javascript/boot-angular — Angular integration for CDI.
*
* Provides utilities to bridge CDI ApplicationContext into Angular
* applications via Angular's dependency injection system. CDI beans
* are registered as Angular providers using InjectionToken.
*
* Usage in Angular module:
* import { createCdiProviders } from '@alt-javascript/boot-angular';
*
* // In app.module.ts or main.ts (standalone)
* const cdiProviders = await createCdiProviders({ contexts, config });
*
* bootstrapApplication(AppComponent, {
* providers: [...cdiProviders],
* });
*
* // In components:
* @Component({ ... })
* export class TodoComponent {
* constructor(@Inject('todoService') private todoService: TodoService) {}
* }
*
* Angular's DI and CDI operate at different layers:
* - CDI manages application services (framework-agnostic business logic)
* - Angular DI manages UI components and Angular-specific services
* - This bridge registers CDI beans as Angular value providers
*
* Note: This package does NOT depend on Angular at runtime. It produces
* provider config objects that Angular's bootstrapApplication() consumes.
*/
import { Boot } from '@alt-javascript/boot';
import { ApplicationContext } from '@alt-javascript/cdi';
/**
* Boot CDI and produce Angular provider definitions.
*
* Returns an array of { provide, useValue } objects compatible with
* Angular's providers array.
*
* @param {object} options
* @param {Array} options.contexts — CDI Context instances
* @param {object} options.config — config object
* @returns {Promise<{ applicationContext, providers }>}
*/
export async function createCdiProviders(options) {
const { contexts, config } = options;
const appCtx = new ApplicationContext({ contexts, config });
await appCtx.start({ run: false });
const providers = [
{ provide: 'applicationContext', useValue: appCtx },
];
const components = appCtx.components;
for (const name of Object.keys(components)) {
if (components[name].instance) {
providers.push({ provide: name, useValue: components[name].instance });
}
}
return { applicationContext: appCtx, providers };
}
/**
* Utility: wrap CDI beans as a lookup service injectable in Angular.
*
* Angular components can inject this service and call getBean() to
* resolve CDI beans by name — useful when you don't want a separate
* provider for every bean.
*
* @Component({ ... })
* export class MyComponent {
* constructor(@Inject('cdiService') private cdi: CdiService) {
* const svc = this.cdi.getBean('greetingService');
* }
* }
*/
export class CdiService {
constructor(applicationContext) {
this._ctx = applicationContext;
}
getBean(name) {
return this._ctx.get(name);
}
get applicationContext() {
return this._ctx;
}
}
/**
* Create providers including a CdiService for dynamic bean lookup.
*
* @param {object} options — same as createCdiProviders
* @returns {Promise<{ applicationContext, providers }>}
*/
export async function createCdiProvidersWithService(options) {
const { applicationContext, providers } = await createCdiProviders(options);
const cdiService = new CdiService(applicationContext);
providers.push({ provide: 'cdiService', useValue: cdiService });
return { applicationContext, providers };
}
/**
* Boot CDI via Boot.boot() and produce Angular provider definitions.
*
* Mirrors the vueStarter/reactStarter pattern — calls Boot.boot() for full
* profile URL resolution, ${placeholder} expansion, banner, logger setup,
* and CDI wiring. Config may be a plain POJO.
*
* Returns Angular provider objects ready for bootstrapApplication():
*
* const { providers } = await angularStarter({ contexts, config });
* bootstrapApplication(AppComponent, { providers });
*
* @param {object} options
* @param {Array} options.contexts — CDI Context instances (required)
* @param {object} [options.config] — config POJO or config object
* @returns {Promise<{ applicationContext, providers }>}
*/
export async function angularStarter(options) {
const { contexts } = options;
if (!contexts) throw new Error('angularStarter: options.contexts is required.');
const appCtx = await Boot.boot({ config: options.config, contexts, run: false });
const providers = [
{ provide: 'applicationContext', useValue: appCtx },
];
for (const name of Object.keys(appCtx.components)) {
if (appCtx.components[name].instance) {
providers.push({ provide: name, useValue: appCtx.components[name].instance });
}
}
const cdiService = new CdiService(appCtx);
providers.push({ provide: 'cdiService', useValue: cdiService });
return { applicationContext: appCtx, providers };
}