Skip to content

Commit 3e0d499

Browse files
committed
store: Add and check _disposed on PerAccountStore and UpdateMachine
1 parent b2cedd5 commit 3e0d499

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

lib/model/store.dart

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ class PerAccountStore extends ChangeNotifier with EmojiStore, ChannelStore, Mess
445445
// End of data.
446446
////////////////////////////////////////////////////////////////
447447
448+
bool _disposed = false;
449+
448450
/// Called when the app is reassembled during debugging, e.g. for hot reload.
449451
///
450452
/// This will redo from scratch any computations we can, such as parsing
@@ -456,15 +458,19 @@ class PerAccountStore extends ChangeNotifier with EmojiStore, ChannelStore, Mess
456458

457459
@override
458460
void dispose() {
461+
assert(!_disposed);
459462
recentDmConversationsView.dispose();
460463
unreads.dispose();
461464
_messages.dispose();
462465
typingStatus.dispose();
463466
updateMachine?.dispose();
467+
_disposed = true;
464468
super.dispose();
465469
}
466470

467471
Future<void> handleEvent(Event event) async {
472+
assert(!_disposed);
473+
468474
switch (event) {
469475
case HeartbeatEvent():
470476
assert(debugLog("server event: heartbeat"));
@@ -606,6 +612,8 @@ class PerAccountStore extends ChangeNotifier with EmojiStore, ChannelStore, Mess
606612
}
607613

608614
Future<void> sendMessage({required MessageDestination destination, required String content}) {
615+
assert(!_disposed);
616+
609617
// TODO implement outbox; see design at
610618
// https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/.23M3881.20Sending.20outbox.20messages.20is.20fraught.20with.20issues/near/1405739
611619
return _apiSendMessage(connection,
@@ -790,6 +798,8 @@ class UpdateMachine {
790798
final String queueId;
791799
int lastEventId;
792800

801+
bool _disposed = false;
802+
793803
static Future<InitialSnapshot> _registerQueueWithRetry(
794804
ApiConnection connection) async {
795805
BackoffMachine? backoffMachine;
@@ -876,11 +886,15 @@ class UpdateMachine {
876886
}());
877887
}
878888

889+
if (_disposed) return;
890+
879891
final GetEventsResult result;
880892
try {
881893
result = await getEvents(store.connection,
882894
queueId: queueId, lastEventId: lastEventId);
883895
} catch (e) {
896+
if (_disposed) return;
897+
884898
store.isLoading = true;
885899
switch (e) {
886900
case ZulipApiException(code: 'BAD_EVENT_QUEUE_ID'):
@@ -908,6 +922,8 @@ class UpdateMachine {
908922
}
909923
}
910924

925+
if (_disposed) return;
926+
911927
// After one successful request, we reset backoff to its initial state.
912928
// That way if the user is off the network and comes back on, the app
913929
// doesn't wind up in a state where it's slow to recover the next time
@@ -929,6 +945,7 @@ class UpdateMachine {
929945
final events = result.events;
930946
for (final event in events) {
931947
await store.handleEvent(event);
948+
if (_disposed) return;
932949
}
933950
if (events.isNotEmpty) {
934951
lastEventId = events.last.id;
@@ -944,6 +961,8 @@ class UpdateMachine {
944961
// TODO(#322) save acked token, to dedupe updating it on the server
945962
// TODO(#323) track the addFcmToken/etc request, warn if not succeeding
946963
Future<void> registerNotificationToken() async {
964+
if (_disposed) return;
965+
947966
if (!debugEnableRegisterNotificationToken) {
948967
return;
949968
}
@@ -957,12 +976,14 @@ class UpdateMachine {
957976
await NotificationService.registerToken(store.connection, token: token);
958977
}
959978

960-
/// Cleans up resources.
979+
/// Cleans up resources and tells the instance not to make new API requests.
961980
///
962981
/// After this is called, the instance is not in a usable state
963982
/// and should be abandoned.
964983
void dispose() { // TODO abort long-poll and close ApiConnection
984+
assert(!_disposed);
965985
NotificationService.instance.token.removeListener(_registerNotificationToken);
986+
_disposed = true;
966987
}
967988

968989
/// In debug mode, controls whether [fetchEmojiData] should

0 commit comments

Comments
 (0)