2024-01-04 22:29:43 -05:00
|
|
|
import 'dart:async';
|
|
|
|
|
2024-02-11 00:29:58 -05:00
|
|
|
import 'package:async_tools/async_tools.dart';
|
2024-01-04 22:29:43 -05:00
|
|
|
import 'package:bloc/bloc.dart';
|
|
|
|
|
|
|
|
abstract class StreamWrapperCubit<State> extends Cubit<AsyncValue<State>> {
|
|
|
|
StreamWrapperCubit(Stream<State> stream, {State? defaultState})
|
|
|
|
: super(defaultState != null
|
|
|
|
? AsyncValue.data(defaultState)
|
|
|
|
: const AsyncValue.loading()) {
|
|
|
|
_subscription = stream.listen((event) => emit(AsyncValue.data(event)),
|
|
|
|
// ignore: avoid_types_on_closure_parameters
|
|
|
|
onError: (Object error, StackTrace stackTrace) {
|
|
|
|
emit(AsyncValue.error(error, stackTrace));
|
|
|
|
});
|
2024-02-09 21:17:28 -05:00
|
|
|
}
|
2024-01-04 22:29:43 -05:00
|
|
|
|
2024-02-09 21:17:28 -05:00
|
|
|
@override
|
|
|
|
Future<void> close() async {
|
|
|
|
await _subscription.cancel();
|
|
|
|
await super.close();
|
2024-01-04 22:29:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
late final StreamSubscription<State> _subscription;
|
|
|
|
}
|