Skip to content

Commit

Permalink
fix(signal): Add missing getters to Signal of list type.
Browse files Browse the repository at this point in the history
  • Loading branch information
CarLeonDev committed Oct 27, 2023
1 parent 7ee4754 commit 6bbf54f
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions packages/reactter/lib/src/signals/signal_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ extension SignalListExt<E> on Signal<List<E>> {
void operator []=(int index, E valueToSet) =>
update((_) => value[index] = valueToSet);

/// Returns the first element.
///
/// Throws a [StateError] if `this` is empty.
/// Otherwise returns the first element in the iteration order,
/// equivalent to `this.elementAt(0)`.
E get first => value.first;

/// The first element of the list.
///
/// The list must be non-empty when accessing its first element.
Expand All @@ -35,6 +42,16 @@ extension SignalListExt<E> on Signal<List<E>> {
/// ```
set first(E valueToSet) => update((_) => value.first = valueToSet);

/// Returns the last element.
///
/// Throws a [StateError] if `this` is empty.
/// Otherwise may iterate through the elements and returns the last one
/// seen.
/// Some iterables may have more efficient ways to find the last element
/// (for example a list can directly access the last element,
/// without iterating through the previous ones).
E get last => value.last;

/// The last element of the list.
///
/// The list must be non-empty when accessing its last element.
Expand All @@ -53,6 +70,15 @@ extension SignalListExt<E> on Signal<List<E>> {
/// ```
set last(E valueToSet) => update((_) => value.last = valueToSet);

/// The number of objects in this list.
///
/// The valid indices for a list are `0` through `length - 1`.
/// ```dart
/// final numbers = <int>[1, 2, 3];
/// print(numbers.length); // 3
/// ```
int get length => value.length;

/// Setting the `length` changes the number of elements in the list.
///
/// The list must be growable.
Expand Down Expand Up @@ -399,27 +425,6 @@ extension SignalListNullExt<E> on Signal<List<E>?> {
update((_) => value?[index] = valueToSet);
}

/// The first element of the list.
///
/// The list must be non-empty when accessing its first element.
///
/// The first element of a list can be modified, unlike an [Iterable].
/// A `list.first` is equivalent to `list[0]`,
/// both for getting and setting the value.
///
/// ```dart
/// final numbers = <int>[1, 2, 3];
/// print(numbers.first); // 1
/// numbers.first = 10;
/// print(numbers.first); // 10
/// numbers.clear();
/// numbers.first; // Throws.
/// ```
set first(E valueToSet) {
if (value == null) return;
update((_) => value?.first = valueToSet);
}

/// The last element of the list.
///
/// The list must be non-empty when accessing its last element.
Expand Down

0 comments on commit 6bbf54f

Please sign in to comment.