Line data Source code
1 : part of 'loading_bloc.dart'; 2 : 3 : extension _IsLoadingHelpers on Stream<_TagCountTuple> { 4 2 : Stream<LoadingWithTag> mapToLoadingWithTag() => map( 5 2 : (tuple) => LoadingWithTag( 6 2 : loading: tuple.count >= 1, 7 1 : tag: tuple.tag, 8 : ), 9 : ); 10 : } 11 : 12 : extension _LoadingCountsBinders on Stream<Result<dynamic>> { 13 1 : StreamSubscription bindToLoadingCounts( 14 : BehaviorSubject<Map<String, BehaviorSubject<_TagCountTuple>>> loadingCounts, 15 : ) => 16 2 : listen((result) { 17 3 : final tagPerSubject = loadingCounts.value[result.tag]; 18 : 19 : if (tagPerSubject != null) { 20 : /// Emit an event to the existing subject 21 2 : tagPerSubject.value = tagPerSubject.value 22 2 : .copyWith(incrementCount: result is ResultLoading); 23 : return; 24 : } 25 : 26 1 : final map = loadingCounts.value; 27 : 28 : /// Mark all tuples as not initial, as this will allow to skip 29 : /// the seed value when at [_LoadingCountsMappers.mapToLoadingWithTag] 30 1 : map.entries 31 4 : .map((entry) => entry.value.value) 32 3 : .forEach((tuple) => tuple.initial = false); 33 : 34 : /// Add new subject 35 4 : map[result.tag] = BehaviorSubject.seeded(_TagCountTuple( 36 1 : tag: result.tag, 37 : count: 1, 38 : )); 39 : 40 1 : loadingCounts.value = map; 41 : }); 42 : } 43 : 44 : extension _LoadingCountsMappers 45 : on Stream<Map<String, BehaviorSubject<_TagCountTuple>>> { 46 2 : Stream<LoadingWithTag> mapToLoadingWithTag() => flatMap( 47 2 : (value) => Rx.merge( 48 2 : value.entries.map( 49 1 : (entry) { 50 3 : final stream = entry.value.mapToLoadingWithTag().distinct(); 51 : 52 : /// Skip the seed value if the tuple is not initials 53 4 : return entry.value.value.initial == false 54 1 : ? stream.skip(1) 55 : : stream; 56 : }, 57 : ), 58 : ), 59 1 : ).distinct(); 60 : }