derivedSetting<T, I> method

Setting<T> derivedSetting<T, I>({
  1. required String key,
  2. required ReadableBeacon<I> input,
  3. required T decode(
    1. RawSettingValue,
    2. I
    ),
  4. required RawSettingValue encode(
    1. T
    ),
})

A Setting that is derived from input. Any time the input changes, the decode function will be invoked with the current value of the returned Setting as well as the new value of input. Similarly, any time the returned Setting changes, the decode function will be invoked with the new value of the returned Setting as well as the current value of input.

This is useful if a Setting depends on another Setting.

Implementation

Setting<T> derivedSetting<T, I>({
  required String key,
  required ReadableBeacon<I> input,
  required T Function(RawSettingValue, I) decode,
  required RawSettingValue Function(T) encode,
}) {
  final s = setting<T>(
    key: key,
    decode: (value) => decode(value, input.value),
    encode: (value) => encode(value),
  );
  _group.effect(
    () {
      final $input = input.value;
      final $s = s.value.value;
      final decoded = decode(s._toRawValue(_storage.get(key)), $input);
      if (decoded != $s) {
        s.value.value = decoded;
      }
    },
    name: 'derivedSettingEffect',
  );
  return s;
}