Skip to content

Commit 9eafed4

Browse files
gnpricechrisbobbe
authored andcommitted
store [nfc]: Add GlobalStore reference on PerAccountStore
1 parent 6ef1996 commit 9eafed4

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

lib/model/store.dart

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,14 @@ abstract class GlobalStore extends ChangeNotifier {
150150
/// [UpdateMachine].
151151
class PerAccountStore extends ChangeNotifier with StreamStore {
152152
factory PerAccountStore.fromInitialSnapshot({
153+
required GlobalStore globalStore,
153154
required Account account,
154155
required ApiConnection connection,
155156
required InitialSnapshot initialSnapshot,
156157
}) {
157158
final streams = StreamStoreImpl(initialSnapshot: initialSnapshot);
158159
return PerAccountStore._(
160+
globalStore: globalStore,
159161
account: account,
160162
connection: connection,
161163
zulipVersion: initialSnapshot.zulipVersion,
@@ -181,6 +183,7 @@ class PerAccountStore extends ChangeNotifier with StreamStore {
181183
}
182184

183185
PerAccountStore._({
186+
required GlobalStore globalStore,
184187
required this.account,
185188
required this.connection,
186189
required this.zulipVersion,
@@ -193,7 +196,12 @@ class PerAccountStore extends ChangeNotifier with StreamStore {
193196
required this.users,
194197
required streams,
195198
required this.recentDmConversationsView,
196-
}) : _streams = streams;
199+
}) : _globalStore = globalStore,
200+
_streams = streams;
201+
202+
// We'll use this in an upcoming commit.
203+
// ignore: unused_field
204+
final GlobalStore _globalStore;
197205

198206
final Account account;
199207
final ApiConnection connection; // TODO(#135): update zulipFeatureLevel with events
@@ -453,7 +461,7 @@ class LiveGlobalStore extends GlobalStore {
453461

454462
@override
455463
Future<PerAccountStore> loadPerAccount(Account account) async {
456-
final updateMachine = await UpdateMachine.load(account);
464+
final updateMachine = await UpdateMachine.load(this, account);
457465
return updateMachine.store;
458466
}
459467

@@ -491,7 +499,7 @@ class UpdateMachine {
491499
/// Load the user's data from the server, and start an event queue going.
492500
///
493501
/// In the future this might load an old snapshot from local storage first.
494-
static Future<UpdateMachine> load(Account account) async {
502+
static Future<UpdateMachine> load(GlobalStore globalStore, Account account) async {
495503
final connection = ApiConnection.live(
496504
realmUrl: account.realmUrl, zulipFeatureLevel: account.zulipFeatureLevel,
497505
email: account.email, apiKey: account.apiKey);
@@ -503,6 +511,7 @@ class UpdateMachine {
503511
if (kDebugMode) print("initial fetch time: ${t.inMilliseconds}ms");
504512

505513
final store = PerAccountStore.fromInitialSnapshot(
514+
globalStore: globalStore,
506515
account: account,
507516
connection: connection,
508517
initialSnapshot: initialSnapshot,

test/example_data.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:zulip/model/narrow.dart';
77
import 'package:zulip/model/store.dart';
88

99
import 'api/fake_api.dart';
10+
import 'model/test_store.dart';
1011
import 'stdlib_checks.dart';
1112

1213
////////////////////////////////////////////////////////////////
@@ -414,9 +415,13 @@ UpdateMessageFlagsRemoveEvent updateMessageFlagsRemoveEvent(
414415
}
415416

416417
////////////////////////////////////////////////////////////////
417-
// The entire per-account state.
418+
// The entire per-account or global state.
418419
//
419420

421+
GlobalStore globalStore({List<Account> accounts = const []}) {
422+
return TestGlobalStore(accounts: accounts);
423+
}
424+
420425
InitialSnapshot initialSnapshot({
421426
String? queueId,
422427
int? lastEventId,
@@ -467,9 +472,11 @@ InitialSnapshot initialSnapshot({
467472
const _initialSnapshot = initialSnapshot;
468473

469474
PerAccountStore store({Account? account, InitialSnapshot? initialSnapshot}) {
475+
final effectiveAccount = account ?? selfAccount;
470476
return PerAccountStore.fromInitialSnapshot(
471-
account: account ?? selfAccount,
472-
connection: FakeApiConnection.fromAccount(account ?? selfAccount),
477+
globalStore: globalStore(accounts: [effectiveAccount]),
478+
account: effectiveAccount,
479+
connection: FakeApiConnection.fromAccount(effectiveAccount),
473480
initialSnapshot: initialSnapshot ?? _initialSnapshot(),
474481
);
475482
}

test/model/store_test.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ void main() {
3131

3232
final future1 = globalStore.perAccount(1);
3333
final store1 = PerAccountStore.fromInitialSnapshot(
34+
globalStore: globalStore,
3435
account: account1,
3536
connection: FakeApiConnection.fromAccount(account1),
3637
initialSnapshot: eg.initialSnapshot(),
@@ -42,6 +43,7 @@ void main() {
4243

4344
final future2 = globalStore.perAccount(2);
4445
final store2 = PerAccountStore.fromInitialSnapshot(
46+
globalStore: globalStore,
4547
account: account2,
4648
connection: FakeApiConnection.fromAccount(account2),
4749
initialSnapshot: eg.initialSnapshot(),
@@ -69,11 +71,13 @@ void main() {
6971

7072
final future2 = globalStore.perAccount(2);
7173
final store1 = PerAccountStore.fromInitialSnapshot(
74+
globalStore: globalStore,
7275
account: account1,
7376
connection: FakeApiConnection.fromAccount(account1),
7477
initialSnapshot: eg.initialSnapshot(),
7578
);
7679
final store2 = PerAccountStore.fromInitialSnapshot(
80+
globalStore: globalStore,
7781
account: account2,
7882
connection: FakeApiConnection.fromAccount(account2),
7983
initialSnapshot: eg.initialSnapshot(),
@@ -99,6 +103,7 @@ void main() {
99103
final future1 = globalStore.perAccount(1);
100104
check(globalStore.perAccountSync(1)).isNull();
101105
final store1 = PerAccountStore.fromInitialSnapshot(
106+
globalStore: globalStore,
102107
account: account1,
103108
connection: FakeApiConnection.fromAccount(account1),
104109
initialSnapshot: eg.initialSnapshot(),

test/model/test_store.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class TestGlobalStore extends GlobalStore {
5151
@override
5252
Future<PerAccountStore> loadPerAccount(Account account) {
5353
return Future.value(PerAccountStore.fromInitialSnapshot(
54+
globalStore: this,
5455
account: account,
5556
connection: FakeApiConnection.fromAccount(account),
5657
initialSnapshot: _initialSnapshots[account.id]!,

0 commit comments

Comments
 (0)