@@ -418,8 +418,9 @@ class LiveGlobalStore extends GlobalStore {
418
418
final AppDatabase _db;
419
419
420
420
@override
421
- Future <PerAccountStore > loadPerAccount (Account account) {
422
- return LivePerAccountStore .load (account);
421
+ Future <PerAccountStore > loadPerAccount (Account account) async {
422
+ final updateMachine = await UpdateMachine .load (account);
423
+ return updateMachine.store;
423
424
}
424
425
425
426
@override
@@ -436,26 +437,24 @@ class LiveGlobalStore extends GlobalStore {
436
437
}
437
438
}
438
439
439
- /// A [PerAccountStore] which polls an event queue to stay up to date.
440
+ /// A [PerAccountStore] plus an event-polling loop to stay up to date.
440
441
// TODO decouple "live"ness from polling and registerNotificationToken;
441
442
// the latter are made up of testable internal logic, not external integration
442
- class LivePerAccountStore extends PerAccountStore {
443
- LivePerAccountStore .fromInitialSnapshot ({
444
- required super .account,
445
- required super .connection,
446
- required super .initialSnapshot,
443
+ class UpdateMachine {
444
+ UpdateMachine .fromInitialSnapshot ({
445
+ required this .store,
446
+ required InitialSnapshot initialSnapshot,
447
447
}) : queueId = initialSnapshot.queueId ?? (() {
448
448
// The queueId is optional in the type, but should only be missing in the
449
449
// case of unauthenticated access to a web-public realm. We authenticated.
450
450
throw Exception ("bad initial snapshot: missing queueId" );
451
451
})(),
452
- lastEventId = initialSnapshot.lastEventId,
453
- super .fromInitialSnapshot ();
452
+ lastEventId = initialSnapshot.lastEventId;
454
453
455
454
/// Load the user's data from the server, and start an event queue going.
456
455
///
457
456
/// In the future this might load an old snapshot from local storage first.
458
- static Future <PerAccountStore > load (Account account) async {
457
+ static Future <UpdateMachine > load (Account account) async {
459
458
final connection = ApiConnection .live (
460
459
realmUrl: account.realmUrl, zulipFeatureLevel: account.zulipFeatureLevel,
461
460
email: account.email, apiKey: account.apiKey);
@@ -466,30 +465,33 @@ class LivePerAccountStore extends PerAccountStore {
466
465
// TODO log the time better
467
466
if (kDebugMode) print ("initial fetch time: ${t .inMilliseconds }ms" );
468
467
469
- final store = LivePerAccountStore .fromInitialSnapshot (
468
+ final store = PerAccountStore .fromInitialSnapshot (
470
469
account: account,
471
470
connection: connection,
472
471
initialSnapshot: initialSnapshot,
473
472
);
474
- store.poll ();
473
+ final updateMachine = UpdateMachine .fromInitialSnapshot (
474
+ store: store, initialSnapshot: initialSnapshot);
475
+ updateMachine.poll ();
475
476
// TODO do registerNotificationToken before registerQueue:
476
477
// https://github.com/zulip/zulip-flutter/pull/325#discussion_r1365982807
477
- store .registerNotificationToken ();
478
- return store ;
478
+ updateMachine .registerNotificationToken ();
479
+ return updateMachine ;
479
480
}
480
481
482
+ final PerAccountStore store;
481
483
final String queueId;
482
484
int lastEventId;
483
485
484
486
void poll () async {
485
487
while (true ) {
486
- final result = await getEvents (connection,
488
+ final result = await getEvents (store. connection,
487
489
queueId: queueId, lastEventId: lastEventId);
488
490
// TODO handle errors on get-events; retry with backoff
489
491
// TODO abort long-poll and close ApiConnection on [dispose]
490
492
final events = result.events;
491
493
for (final event in events) {
492
- handleEvent (event);
494
+ store. handleEvent (event);
493
495
}
494
496
if (events.isNotEmpty) {
495
497
lastEventId = events.last.id;
@@ -513,6 +515,6 @@ class LivePerAccountStore extends PerAccountStore {
513
515
Future <void > _registerNotificationToken () async {
514
516
final token = NotificationService .instance.token.value;
515
517
if (token == null ) return ;
516
- await NotificationService .registerToken (connection, token: token);
518
+ await NotificationService .registerToken (store. connection, token: token);
517
519
}
518
520
}
0 commit comments