-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(core): added unique dynamic module factory #12898
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { DynamicModule, Type } from '@nestjs/common'; | ||
import { RuntimeException } from '@nestjs/core/errors/exceptions'; | ||
|
||
const kUniqueModuleId = Symbol('kUniqueModuleId'); | ||
|
||
export class UniqueDynamicModuleFactory { | ||
protected static readonly uniqueMap = new Map<string, string>(); | ||
|
||
/** | ||
* You can use this method to avoid the hash algorithm during the DI compilation. | ||
* | ||
* Imagine you have a module that exports a provider, and that provider is | ||
* imported using `MyModule.forRoot('myToken')`, and you can get the reference of this provider using | ||
* injected using @Inject('myToken'). If you import this module twice, with different arguments on `.forRoot`, | ||
* in order to register both modules, we need to serialize and hash the dynamic module created by `.forRoot`. | ||
* Otherwise, it overrides the providers defined in the first import with the providers declared on the second import. | ||
* | ||
* This function tells the algorithm to skip the hash and directly use the `staticUniqueId`, and | ||
*you are responsible for making it unique. | ||
* | ||
* @param staticUniqueId The unique ID across all modules. | ||
* @param dynamicModule The dynamic module. | ||
* | ||
* @throws RuntimeException If the ID is already registered, an error will occur. | ||
*/ | ||
static wrap(staticUniqueId: string, dynamicModule: DynamicModule) { | ||
if (!this.uniqueMap.has(staticUniqueId)) { | ||
dynamicModule[ | ||
kUniqueModuleId | ||
] = `${dynamicModule.module.name}_${staticUniqueId}`; | ||
this.uniqueMap.set(staticUniqueId, dynamicModule.module.name); | ||
return dynamicModule; | ||
} | ||
|
||
throw new RuntimeException( | ||
`A module with this ID was already added before for "${ | ||
this.uniqueMap.get(staticUniqueId).split('_')[0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}".`, | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Check if the given dynamic module was marked as unique. | ||
* | ||
* @param dynamicModule The dynamic module | ||
*/ | ||
export function isUniqueDynamicModule( | ||
dynamicModule: DynamicModule | Type<any> | undefined, | ||
): boolean { | ||
return dynamicModule && dynamicModule[kUniqueModuleId] !== undefined; | ||
} | ||
|
||
/** | ||
* Get the stored unique dynamic module id. | ||
* | ||
* @param dynamicModule The dynamic module | ||
*/ | ||
export function getUniqueDynamicModuleId( | ||
dynamicModule: DynamicModule | Type<any>, | ||
): string | undefined { | ||
return dynamicModule[kUniqueModuleId]; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit concerned on this being
static
what if we want to create multiple apps (let's say, one
NestFactory.createApplicationContext
and other withNestFactory.create
)I can't think of any other approach tho
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just chiming in here - I don't think anyone should be doing that 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, me too :p So we can treat that as a limitation of
UniqueDynamicModuleFactory
if someone try to do so