Skip to content

Commit

Permalink
Merge pull request #1231 from lichess-org/resync_broadcast_on_resume
Browse files Browse the repository at this point in the history
Resync broadcast state on app resume
  • Loading branch information
veloce authored Dec 7, 2024
2 parents 73c7bff + d8ab907 commit 4129b12
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/src/model/broadcast/broadcast_game_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:async';
import 'package:dartchess/dartchess.dart';
import 'package:deep_pick/deep_pick.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:flutter/widgets.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:lichess_mobile/src/model/analysis/analysis_controller.dart';
import 'package:lichess_mobile/src/model/analysis/analysis_preferences.dart';
Expand Down Expand Up @@ -33,6 +34,7 @@ class BroadcastGameController extends _$BroadcastGameController
static Uri broadcastSocketUri(BroadcastRoundId broadcastRoundId) =>
Uri(path: 'study/$broadcastRoundId/socket/v6');

AppLifecycleListener? _appLifecycleListener;
StreamSubscription<SocketEvent>? _subscription;

late SocketClient _socketClient;
Expand All @@ -55,11 +57,18 @@ class BroadcastGameController extends _$BroadcastGameController

final evaluationService = ref.watch(evaluationServiceProvider);

_appLifecycleListener = AppLifecycleListener(
onResume: () {
ref.invalidateSelf();
},
);

ref.onDispose(() {
_subscription?.cancel();
_startEngineEvalTimer?.cancel();
_engineEvalDebounce.dispose();
evaluationService.disposeEngine();
_appLifecycleListener?.dispose();
});

final pgn = await ref.withClient(
Expand Down
9 changes: 9 additions & 0 deletions lib/src/model/broadcast/broadcast_round_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:async';
import 'package:dartchess/dartchess.dart';
import 'package:deep_pick/deep_pick.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:flutter/widgets.dart';
import 'package:lichess_mobile/src/model/broadcast/broadcast.dart';
import 'package:lichess_mobile/src/model/broadcast/broadcast_repository.dart';
import 'package:lichess_mobile/src/model/common/chess.dart';
Expand All @@ -21,6 +22,7 @@ class BroadcastRoundController extends _$BroadcastRoundController {
Uri(path: 'study/$broadcastRoundId/socket/v6');

StreamSubscription<SocketEvent>? _subscription;
AppLifecycleListener? _appLifecycleListener;

late SocketClient _socketClient;

Expand All @@ -34,8 +36,15 @@ class BroadcastRoundController extends _$BroadcastRoundController {

_subscription = _socketClient.stream.listen(_handleSocketEvent);

_appLifecycleListener = AppLifecycleListener(
onResume: () {
ref.invalidateSelf();
},
);

ref.onDispose(() {
_subscription?.cancel();
_appLifecycleListener?.dispose();
});

final round = await ref.withClient(
Expand Down

0 comments on commit 4129b12

Please sign in to comment.