lazyPutAsync<S> method

Future<S> Function() lazyPutAsync<S>(
  1. Future<S> builder(), {
  2. String? tag,
  3. bool permanent = false,
  4. bool isFactory = false,
})

Registers an asynchronous dependency to be lazily instantiated.

The builder is only executed when the dependency is first requested via findAsync.

If isFactory is true, the builder is called for every request. Otherwise, the result is cached.

Use permanent to survive a non-forced reset only. dispose always resets with force: true. Prefer permanent on root / long-lived scopes.

Returns a function that allows retrieving the dependency (shortcut for findAsync).

Implementation

Future<S> Function() lazyPutAsync<S>(
  Future<S> Function() builder, {
  String? tag,
  bool permanent = false,
  bool isFactory = false,
}) {
  _ensureUsable();
  final key = _getKey<S>(tag);
  final keyString = key.debugString;

  if (_aliases.containsKey(key)) {
    throw StateError(
      'LevitScope($name): "$keyString" is already registered as an alias.',
    );
  }
  if (!isFactory &&
      _registry.containsKey(key) &&
      _registry[key]!.isInstantiated) {
    return () => findAsync<S>(tag: tag);
  }

  final info = LevitDependency<S>(
    asyncBuilder: builder,
    permanent: permanent || isFactory,
    isLazy: true,
    isFactory: isFactory,
  );

  _registerBinding(
    key,
    keyString,
    info,
    isFactory ? 'putFactoryAsync' : 'lazyPutAsync',
    tag: tag,
  );

  return () => findAsync<S>(tag: tag);
}