|
| 1 | +/* eslint-disable no-undef-init */ |
1 | 2 | /* eslint-disable @typescript-eslint/no-unused-vars */ |
2 | 3 | /* eslint-disable @typescript-eslint/no-explicit-any */ |
3 | 4 | import { multiOptional, ServiceIdentifier } from 'reactant-di'; |
4 | 5 |
|
5 | | -import { containerKey, dynamicModulesKey, identifierKey } from '../constants'; |
| 6 | +import { |
| 7 | + containerKey, |
| 8 | + dynamicModulesKey, |
| 9 | + identifierKey, |
| 10 | + modulesKey, |
| 11 | +} from '../constants'; |
6 | 12 | import type { DynamicModules, PropertyDescriptor } from '../interfaces'; |
7 | 13 |
|
8 | | -type Dynamic = <M extends boolean = false>( |
9 | | - serviceIdentifier: ServiceIdentifier<unknown>, |
| 14 | +type Dynamic = <M extends boolean = false, T extends boolean = false>( |
| 15 | + serviceIdentifierOrName: ServiceIdentifier<unknown>, |
10 | 16 | options?: { |
11 | 17 | /** |
12 | 18 | * Whether to inject multiple instances. |
13 | 19 | */ |
14 | | - multiple?: boolean; |
| 20 | + multiple?: T extends false ? false : M; |
| 21 | + /** |
| 22 | + * use token identifier to get service, use name to get service by default. |
| 23 | + */ |
| 24 | + useToken?: T; |
15 | 25 | } |
16 | 26 | ) => ( |
17 | 27 | target: object, |
18 | 28 | key: string | symbol, |
19 | 29 | descriptor?: PropertyDescriptor<unknown> |
20 | 30 | ) => void; |
21 | 31 |
|
22 | | -export const dynamic: Dynamic = (serviceIdentifier, options) => ( |
23 | | - target, |
24 | | - key |
25 | | -) => { |
26 | | - const multipleInject = options?.multiple ?? false; |
27 | | - if (multipleInject) { |
28 | | - multiOptional(serviceIdentifier)(class {}); |
29 | | - } |
30 | | - function getter(this: any) { |
31 | | - const dynamicModules: DynamicModules = this[dynamicModulesKey]; |
32 | | - if (__DEV__ && !dynamicModules) { |
| 32 | +export const dynamic: Dynamic = |
| 33 | + (serviceIdentifierOrName, options) => (target, key) => { |
| 34 | + const multipleInject = options?.multiple ?? false; |
| 35 | + const useToken = options?.useToken ?? false; |
| 36 | + if (multipleInject) { |
| 37 | + multiOptional(serviceIdentifierOrName)(class {}); |
| 38 | + } |
| 39 | + function getter(this: any) { |
| 40 | + if (!useToken) { |
| 41 | + return this[modulesKey]?.[serviceIdentifierOrName as string]; |
| 42 | + } |
| 43 | + const dynamicModules: DynamicModules = this[dynamicModulesKey]; |
| 44 | + if (__DEV__ && !dynamicModules) { |
| 45 | + throw new Error( |
| 46 | + `The property '${key.toString()}' is not readable when class ${ |
| 47 | + this[identifierKey] ?? this.constructor.name |
| 48 | + } is constructing.` |
| 49 | + ); |
| 50 | + } |
| 51 | + if (dynamicModules.has(serviceIdentifierOrName)) { |
| 52 | + return dynamicModules.get(serviceIdentifierOrName)!.value; |
| 53 | + } |
| 54 | + let value: unknown = undefined; |
| 55 | + try { |
| 56 | + const services = this[containerKey]!.getAll(serviceIdentifierOrName); |
| 57 | + value = !multipleInject ? services[0] : services; |
| 58 | + } catch (e) { |
| 59 | + // |
| 60 | + } |
| 61 | + dynamicModules.set(serviceIdentifierOrName, { |
| 62 | + multiple: multipleInject, |
| 63 | + value, |
| 64 | + }); |
| 65 | + return value; |
| 66 | + } |
| 67 | + |
| 68 | + function setter(this: any, _: unknown) { |
33 | 69 | throw new Error( |
34 | | - `The property '${key.toString()}' is not readable when class ${ |
| 70 | + `Cannot assign to read only 'dynamic' injection property '${key.toString()}' of class '${ |
35 | 71 | this[identifierKey] ?? this.constructor.name |
36 | | - } is constructing.` |
| 72 | + }'` |
37 | 73 | ); |
38 | 74 | } |
39 | | - if (dynamicModules.has(serviceIdentifier)) { |
40 | | - return dynamicModules.get(serviceIdentifier)!.value; |
41 | | - } |
42 | | - let value: unknown = null; |
43 | | - try { |
44 | | - const services = this[containerKey]!.getAll(serviceIdentifier); |
45 | | - value = !multipleInject ? services[0] : services; |
46 | | - } catch (e) { |
47 | | - // |
48 | | - } |
49 | | - dynamicModules.set(serviceIdentifier, { multiple: multipleInject, value }); |
50 | | - return value; |
51 | | - } |
52 | | - |
53 | | - function setter(this: any, _: unknown) { |
54 | | - throw new Error( |
55 | | - `Cannot assign to read only 'dynamic' injection property '${key.toString()}' of class '${ |
56 | | - this[identifierKey] ?? this.constructor.name |
57 | | - }'` |
58 | | - ); |
59 | | - } |
60 | 75 |
|
61 | | - // It should be compatible with the TS decorator and the babel decorator |
62 | | - return { |
63 | | - configurable: true, |
64 | | - enumerable: true, |
65 | | - get: getter, |
66 | | - set: setter, |
67 | | - } as any; |
68 | | -}; |
| 76 | + // It should be compatible with the TS decorator and the babel decorator |
| 77 | + return { |
| 78 | + configurable: true, |
| 79 | + enumerable: true, |
| 80 | + get: getter, |
| 81 | + set: setter, |
| 82 | + } as any; |
| 83 | + }; |
0 commit comments