Skip to content

Commit ffda581

Browse files
committed
user [nfc]: Move selfUserId to PerAccountStoreBase
This `selfUserId` getter has 16 other references across 11 files. Thankfully our architecture, and in particular this way of combining the substores: class PerAccountStore extends PerAccountStoreBase with ChangeNotifier, EmojiStore, UserStore, ChannelStore, MessageStore { has meant that those all refer to it on the main PerAccountStore type, rather than on an individual substore like UserStore. So none of those need to be touched when we rearrange like this where the data is stored.
1 parent b6f712f commit ffda581

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

lib/model/store.dart

+18-6
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,20 @@ class CorePerAccountStore {
339339
required GlobalStore globalStore,
340340
required this.connection,
341341
required this.accountId,
342+
required this.selfUserId,
342343
}) : _globalStore = globalStore,
343344
assert(connection.realmUrl == globalStore.getAccount(accountId)!.realmUrl);
344345

345346
final GlobalStore _globalStore;
346347
final ApiConnection connection; // TODO(#135): update zulipFeatureLevel with events
347348
final int accountId;
349+
350+
// This isn't strictly needed as a field; it could be a getter
351+
// that uses `_globalStore.getAccount(accountId)`.
352+
// But we denormalize it here to save a hash-table lookup every time
353+
// the self-user ID is needed, which can be often.
354+
// It never changes on the account; see [GlobalStore.updateAccount].
355+
final int selfUserId;
348356
}
349357

350358
/// A base class for [PerAccountStore] and its substores,
@@ -384,6 +392,14 @@ abstract class PerAccountStoreBase {
384392
/// which is possible only if [PerAccountStore.dispose] has been called
385393
/// on this store.
386394
Account get account => _globalStore.getAccount(accountId)!;
395+
396+
/// The user ID of the "self-user",
397+
/// i.e. the account the person using this app is logged into.
398+
///
399+
/// This always equals the [Account.userId] on [account].
400+
///
401+
/// For the corresponding [User] object, see [UserStore.selfUser].
402+
int get selfUserId => _core.selfUserId;
387403
}
388404

389405
const _tryResolveUrl = tryResolveUrl;
@@ -441,6 +457,7 @@ class PerAccountStore extends PerAccountStoreBase with ChangeNotifier, EmojiStor
441457
globalStore: globalStore,
442458
connection: connection,
443459
accountId: accountId,
460+
selfUserId: account.userId,
444461
);
445462
final channels = ChannelStoreImpl(initialSnapshot: initialSnapshot);
446463
return PerAccountStore._(
@@ -464,9 +481,7 @@ class PerAccountStore extends PerAccountStoreBase with ChangeNotifier, EmojiStor
464481
typingStartedWaitPeriod: Duration(
465482
milliseconds: initialSnapshot.serverTypingStartedWaitPeriodMilliseconds),
466483
),
467-
users: UserStoreImpl(
468-
selfUserId: account.userId,
469-
initialSnapshot: initialSnapshot),
484+
users: UserStoreImpl(core: core, initialSnapshot: initialSnapshot),
470485
typingStatus: TypingStatus(
471486
selfUserId: account.userId,
472487
typingStartedExpiryPeriod: Duration(milliseconds: initialSnapshot.serverTypingStartedExpiryPeriodMilliseconds),
@@ -602,9 +617,6 @@ class PerAccountStore extends PerAccountStoreBase with ChangeNotifier, EmojiStor
602617
////////////////////////////////
603618
// Users and data about them.
604619

605-
@override
606-
int get selfUserId => _users.selfUserId;
607-
608620
@override
609621
User? getUser(int userId) => _users.getUser(userId);
610622

lib/model/user.dart

+4-14
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,10 @@ import '../api/model/events.dart';
22
import '../api/model/initial_snapshot.dart';
33
import '../api/model/model.dart';
44
import 'localizations.dart';
5+
import 'store.dart';
56

67
/// The portion of [PerAccountStore] describing the users in the realm.
7-
mixin UserStore {
8-
/// The user ID of the "self-user",
9-
/// i.e. the account the person using this app is logged into.
10-
///
11-
/// This always equals the [Account.userId] on [PerAccountStore.account].
12-
///
13-
/// For the corresponding [User] object, see [selfUser].
14-
int get selfUserId;
15-
8+
mixin UserStore on PerAccountStoreBase {
169
/// The user with the given ID, if that user is known.
1710
///
1811
/// There may be other users that are perfectly real but are
@@ -80,19 +73,16 @@ mixin UserStore {
8073
/// Generally the only code that should need this class is [PerAccountStore]
8174
/// itself. Other code accesses this functionality through [PerAccountStore],
8275
/// or through the mixin [UserStore] which describes its interface.
83-
class UserStoreImpl with UserStore {
76+
class UserStoreImpl extends PerAccountStoreBase with UserStore {
8477
UserStoreImpl({
85-
required this.selfUserId,
78+
required super.core,
8679
required InitialSnapshot initialSnapshot,
8780
}) : _users = Map.fromEntries(
8881
initialSnapshot.realmUsers
8982
.followedBy(initialSnapshot.realmNonActiveUsers)
9083
.followedBy(initialSnapshot.crossRealmBots)
9184
.map((user) => MapEntry(user.userId, user)));
9285

93-
@override
94-
final int selfUserId;
95-
9686
final Map<int, User> _users;
9787

9888
@override

0 commit comments

Comments
 (0)