mirror of
https://gitlab.com/comunic/comunicmobile
synced 2025-06-19 08:15:16 +00:00
Listen to WebSocket close events
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
import 'package:comunic/helpers/events_helper.dart';
|
||||
import 'package:comunic/helpers/websocket_helper.dart';
|
||||
import 'package:comunic/ui/routes/main_route.dart';
|
||||
import 'package:comunic/ui/widgets/safe_state.dart';
|
||||
import 'package:comunic/utils/intl_utils.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
@ -17,7 +19,7 @@ class InitializeWidget extends StatefulWidget {
|
||||
_InitializeWidgetState createState() => _InitializeWidgetState();
|
||||
}
|
||||
|
||||
class _InitializeWidgetState extends State<InitializeWidget> {
|
||||
class _InitializeWidgetState extends SafeState<InitializeWidget> {
|
||||
bool _error = false;
|
||||
|
||||
@override
|
||||
@ -25,6 +27,9 @@ class _InitializeWidgetState extends State<InitializeWidget> {
|
||||
_tryConnect();
|
||||
|
||||
super.initState();
|
||||
|
||||
// Listen to WebSocket close event
|
||||
super.listen<WSClosedEvent>((e) => _tryConnect());
|
||||
}
|
||||
|
||||
/// Try to connect to server
|
||||
|
@ -1,3 +1,6 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:comunic/helpers/events_helper.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// Little State hack to avoid issues
|
||||
@ -5,9 +8,26 @@ import 'package:flutter/material.dart';
|
||||
/// @author Pierre HUBERT
|
||||
|
||||
abstract class SafeState<T extends StatefulWidget> extends State<T> {
|
||||
|
||||
final _subscriptions = List<StreamSubscription>();
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
// Close subscriptions
|
||||
_subscriptions.forEach((f) => f.cancel());
|
||||
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
void setState(fn) {
|
||||
if(mounted)
|
||||
super.setState(fn);
|
||||
}
|
||||
|
||||
/// Register to a new subscription
|
||||
@protected
|
||||
void listen<T>(void onEvent(T event)) {
|
||||
_subscriptions.add(EventsHelper.on<T>(onEvent));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user