@xrystal/core 3.13.4 → 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,7 +1,7 @@
1
1
  {
2
2
  "author": "Yusuf Yasir KAYGUSUZ",
3
3
  "name": "@xrystal/core",
4
- "version": "3.13.4",
4
+ "version": "3.13.6",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -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,
@@ -40,9 +41,7 @@ export class X {
40
41
  const className = dependency.name;
41
42
  const name = className.charAt(0).toLowerCase() + className.slice(1);
42
43
  if (!this.isRegistered(name)) {
43
- this.container.register({
44
- [name]: asClass(dependency).singleton()
45
- });
44
+ this.container.register({ [name]: asClass(dependency).singleton() });
46
45
  if (verbose)
47
46
  console.log(`[DI] Registered: ${name}`);
48
47
  }
@@ -52,8 +51,6 @@ export class X {
52
51
  console.error(`[DI] Critical: Failed to load module at ${m.path}:`, err.message);
53
52
  }
54
53
  }
55
- if (verbose)
56
- console.log('[DI] Load completed. Total registrations:', Object.keys(this.container.registrations).length);
57
54
  return this;
58
55
  }
59
56
  register(Dependency) {
@@ -62,9 +59,7 @@ export class X {
62
59
  const name = Dependency.name.charAt(0).toLowerCase() + Dependency.name.slice(1);
63
60
  if (this.isRegistered(name))
64
61
  return this;
65
- this.container.register({
66
- [name]: asClass(Dependency).singleton()
67
- });
62
+ this.container.register({ [name]: asClass(Dependency).singleton() });
68
63
  return this;
69
64
  }
70
65
  registerAll(dependencies) {
@@ -74,58 +69,77 @@ export class X {
74
69
  return this;
75
70
  }
76
71
  registerInstance(name, instance) {
77
- if (!name || this.isRegistered(name))
72
+ if (!name)
78
73
  return this;
79
74
  const formattedName = name.charAt(0).toLowerCase() + name.slice(1);
80
- this.container.register({
81
- [formattedName]: asValue(instance)
82
- });
75
+ if (this.isRegistered(formattedName))
76
+ return this;
77
+ this.container.register({ [formattedName]: asValue(instance) });
78
+ return this;
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);
83
86
  return this;
84
87
  }
85
88
  async initialize(input, verbose = false) {
86
89
  const cradle = this.container.cradle;
87
90
  const inputList = input ? (Array.isArray(input) ? input : [input]) : [];
88
- const propsMap = new Map();
89
- const initializedNames = [];
90
- inputList.forEach(item => {
91
- if (item?.service) {
92
- const name = typeof item.service === 'function'
93
- ? item.service.name.charAt(0).toLowerCase() + item.service.name.slice(1)
94
- : item.service;
95
- propsMap.set(name, item.props);
91
+ for (const item of inputList) {
92
+ if (!item?.service)
93
+ continue;
94
+ const name = typeof item.service === 'function'
95
+ ? item.service.name.charAt(0).toLowerCase() + item.service.name.slice(1)
96
+ : item.service;
97
+ if (this.initializedNames.has(name))
98
+ continue;
99
+ const instance = cradle[name];
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
+ }
110
+ }
111
+ this.initializedNames.add(name);
96
112
  }
97
- });
113
+ }
98
114
  for (const key of Object.keys(this.container.registrations)) {
115
+ if (this.initializedNames.has(key))
116
+ continue;
99
117
  const instance = cradle[key];
100
118
  if (instance && typeof instance.load === 'function') {
101
119
  try {
102
- const props = propsMap.get(key) || {};
103
- await instance.load(props);
104
- initializedNames.push(key);
120
+ await instance.load({});
121
+ if (verbose)
122
+ console.log(`[DI] Initialized (Auto): ${key}`);
105
123
  }
106
124
  catch (err) {
107
- console.error(`[DI] Critical: Service "${key}" failed to initialize:`, err.message);
125
+ console.error(`[DI] Critical: Auto service "${key}" failed:`, err.message);
108
126
  }
109
127
  }
110
- }
111
- if (verbose && initializedNames.length > 0) {
112
- console.log('[DI] Successfully initialized:', initializedNames);
128
+ this.initializedNames.add(key);
113
129
  }
114
130
  return this;
115
131
  }
116
132
  get(target) {
117
133
  if (!target)
118
- throw new Error('[DI] Target is required for resolution');
134
+ throw new Error('[DI] Target is required');
119
135
  const resolveName = typeof target === 'function'
120
136
  ? target.name.charAt(0).toLowerCase() + target.name.slice(1)
121
137
  : target;
122
138
  return this.container.resolve(resolveName);
123
139
  }
124
- get cradle() {
125
- return this.container.cradle;
126
- }
140
+ get cradle() { return this.container.cradle; }
127
141
  isRegistered(name) {
128
- return !!this.container.registrations[name];
142
+ return !!this.container.registrations[name] && this.container.registrations[name].resolve !== undefined;
129
143
  }
130
144
  }
131
145
  export default new X();