1- import { Container } from "@/di/Container" ;
2- import type { Constructor , Lifetime } from "@/types" ;
1+ import type { Lifetime } from "@/types" ;
32import { injectionMetadata } from "@/utils/meta-data" ;
43
54type InjectableOptions = {
65 lifetime ?: Lifetime ;
76 token ?: object ; // For interface-based DI
87} ;
98
10- export function Inject ( token : unknown ) {
11- return ( target : object , _ : string | symbol , parameterIndex : number ) => {
12- const metadata = injectionMetadata . get ( target ) ?? [ ] ;
13- metadata [ parameterIndex ] = token ;
14- injectionMetadata . set ( target , metadata ) ;
9+ export function Inject < T > ( token : T ) {
10+ return (
11+ target : object ,
12+ _propertyKey : string | symbol | undefined ,
13+ parameterIndex : number
14+ ) => {
15+ if ( _propertyKey === undefined ) {
16+ // Store metadata on the class constructor
17+ const ctor = target . constructor ;
18+ const metadata = injectionMetadata . get ( ctor ) ?? [ ] ;
19+
20+ metadata [ parameterIndex ] = token ;
21+ injectionMetadata . set ( ctor , metadata ) ;
22+ }
1523 } ;
1624}
1725
@@ -20,38 +28,34 @@ export function Injectable(options?: InjectableOptions) {
2028 const token = options ?. token ?? target ;
2129 const lifetime = options ?. lifetime ?? "singleton" ;
2230
23- // Register the class in the container
24- Container . register (
25- token ,
26- ( ) => {
27- // Use the existing createInstance function for dependency resolution
28- return createInstance ( target ) ;
29- } ,
30- lifetime
31- ) ;
32-
33- // Store original constructor for potential edge cases
34- target . __originalConstructor = target . prototype . constructor ;
35-
31+ // Preserve original constructor for inheritance
32+ target . __originalConstructor = target ;
33+ // console.log({ target, token, lifetime, instance: createInstance(target) });
34+ // Container.register(
35+ // token,
36+ // () => createInstance(target),
37+ // lifetime
38+ // );
3639 } ;
3740}
3841
3942
40- function createInstance < T > ( constructor : Constructor < T > ) : T {
41- // Use original constructor if available
42- const ctor = constructor . prototype ?. __originalConstructor || constructor ;
43- const metadata : any [ ] = injectionMetadata . get ( ctor ) ?? [ ] ;
43+ // function createInstance<T>(constructor: Constructor<T>): T {
44+ // // Get metadata from the constructor directly
45+ // const metadata: any[] = injectionMetadata.get(constructor) ?? [];
4446
45- const args = metadata . map ( ( token ) => {
46- try {
47- return Container . resolve ( token ) ;
48- } catch ( error ) {
49- if ( error instanceof Error ) {
50- throw new Error ( `Error resolving ${ token } : ${ error . message } ` ) ;
51- }
52- throw error ;
53- }
54- } ) ;
47+ // console.log({ metadata });
48+
49+ // const args = metadata.map(token => {
50+ // try {
51+ // return Container.resolve(token);
52+ // } catch (error) {
53+ // if (error instanceof Error) {
54+ // throw new Error(`Dependency resolution failed: ${error.message}`);
55+ // }
56+ // throw error;
57+ // }
58+ // });
5559
56- return new ctor ( ...args ) ;
57- }
60+ // return new constructor (...args);
61+ // }
0 commit comments