@xrystal/core 3.13.5 → 3.13.6
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/package.json
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
export declare class X {
|
|
2
2
|
private container;
|
|
3
|
+
private initializedNames;
|
|
3
4
|
constructor();
|
|
4
5
|
load(patterns: string | string[], verbose?: boolean): this;
|
|
5
6
|
register(Dependency: any): this;
|
|
6
7
|
registerAll(dependencies: any[]): this;
|
|
7
8
|
registerInstance(name: string, instance: any): this;
|
|
9
|
+
remove(target: string | any): this;
|
|
8
10
|
initialize(input?: {
|
|
9
11
|
service: any;
|
|
10
12
|
props?: any;
|
|
@@ -2,6 +2,7 @@ import { createContainer, asClass, asValue, InjectionMode, listModules } from 'a
|
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
export class X {
|
|
4
4
|
container;
|
|
5
|
+
initializedNames = new Set();
|
|
5
6
|
constructor() {
|
|
6
7
|
this.container = createContainer({
|
|
7
8
|
injectionMode: InjectionMode.PROXY,
|
|
@@ -68,43 +69,55 @@ export class X {
|
|
|
68
69
|
return this;
|
|
69
70
|
}
|
|
70
71
|
registerInstance(name, instance) {
|
|
71
|
-
if (!name
|
|
72
|
+
if (!name)
|
|
72
73
|
return this;
|
|
73
74
|
const formattedName = name.charAt(0).toLowerCase() + name.slice(1);
|
|
75
|
+
if (this.isRegistered(formattedName))
|
|
76
|
+
return this;
|
|
74
77
|
this.container.register({ [formattedName]: asValue(instance) });
|
|
75
78
|
return this;
|
|
76
79
|
}
|
|
80
|
+
remove(target) {
|
|
81
|
+
const name = typeof target === 'function'
|
|
82
|
+
? target.name.charAt(0).toLowerCase() + target.name.slice(1)
|
|
83
|
+
: target;
|
|
84
|
+
this.container.register(name, asValue(undefined));
|
|
85
|
+
this.initializedNames.delete(name);
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
77
88
|
async initialize(input, verbose = false) {
|
|
78
89
|
const cradle = this.container.cradle;
|
|
79
90
|
const inputList = input ? (Array.isArray(input) ? input : [input]) : [];
|
|
80
|
-
const initializedNames = new Set();
|
|
81
91
|
for (const item of inputList) {
|
|
82
92
|
if (!item?.service)
|
|
83
93
|
continue;
|
|
84
94
|
const name = typeof item.service === 'function'
|
|
85
95
|
? item.service.name.charAt(0).toLowerCase() + item.service.name.slice(1)
|
|
86
96
|
: item.service;
|
|
97
|
+
if (this.initializedNames.has(name))
|
|
98
|
+
continue;
|
|
87
99
|
const instance = cradle[name];
|
|
88
|
-
if (instance
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
100
|
+
if (instance) {
|
|
101
|
+
if (typeof instance.load === 'function') {
|
|
102
|
+
try {
|
|
103
|
+
await instance.load(item.props || {});
|
|
104
|
+
if (verbose)
|
|
105
|
+
console.log(`[DI] Initialized (Priority): ${name}`);
|
|
106
|
+
}
|
|
107
|
+
catch (err) {
|
|
108
|
+
console.error(`[DI] Critical: Priority service "${name}" failed:`, err.message);
|
|
109
|
+
}
|
|
97
110
|
}
|
|
111
|
+
this.initializedNames.add(name);
|
|
98
112
|
}
|
|
99
113
|
}
|
|
100
114
|
for (const key of Object.keys(this.container.registrations)) {
|
|
101
|
-
if (initializedNames.has(key))
|
|
115
|
+
if (this.initializedNames.has(key))
|
|
102
116
|
continue;
|
|
103
117
|
const instance = cradle[key];
|
|
104
118
|
if (instance && typeof instance.load === 'function') {
|
|
105
119
|
try {
|
|
106
120
|
await instance.load({});
|
|
107
|
-
initializedNames.add(key);
|
|
108
121
|
if (verbose)
|
|
109
122
|
console.log(`[DI] Initialized (Auto): ${key}`);
|
|
110
123
|
}
|
|
@@ -112,6 +125,7 @@ export class X {
|
|
|
112
125
|
console.error(`[DI] Critical: Auto service "${key}" failed:`, err.message);
|
|
113
126
|
}
|
|
114
127
|
}
|
|
128
|
+
this.initializedNames.add(key);
|
|
115
129
|
}
|
|
116
130
|
return this;
|
|
117
131
|
}
|
|
@@ -124,6 +138,8 @@ export class X {
|
|
|
124
138
|
return this.container.resolve(resolveName);
|
|
125
139
|
}
|
|
126
140
|
get cradle() { return this.container.cradle; }
|
|
127
|
-
isRegistered(name) {
|
|
141
|
+
isRegistered(name) {
|
|
142
|
+
return !!this.container.registrations[name] && this.container.registrations[name].resolve !== undefined;
|
|
143
|
+
}
|
|
128
144
|
}
|
|
129
145
|
export default new X();
|