getScopedValueState<TResult, TScopedValue extends ScopedValue<TResult> > method
- TScopedValue provider(), {
- void onInit(
- ScopedValueState<
TResult, TScopedValue> state
- ScopedValueState<
- void onUpdate(
- ScopedValueState<
TResult, TScopedValue> state
- ScopedValueState<
- Object? name,
- ScopedLoggerScope? scope,
- String? managedBy,
- 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.
provider
にTScopedValue
を返すコールバックを渡すことでそのScopedValueのステートを作成しつつ適切な処理を行いScopedValueStateを返します。
TScopedValue/name
でキーが保存されており、そのキーごとに状態が保持されます。
キーを検索しすでに作成済みの状態が存在しない場合は新しく状態を作成しScopedValueState.initValueを実行して状態を保存します。
すでに作成済みの状態が存在する場合はScopedValueState.didUpdateValueを実行して値を返します。
ScopedValueState.initValueやScopedValueState.didUpdateValueを実行する直前にonInit
やonUpdate
が実行されます。
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;
}
}