findAsyncIn method

Future<T> findAsyncIn(
  1. LevitScope scope, {
  2. String? tag,
})

Asynchronously finds or creates the store instance within scope.

Uses optional tag to isolate independent instances of the same store definition.

Throws if builder throws while creating the store value.

Implementation

Future<T> findAsyncIn(LevitScope scope, {String? tag}) async {
  final instanceKey =
      tag != null ? 'ls_store_${_getStoreTag(this, tag)}' : _defaultKey;

  var instance =
      await scope.findOrNullAsync<_LevitStoreInstance<T>>(tag: instanceKey);

  if (instance == null) {
    if (!scope.isRegisteredLocally<_LevitStoreInstance<T>>(
      tag: instanceKey,
    )) {
      scope.lazyPut(() => _LevitStoreInstance<T>(this), tag: instanceKey);
    }
    instance =
        await scope.findAsync<_LevitStoreInstance<T>>(tag: instanceKey);
  }

  return instance.value;
}