After correcting the countdown on mobile version1
This commit is contained in:
parent
39c393b50e
commit
1d2aae29b4
@ -13,9 +13,19 @@ import '../utils/server_adjusted_countdown.dart';
|
||||
part 'prediction_event.dart';
|
||||
part 'prediction_state.dart';
|
||||
|
||||
/// `Bloc` processes events **concurrently** by default. `PredictionRefreshTick`
|
||||
/// does an `await`ed HTTP round-trip, so a slow response can resolve *after* a
|
||||
/// later tick already started/finished — an old, in-flight refresh clobbering
|
||||
/// state a newer one already updated. Sequential processing (à la
|
||||
/// `bloc_concurrency`'s `sequential()`, implemented inline to avoid the extra
|
||||
/// dependency) guarantees ticks are fully handled one at a time, in order.
|
||||
EventTransformer<E> _sequential<E>() {
|
||||
return (events, mapper) => events.asyncExpand(mapper);
|
||||
}
|
||||
|
||||
class PredictionBloc extends Bloc<PredictionEvent, PredictionState> {
|
||||
PredictionBloc(this._repo, this._socket, this._walletRepo) : super(const PredictionState.initial()) {
|
||||
on<PredictionLoadRequested>(_onLoad);
|
||||
on<PredictionLoadRequested>(_onLoad, transformer: _sequential());
|
||||
on<PredictionIntervalChanged>(_onIntervalChanged);
|
||||
on<PredictionStakeSelected>(_onStakeSelected);
|
||||
on<PredictionDirectionSelected>(_onDirectionSelected);
|
||||
@ -26,7 +36,7 @@ class PredictionBloc extends Bloc<PredictionEvent, PredictionState> {
|
||||
on<PredictionPlaceBetRequested>(_onPlaceBet);
|
||||
on<PredictionPlaceMultiBetRequested>(_onPlaceMultiBet);
|
||||
on<PredictionAutoSaveRequested>(_onAutoSave);
|
||||
on<PredictionRefreshTick>(_onRefreshTick);
|
||||
on<PredictionRefreshTick>(_onRefreshTick, transformer: _sequential());
|
||||
on<PredictionWsMessageReceived>(_onWsMessage);
|
||||
on<PredictionSocketStatusChanged>(_onSocketStatusChanged);
|
||||
on<PredictionHistoryPageRequested>(_onHistoryPage);
|
||||
|
||||
@ -760,9 +760,14 @@ class _StatusChip extends StatelessWidget {
|
||||
|
||||
/// Smooth local countdown using serverNowMs skew (web parity).
|
||||
///
|
||||
/// Relative `secondsToBetting*` values are frozen at publish time; subtracting
|
||||
/// elapsed client time since [RoundModel.serverNowMs] keeps the UI monotonic
|
||||
/// across WS/poll refreshes.
|
||||
/// Always derives from the LATEST [widget.round] (already de-duplicated and
|
||||
/// made monotonic upstream by [mergeRound] / [roundUiChanged] in the bloc) plus
|
||||
/// a locally ticking clock — exactly like the web `useServerAdjustedCountdown`
|
||||
/// hook. Deliberately does **not** keep a separate frozen "anchor" snapshot:
|
||||
/// an earlier version did, and because that anchor was reset independently
|
||||
/// from the bloc's own change-detection, the two layers could disagree and
|
||||
/// produce the "55 → 0 → 52 → 0 → 49 → 0" jump bug. A single source of truth
|
||||
/// (the round passed down) avoids that entirely.
|
||||
class _LiveCountdown extends StatefulWidget {
|
||||
const _LiveCountdown({
|
||||
super.key,
|
||||
@ -783,30 +788,15 @@ class _LiveCountdownState extends State<_LiveCountdown> {
|
||||
Timer? _timer;
|
||||
int _nowMs = DateTime.now().millisecondsSinceEpoch;
|
||||
|
||||
/// Anchored snapshot so timer-only WS ticks (if any reach the widget) cannot
|
||||
/// jump the displayed seconds. Relocked only on round id / phase changes.
|
||||
RoundModel? _anchor;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_anchor = widget.round;
|
||||
_timer = Timer.periodic(const Duration(milliseconds: 200), (_) {
|
||||
if (!mounted) return;
|
||||
setState(() => _nowMs = DateTime.now().millisecondsSinceEpoch);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant _LiveCountdown oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
final old = oldWidget.round;
|
||||
final next = widget.round;
|
||||
if (old.id != next.id || old.status != next.status || _anchor == null) {
|
||||
_anchor = next;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_timer?.cancel();
|
||||
@ -815,8 +805,7 @@ class _LiveCountdownState extends State<_LiveCountdown> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final round = _anchor ?? widget.round;
|
||||
final adjusted = serverAdjustedCountdown(round, clientNowMs: _nowMs);
|
||||
final adjusted = serverAdjustedCountdown(widget.round, clientNowMs: _nowMs);
|
||||
final isOpen = adjusted.bettingOpen;
|
||||
final seconds =
|
||||
isOpen ? adjusted.secondsToBettingEnd : adjusted.secondsToBettingStart;
|
||||
|
||||
@ -68,22 +68,40 @@ bool roundUiChanged(RoundModel? prev, RoundModel? next) {
|
||||
|
||||
/// Merges an incoming WS/HTTP round onto [prev], preserving absolute deadlines
|
||||
/// when the payload omits them (Redis cache historically stripped them).
|
||||
///
|
||||
/// Guards against **out-of-order delivery**: the safety HTTP poll and WS ticks
|
||||
/// race independently, so a slow HTTP response can resolve *after* a newer WS
|
||||
/// tick already advanced the countdown. If that stale response's `serverNowMs`
|
||||
/// is older than what we already have for the same round, its timing fields
|
||||
/// (status / secondsToBetting* / serverNowMs) are ignored — otherwise the
|
||||
/// countdown regresses and the derived status flaps (betting → locked →
|
||||
/// betting), which is exactly what produced the "55 → 0 → 52 → 0 → 49 → 0" bug.
|
||||
RoundModel mergeRound(RoundModel? prev, RoundModel incoming) {
|
||||
if (prev == null) return _withEffectiveStatus(incoming);
|
||||
|
||||
final sameRound = prev.id == incoming.id;
|
||||
final prevServerNow = prev.serverNowMs;
|
||||
final incomingServerNow = incoming.serverNowMs;
|
||||
final incomingIsFresher = !sameRound ||
|
||||
incomingServerNow == null ||
|
||||
prevServerNow == null ||
|
||||
incomingServerNow >= prevServerNow;
|
||||
|
||||
final merged = RoundModel(
|
||||
id: incoming.id,
|
||||
status: incoming.status,
|
||||
secondsToBettingEnd: incoming.secondsToBettingEnd,
|
||||
secondsToBettingStart: incoming.secondsToBettingStart,
|
||||
secondsToClose: incoming.secondsToClose,
|
||||
status: incomingIsFresher ? incoming.status : prev.status,
|
||||
secondsToBettingEnd:
|
||||
incomingIsFresher ? incoming.secondsToBettingEnd : prev.secondsToBettingEnd,
|
||||
secondsToBettingStart: incomingIsFresher
|
||||
? incoming.secondsToBettingStart
|
||||
: prev.secondsToBettingStart,
|
||||
secondsToClose: incomingIsFresher ? incoming.secondsToClose : prev.secondsToClose,
|
||||
outcome: incoming.outcome ?? (sameRound ? prev.outcome : null),
|
||||
shortPoolPercent: incoming.shortPoolPercent ??
|
||||
(sameRound ? prev.shortPoolPercent : null),
|
||||
longPoolPercent: incoming.longPoolPercent ??
|
||||
(sameRound ? prev.longPoolPercent : null),
|
||||
serverNowMs: incoming.serverNowMs ?? prev.serverNowMs,
|
||||
serverNowMs: incomingIsFresher ? (incomingServerNow ?? prevServerNow) : prevServerNow,
|
||||
candleOpenMs: incoming.candleOpenMs ?? (sameRound ? prev.candleOpenMs : null),
|
||||
bettingEndMs: incoming.bettingEndMs ??
|
||||
(sameRound ? prev.bettingEndMs : null) ??
|
||||
|
||||
Loading…
Reference in New Issue
Block a user