11import  'dart:async' ;
22import  'dart:convert' ;
33import  'dart:io' ;
4+ import  'dart:math' ;
45
56import  'package:drift/drift.dart' ;
67import  'package:drift/native.dart' ;
@@ -712,6 +713,8 @@ class PerAccountStore extends PerAccountStoreBase with
712713  @override 
713714  void  dispose () {
714715    assert (! _disposed);
716+     _postRestartTimer? .cancel ();
717+     _postRestartTimer =  null ;
715718    recentDmConversationsView.dispose ();
716719    unreads.dispose ();
717720    _messages.dispose ();
@@ -725,21 +728,47 @@ class PerAccountStore extends PerAccountStoreBase with
725728    super .dispose ();
726729  }
727730
731+   Timer ?  _postRestartTimer;
732+ 
733+   void  _postRestart () {
734+     assert (! _disposed);
735+     // TODO(#1271): show user an explanation for the reload?  e.g., 
736+     //   a snack bar saying the server was upgraded 
737+     _globalStore._reloadPerAccount (accountId);
738+   }
739+ 
728740  Future <void > _handleRestartEvent (RestartEvent  event) async  {
741+     assert (! _disposed);
729742    if  (event.zulipVersion ==  account.zulipVersion
730743        &&  event.zulipMergeBase ==  account.zulipMergeBase
731744        &&  event.zulipFeatureLevel ==  account.zulipFeatureLevel) {
732745      return ;
733746    }
734747
735-     // TODO(#1271): replace event queue, if zulipFeatureLevel makes it necessary 
736748    await  _globalStore.updateAccount (accountId, AccountsCompanion (
737749      zulipVersion:  Value (event.zulipVersion),
738750      zulipMergeBase:  Value (event.zulipMergeBase),
739751      zulipFeatureLevel:  Value (event.zulipFeatureLevel),
740752    ));
741753    connection.zulipFeatureLevel =  event.zulipFeatureLevel;
742754    notifyListeners ();
755+ 
756+     // In principle, there could be an API change such that we should 
757+     // reload promptly after the server upgraded across it. 
758+     // The last known time that happened (as of 2025-10) was 2023-02, 
759+     // at FL-163 before server-7, and so before the oldest servers we support. 
760+     // See https://github.com/zulip/zulip-flutter/issues/1271 . 
761+     // TODO(#1271): what about *downgrade*?  Reload promptly then? 
762+     // 
763+     // Otherwise, a server upgrade means we should reload eventually, 
764+     // so as to fully pick up the new server's features. 
765+ 
766+     if  (_postRestartTimer !=  null ) return ;
767+     const  minWaitMs =  1  *  Duration .millisecondsPerMinute;
768+     const  maxWaitMs =  1  *  Duration .millisecondsPerHour;
769+     final  waitMs =  minWaitMs +  Random ().nextInt (maxWaitMs -  minWaitMs +  1 );
770+     final  waitDuration =  Duration (milliseconds:  waitMs);
771+     _postRestartTimer =  Timer (waitDuration, _postRestart);
743772  }
744773
745774  Future <void > handleEvent (Event  event) async  {
0 commit comments