| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import 'dart:async';
- import 'package:meta/meta.dart';
- import 'package:get/get.dart';
- class Closeable{
- @protected
- Future<void> onClose()async{}
- @protected
- void listenStream<T>(Stream<T> stream, void Function(T event) onData){
- _subscriptions.add(stream.listen(onData));
- }
- @protected
- void listenRx<T>(Rx<T> rx, void Function(T event) onData){
- _subscriptions.add(rx.listen(onData));
- }
- @protected
- void addTimer(Duration interval, void Function() callback){
- _timers.add(Timer.periodic(interval, (timer) {
- callback();
- }));
- }
- @protected
- void rxBindRx<T>({required Rx<T> dst, required Rx<T> src}){
- dst.bindStream(src.stream);
- _rxList.add(dst);
- }
- void close(){
- _isActive=false;
- for(final one in _subscriptions){
- one.cancel();
- }
- for(final one in _timers){
- one.cancel();
- }
- for(final one in _rxList){
- one.close();
- }
- onClose().then((value) => _closeController.close());
- }
- Future<void> join(){
- _joinFuture??=()async{
- try{
- await for (final _ in _closeController.stream) {}
- }finally{}
- }();
- return _joinFuture!;
- }
- final _closeController = StreamController();
- Future<void>? _joinFuture;
- var _isActive=true;
- bool get isActive=>_isActive;
- final List<Timer> _timers = [];
- final _subscriptions = <StreamSubscription>[];
- final _rxList = <Rx>[];
- }
|