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

create a new instance even if dispose is not completed after resetLaz… #284

Conversation

dzziwny
Copy link

@dzziwny dzziwny commented Jun 30, 2022

In app i need to resetLazySingleton in dispose (sync) method in statefull widget. My service dispose method is async and takes long time. Once i am back in widget, Get.I<Foo>() returns me same instance, with on going dispose. For now, the only way to avoid that is to register dispose function as non-Future, like:

GetIt.I.registerLazySingleton<Foo>(
    () => Foo(),
    dispose: (foo) {
      foo.dispose();
    },
  );

but here, await resetLazySingleton<Foo>() will not await my dispose function and nowhere i am able to wait for it. Also from convention, i don't see a point, where i would need to get again (almost) disposed instance - so i think its buggy.

I think:

  1. resetLazySingleton<Foo>(); return Get.I<Foo>(); should return a new instance;
  2. await resetLazySingleton<Foo>() should await for dispose function with Future type.

@escamoteur
Copy link
Collaborator

Hi, interesting find and I agree with your view. I will merge after my vacation.
thanks a lot for contributing

@dzziwny
Copy link
Author

dzziwny commented Aug 24, 2022

Hi, interesting find and I agree with your view. I will merge after my vacation. thanks a lot for contributing

Reminder ;-)

@dzziwny
Copy link
Author

dzziwny commented Nov 2, 2022

@escamoteur can i help somehow?

@dzziwny dzziwny force-pushed the bugfix/no-need-to-wait-for-dispose-after-resetLazySingleton branch from 02a49e3 to a95a35f Compare February 15, 2023 19:36
@dzziwny
Copy link
Author

dzziwny commented Feb 15, 2023

Any update?

@escamoteur escamoteur merged commit 79504a2 into fluttercommunity:master May 5, 2023
@escamoteur
Copy link
Collaborator

I know that it's been a long time, but I had to make a longer break due to health issues.

I streamlined your solution a bit. I think:

    dynamic disposeReturn;
    if (instanceFactory.instance != null) {
      if (disposingFunction != null) {
        disposeReturn = disposingFunction.call(instanceFactory.instance! as T);
      } else {
        disposeReturn = instanceFactory.dispose();
      }
    }

    instanceFactory.instance = null;
    instanceFactory.pendingResult = null;
    instanceFactory._readyCompleter = Completer<T>();
    if (disposeReturn is Future) {
      await disposeReturn;
    }
  }

should do the same

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants