dart_frog icon indicating copy to clipboard operation
dart_frog copied to clipboard

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

Open ekokurniadi opened this issue 3 years ago • 6 comments

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 avatar Sep 22 '22 19:09 ekokurniadi

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 avatar Sep 22 '22 21:09 felangel

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

ekokurniadi avatar Sep 23 '22 01:09 ekokurniadi

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

ekokurniadi avatar Sep 23 '22 16:09 ekokurniadi

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

Did you manage to resolve the issue?

felangel avatar Sep 23 '22 16:09 felangel

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>();
    }

GabrielRozendo avatar Sep 24 '22 08:09 GabrielRozendo

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

felangel avatar Sep 24 '22 18:09 felangel

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.

SuperMuel avatar Oct 24 '22 17:10 SuperMuel

related to https://github.com/fluttercommunity/get_it/issues/166

felangel avatar Nov 08 '22 21:11 felangel

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.

marcotrumpet avatar Nov 09 '22 09:11 marcotrumpet

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 avatar Nov 21 '22 23:11 felangel