Skip to content

Commit 7da2087

Browse files
committed
store: Add and check _disposed on PerAccountStore and UpdateMachine
1 parent cd81b95 commit 7da2087

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

lib/model/store.dart

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,17 +454,23 @@ class PerAccountStore extends ChangeNotifier with EmojiStore, ChannelStore, Mess
454454
autocompleteViewManager.reassemble();
455455
}
456456

457+
bool _disposed = false;
458+
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;
@@ -868,8 +878,11 @@ class UpdateMachine {
868878
BackoffMachine? backoffMachine;
869879

870880
while (true) {
881+
if (_disposed) return;
882+
871883
if (_debugLoopSignal != null) {
872884
await _debugLoopSignal!.future;
885+
if (_disposed) return;
873886
assert(() {
874887
_debugLoopSignal = Completer();
875888
return true;
@@ -880,7 +893,10 @@ class UpdateMachine {
880893
try {
881894
result = await getEvents(store.connection,
882895
queueId: queueId, lastEventId: lastEventId);
896+
if (_disposed) return;
883897
} catch (e) {
898+
if (_disposed) return;
899+
884900
store.isLoading = true;
885901
switch (e) {
886902
case ZulipApiException(code: 'BAD_EVENT_QUEUE_ID'):
@@ -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,7 @@ 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+
assert(!_disposed);
947965
if (!debugEnableRegisterNotificationToken) {
948966
return;
949967
}
@@ -957,12 +975,14 @@ class UpdateMachine {
957975
await NotificationService.registerToken(store.connection, token: token);
958976
}
959977

960-
/// Cleans up resources.
978+
/// Cleans up resources and tells the instance not to make new API requests.
961979
///
962980
/// After this is called, the instance is not in a usable state
963981
/// and should be abandoned.
964982
void dispose() { // TODO abort long-poll and close ApiConnection
983+
assert(!_disposed);
965984
NotificationService.instance.token.removeListener(_registerNotificationToken);
985+
_disposed = true;
966986
}
967987

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

0 commit comments

Comments
 (0)