-
-
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
Proposal: Hierarchical module resolution #938
Comments
Relates to #550 |
solved by #977 |
feature(@nestjs/core) add hierarchical lifecycle hooks #938
@kamilmysliwiec you are the man! Thank you! |
Thanks! Gonna leave it open until 5.2.0 is published :) |
5.2.0 is here! |
As I experiment lifecycle event are not called in special order (before all non global modules for OnModuleInit, after all non global modules for OnModuleDestroy). Thanks |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I'm submitting a...
Current behavior
Currently all lifecycle hooks run asynchronously, regardless of their importing order. If any module depends on an async provider it has to import a module this provider belongs to and then import the provider itself to wait for it being resolved.
Expected behavior
It feels natural for any module to wait for its dependencies' initialization before its own initialization. I propose to build a hierarchy of modules and resolve the resulting tree starting from its children. It should work in reverse with module destruction. First top level modules are destroyed, then children.
I want Nest to guarantee that when A is created B.onModuleInit runs before A.onModuleInit and when it's destroyed that A.onModuleDestroy runs before B.onMOduleDestroy
What is the motivation / use case for changing the behavior?
Imagine your application having ThirdPartyLib module. It calls function thirdPartyLibInit using FFI to initialize a C-callable library on creation. Imagine your application having controller MyController depending on ThirdPartyLib. It can not function properly without that C-callable library initialized. Now you have to create an async factory that makes that FFI call and inject it everywhere. It's not very clean. I'd like to be able to import ThirdPartyLib into MyController and leave it up to Nest to figure it out.
The text was updated successfully, but these errors were encountered: