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

fix: hot reload makes instance registered twice when using get_it package #335

Closed
ekokurniadi opened this issue Sep 22, 2022 · 10 comments
Closed
Assignees
Labels
won't fix This will not be worked on

Comments

@ekokurniadi
Copy link

ekokurniadi commented Sep 22, 2022

Description

I have some problem when i registered object or instance using get_it, when server is hot reloaded get_it re-registered instance make server stop running

Steps To Reproduce

  1. Create custom entrypoint
  2. Register Instance on Get It
  3. Run server
  4. Do hot reload

Expected Behavior
Instance registered only once

A clear and concise description of what you expected to happen.

Screenshots
gambar

My Get It
gambar

My Custom Entry Point
gambar

If applicable, add screenshots to help explain your problem.

Additional Context

Add any other context about the problem here.

@ekokurniadi ekokurniadi added the bug Something isn't working as expected label Sep 22, 2022
@ekokurniadi ekokurniadi changed the title fix: hot reload is re-registered instance when using get_it package fix: hot reload re-registered instance when using get_it package Sep 22, 2022
@ekokurniadi ekokurniadi changed the title fix: hot reload re-registered instance when using get_it package fix: hot reload make instance registered twice when using get_it package Sep 22, 2022
@ekokurniadi ekokurniadi changed the title fix: hot reload make instance registered twice when using get_it package fix: hot reload makes instance registered twice when using get_it package Sep 22, 2022
@felangel
Copy link
Contributor

Hi @ekokurniadi 👋
Thanks for opening an issue!

I’ll take a closer look at this shortly but out of curiosity was there any specific reason why you decided to include get_it instead of using the built in provider API for dependency injection?

Thanks! 🙏

@felangel felangel self-assigned this Sep 22, 2022
@felangel felangel added the needs triage Issue requires triage label Sep 22, 2022
@ekokurniadi
Copy link
Author

Hi @felangel
can I use custom dependency injection like injectable or get_it ?

@ekokurniadi
Copy link
Author

Hi @felangel
i will close this issue, Thanks for your response

@felangel
Copy link
Contributor

Hi @felangel
i will close this issue, Thanks for your response

Did you manage to resolve the issue?

@GabrielRozendo
Copy link
Contributor

GabrielRozendo commented Sep 24, 2022

I had same situation.
Actually I'm using the built in DI for routes but I also have some job runners and those don't have a "api call", just a cron to run after start.

The "problem" is about hot reload, after changes, reload everything, even the "init()".
A solution would be dart frog offers a place to "init" server and it should not be called on hot reload, only once on start.

For now, I figured out 2 workarounds options to handle this for me.

  1. just a flag to check it
  Future<void> init() async {
    if (_didSetup) return;
    _didSetup = true;
...
  1. use the get it built in verifier:
    if (!GetIt.I.isRegistered<MySingleton>()) {
      GetIt.I.registerLazySingleton<MySingleton>();
    }

@felangel
Copy link
Contributor

I'm going to re-open this since it sounds like something that should be supported by Dart Frog's hot reload.

@felangel felangel reopened this Sep 24, 2022
@SuperMuel
Copy link

I've encountered this problem recently, and as @GabrielRozendo stated, it only occurs after hot reloads.
As I don't mind about my singletons being instantiated again, I just reset them all after each hot reload :

Future<HttpServer> run(Handler handler, InternetAddress ip, int port) async {
    await GetIt.I.reset();

    configureDependencies();

    return serve(handler, ip, port);
}

I'm using Injectable instead of the built in provider API because it simplifies working in multiple environments, and also because of periodic jobs runners that don't have access to a context to read from.

@felangel
Copy link
Contributor

felangel commented Nov 8, 2022

related to fluttercommunity/get_it#166

@marcotrumpet
Copy link

I had same situation. Actually I'm using the built in DI for routes but I also have some job runners and those don't have a "api call", just a cron to run after start.

The "problem" is about hot reload, after changes, reload everything, even the "init()". A solution would be dart frog offers a place to "init" server and it should not be called on hot reload, only once on start.

For now, I figured out 2 workarounds options to handle this for me.

  1. just a flag to check it
  Future<void> init() async {
    if (_didSetup) return;
    _didSetup = true;
...
  1. use the get it built in verifier:
    if (!GetIt.I.isRegistered<MySingleton>()) {
      GetIt.I.registerLazySingleton<MySingleton>();
    }

I used the same get it verifier as you and it did the trick.

@felangel
Copy link
Contributor

Closing for now since this does not appear to be directly related to Dart Frog. As folks have mentioned, you can check if an object has been registered already to prevent re-registering.

@felangel felangel added won't fix This will not be worked on and removed bug Something isn't working as expected needs triage Issue requires triage labels Nov 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
won't fix This will not be worked on
Projects
None yet
Development

No branches or pull requests

5 participants