getScopedValueState<TResult, TScopedValue extends ScopedValue<TResult>> method

ScopedValueState<TResult, ScopedValue<TResult>> getScopedValueState<TResult, TScopedValue extends ScopedValue<TResult>>(
  1. TScopedValue provider(), {
  2. void onInit(
    1. ScopedValueState<TResult, TScopedValue> state
    )?,
  3. void onUpdate(
    1. ScopedValueState<TResult, TScopedValue> state
    )?,
  4. Object? name,
  5. ScopedLoggerScope? scope,
  6. String? managedBy,
  7. List<LoggerAdapter> loggerAdapters = const [],
})

By passing a callback that returns TScopedValue to provider, it creates a state for that ScopedValue and performs the appropriate processing, returning ScopedValueState.

Keys are stored in TScopedValue/name and the state is maintained for each key.

If the key is searched and the already created state does not exist, a new state is created and ScopedValueState.initValue is executed to save the state.

If the state has already been created, ScopedValueState.didUpdateValue is executed and the value is returned.

onInit and onUpdate are executed just before ScopedValueState.initValue and ScopedValueState.didUpdateValue are executed.

providerTScopedValueを返すコールバックを渡すことでそのScopedValueのステートを作成しつつ適切な処理を行いScopedValueStateを返します。

TScopedValue/nameでキーが保存されており、そのキーごとに状態が保持されます。

キーを検索しすでに作成済みの状態が存在しない場合は新しく状態を作成しScopedValueState.initValueを実行して状態を保存します。

すでに作成済みの状態が存在する場合はScopedValueState.didUpdateValueを実行して値を返します。

ScopedValueState.initValueScopedValueState.didUpdateValueを実行する直前にonInitonUpdateが実行されます。

Implementation

ScopedValueState<TResult, ScopedValue<TResult>>
    getScopedValueState<TResult, TScopedValue extends ScopedValue<TResult>>(
  TScopedValue Function() provider, {
  void Function(ScopedValueState<TResult, TScopedValue> state)? onInit,
  void Function(ScopedValueState<TResult, TScopedValue> state)? onUpdate,
  Object? name,
  ScopedLoggerScope? scope,
  String? managedBy,
  List<LoggerAdapter> loggerAdapters = const [],
}) {
  final key = "$TScopedValue/${name.hashCode}";
  final found = _data[key];
  if (found != null) {
    assert(
      found is ScopedValueState<TResult, TScopedValue>,
      "The stored [value] type is incorrect: ${found.runtimeType}",
    );
    final state = found as ScopedValueState<TResult, TScopedValue>;
    final oldValue = state.value;
    state._setValue(provider.call());
    onUpdate?.call(state);
    found.didUpdateValue(oldValue);
    notifyListeners();
    return state;
  } else {
    final value = provider.call();
    final state =
        value.createState() as ScopedValueState<TResult, TScopedValue>;
    state._setValue(value);
    state._setLoggers(
      loggerAdapters: loggerAdapters,
      baseParameters: {
        if (scope != null) ScopedLoggerEvent.scopeKey: scope.name,
        ScopedLoggerEvent.typeKey: TScopedValue.toString(),
        ScopedLoggerEvent.nameKey: name,
        if (managedBy != null) ScopedLoggerEvent.managedKey: managedBy,
      },
    );
    onInit?.call(state);
    state.initValue();
    _data[key] = state;
    notifyListeners();
    return state;
  }
}