|
1 | 1 | import { DynamicModule } from '@nestjs/common'; |
2 | 2 | import { SHARED_MODULE_METADATA } from '@nestjs/common/constants'; |
3 | 3 | import { Type } from '@nestjs/common/interfaces/type.interface'; |
| 4 | +import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util'; |
4 | 5 | import stringify from 'fast-safe-stringify'; |
5 | 6 | import * as hash from 'object-hash'; |
6 | 7 |
|
7 | 8 | export class ModuleTokenFactory { |
| 9 | + private readonly moduleIdsCache = new WeakMap<Type<unknown>, string>(); |
| 10 | + |
8 | 11 | public create( |
9 | | - metatype: Type<any>, |
10 | | - scope: Type<any>[], |
| 12 | + metatype: Type<unknown>, |
| 13 | + scope: Type<unknown>[], |
11 | 14 | dynamicModuleMetadata?: Partial<DynamicModule> | undefined, |
12 | 15 | ): string { |
| 16 | + const moduleId = this.getModuleId(metatype); |
13 | 17 | const moduleScope = this.reflectScope(metatype); |
14 | 18 | const isSingleScoped = moduleScope === true; |
15 | 19 | const opaqueToken = { |
| 20 | + id: moduleId, |
16 | 21 | module: this.getModuleName(metatype), |
17 | 22 | dynamic: this.getDynamicMetadataToken(dynamicModuleMetadata), |
18 | 23 | scope: isSingleScoped ? this.getScopeStack(scope) : moduleScope, |
@@ -45,6 +50,16 @@ export class ModuleTokenFactory { |
45 | 50 | return stack.map(module => module.name); |
46 | 51 | } |
47 | 52 |
|
| 53 | + public getModuleId(metatype: Type<unknown>): string { |
| 54 | + let moduleId = this.moduleIdsCache.get(metatype); |
| 55 | + if (moduleId) { |
| 56 | + return moduleId; |
| 57 | + } |
| 58 | + moduleId = randomStringGenerator(); |
| 59 | + this.moduleIdsCache.set(metatype, moduleId); |
| 60 | + return moduleId; |
| 61 | + } |
| 62 | + |
48 | 63 | public getModuleName(metatype: Type<any>): string { |
49 | 64 | return metatype.name; |
50 | 65 | } |
|
0 commit comments