Skip to content
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

Rename to postConstruct all methods decorated with the @postConstruct() decorator #10700

Closed
paul-marechal opened this issue Feb 2, 2022 · 3 comments
Labels
quality issues related to code and application quality

Comments

@paul-marechal
Copy link
Member

  • Rename to postConstruct all methods decorated with the @postConstruct() decorator.
  • Type all those methods as () => void.

There are a lot of inconsistencies when it comes to this post-constructor callback method. most of the time the method is called init, but sometimes configure, or initialize. I would prefer to call it postConstruct as it better describes what the method is meant for, but since init is most commonly used I would be OK with renaming to this instead.

Inversify's postConstruct exist because when it tries to create the instances for our bindings, it has to call the constructor before it can inject properties (if any). See the following example:

@injectable()
class Component {

  @inject(SomeService)
  protected _service: SomeService;

  constructor() {
    // this._service is undefined here!
  }

  @postConstruct()
  protected postConstruct(): void {
    // this._service got injected, we can use it.
  }
}

A problem in our code base is that some of those postConstruct functions return promises. Inversify won't wait on them, as the instance creation step assumes everything happens synchronously. This means that more often than not, by the time our asynchronously-post-constructed services are injected, dependents might try to call into uninitialized properties...

@paul-marechal paul-marechal added the quality issues related to code and application quality label Feb 2, 2022
@tsmaeder
Copy link
Contributor

@paul-marechal the problem with returning promises seems much more severe than the naming. Did you open an issue?

@paul-marechal
Copy link
Member Author

paul-marechal commented Feb 23, 2022

Naming is just for consistency/clarity but the asynchronous issue is an actual problem indeed. I'll open an issue just for it.

#10788

@msujew
Copy link
Member

msujew commented Jun 4, 2024

They are now all called init, see #12425.

@msujew msujew closed this as completed Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
quality issues related to code and application quality
Projects
None yet
Development

No branches or pull requests

3 participants