Skip to content

Commit 31c7511

Browse files
chrisbobbegnprice
andcommitted
store [nfc]: Add PerAccountStoreBase to provide connection to substores
And demonstrate its use, with MessageStoreImpl. This will be a convenient way to provide other common things to substores without needing boilerplate on each substore for each thing. Co-authored-by: Greg Price <[email protected]>
1 parent d224a5b commit 31c7511

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

lib/model/message.dart

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import 'dart:convert';
22

3-
import '../api/core.dart';
43
import '../api/model/events.dart';
54
import '../api/model/model.dart';
65
import '../api/route/messages.dart';
76
import '../log.dart';
87
import 'message_list.dart';
8+
import 'store.dart';
99

1010
const _apiSendMessage = sendMessage; // Bit ugly; for alternatives, see: https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/flutter.3A.20PerAccountStore.20methods/near/1545809
1111

@@ -37,14 +37,12 @@ mixin MessageStore {
3737
void reconcileMessages(List<Message> messages);
3838
}
3939

40-
class MessageStoreImpl with MessageStore {
41-
MessageStoreImpl({required this.connection})
40+
class MessageStoreImpl extends PerAccountStoreBase with MessageStore {
41+
MessageStoreImpl({required super.core})
4242
// There are no messages in InitialSnapshot, so we don't have
4343
// a use case for initializing MessageStore with nonempty [messages].
4444
: messages = {};
4545

46-
final ApiConnection connection;
47-
4846
@override
4947
final Map<int, Message> messages;
5048

lib/model/store.dart

+24-6
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,24 @@ abstract class GlobalStore extends ChangeNotifier {
327327

328328
class AccountNotFoundException implements Exception {}
329329

330+
/// A bundle of items that are useful to [PerAccountStore] and its substores.
331+
class CorePerAccountStore {
332+
CorePerAccountStore({required this.connection});
333+
334+
final ApiConnection connection; // TODO(#135): update zulipFeatureLevel with events
335+
}
336+
337+
/// A base class for [PerAccountStore] and its substores,
338+
/// with getters providing the items in [CorePerAccountStore].
339+
abstract class PerAccountStoreBase {
340+
PerAccountStoreBase({required CorePerAccountStore core})
341+
: _core = core;
342+
343+
final CorePerAccountStore _core;
344+
345+
ApiConnection get connection => _core.connection;
346+
}
347+
330348
/// Store for the user's data for a given Zulip account.
331349
///
332350
/// This should always have a consistent snapshot of the state on the server,
@@ -335,7 +353,7 @@ class AccountNotFoundException implements Exception {}
335353
/// This class does not attempt to poll an event queue
336354
/// to keep the data up to date. For that behavior, see
337355
/// [UpdateMachine].
338-
class PerAccountStore extends ChangeNotifier with EmojiStore, UserStore, ChannelStore, MessageStore {
356+
class PerAccountStore extends PerAccountStoreBase with ChangeNotifier, EmojiStore, UserStore, ChannelStore, MessageStore {
339357
/// Construct a store for the user's data, starting from the given snapshot.
340358
///
341359
/// The global store must already have been updated with
@@ -368,10 +386,11 @@ class PerAccountStore extends ChangeNotifier with EmojiStore, UserStore, Channel
368386
}
369387

370388
final realmUrl = account.realmUrl;
389+
final core = CorePerAccountStore(connection: connection);
371390
final channels = ChannelStoreImpl(initialSnapshot: initialSnapshot);
372391
return PerAccountStore._(
373392
globalStore: globalStore,
374-
connection: connection,
393+
core: core,
375394
queueId: queueId,
376395
realmUrl: realmUrl,
377396
realmWildcardMentionPolicy: initialSnapshot.realmWildcardMentionPolicy,
@@ -401,7 +420,7 @@ class PerAccountStore extends ChangeNotifier with EmojiStore, UserStore, Channel
401420
typingStartedExpiryPeriod: Duration(milliseconds: initialSnapshot.serverTypingStartedExpiryPeriodMilliseconds),
402421
),
403422
channels: channels,
404-
messages: MessageStoreImpl(connection: connection),
423+
messages: MessageStoreImpl(core: core),
405424
unreads: Unreads(
406425
initial: initialSnapshot.unreadMsgs,
407426
selfUserId: account.userId,
@@ -415,7 +434,7 @@ class PerAccountStore extends ChangeNotifier with EmojiStore, UserStore, Channel
415434

416435
PerAccountStore._({
417436
required GlobalStore globalStore,
418-
required this.connection,
437+
required super.core,
419438
required this.queueId,
420439
required this.realmUrl,
421440
required this.realmWildcardMentionPolicy,
@@ -438,7 +457,7 @@ class PerAccountStore extends ChangeNotifier with EmojiStore, UserStore, Channel
438457
required this.recentDmConversationsView,
439458
required this.recentSenders,
440459
}) : assert(realmUrl == globalStore.getAccount(accountId)!.realmUrl),
441-
assert(realmUrl == connection.realmUrl),
460+
assert(realmUrl == core.connection.realmUrl),
442461
assert(emoji.realmUrl == realmUrl),
443462
_globalStore = globalStore,
444463
_realmEmptyTopicDisplayName = realmEmptyTopicDisplayName,
@@ -454,7 +473,6 @@ class PerAccountStore extends ChangeNotifier with EmojiStore, UserStore, Channel
454473
// Where data comes from in the first place.
455474

456475
final GlobalStore _globalStore;
457-
final ApiConnection connection; // TODO(#135): update zulipFeatureLevel with events
458476

459477
final String queueId;
460478
UpdateMachine? get updateMachine => _updateMachine;

0 commit comments

Comments
 (0)