Skip to content

Commit 0491f8d

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

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

lib/model/store.dart

Lines changed: 21 additions & 2 deletions
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;
@@ -865,11 +875,13 @@ class UpdateMachine {
865875
}
866876

867877
void poll() async {
868-
BackoffMachine? backoffMachine;
878+
assert(!_disposed);
869879

880+
BackoffMachine? backoffMachine;
870881
while (true) {
871882
if (_debugLoopSignal != null) {
872883
await _debugLoopSignal!.future;
884+
if (_disposed) return;
873885
assert(() {
874886
_debugLoopSignal = Completer();
875887
return true;
@@ -880,7 +892,10 @@ class UpdateMachine {
880892
try {
881893
result = await getEvents(store.connection,
882894
queueId: queueId, lastEventId: lastEventId);
895+
if (_disposed) return;
883896
} catch (e) {
897+
if (_disposed) return;
898+
884899
store.isLoading = true;
885900
switch (e) {
886901
case ZulipApiException(code: 'BAD_EVENT_QUEUE_ID'):
@@ -929,6 +944,7 @@ class UpdateMachine {
929944
final events = result.events;
930945
for (final event in events) {
931946
await store.handleEvent(event);
947+
if (_disposed) return;
932948
}
933949
if (events.isNotEmpty) {
934950
lastEventId = events.last.id;
@@ -944,6 +960,7 @@ class UpdateMachine {
944960
// TODO(#322) save acked token, to dedupe updating it on the server
945961
// TODO(#323) track the addFcmToken/etc request, warn if not succeeding
946962
Future<void> registerNotificationToken() async {
963+
assert(!_disposed);
947964
if (!debugEnableRegisterNotificationToken) {
948965
return;
949966
}
@@ -957,12 +974,14 @@ class UpdateMachine {
957974
await NotificationService.registerToken(store.connection, token: token);
958975
}
959976

960-
/// Cleans up resources.
977+
/// Cleans up resources and tells the instance not to make new API requests.
961978
///
962979
/// After this is called, the instance is not in a usable state
963980
/// and should be abandoned.
964981
void dispose() { // TODO abort long-poll and close ApiConnection
982+
assert(!_disposed);
965983
NotificationService.instance.token.removeListener(_registerNotificationToken);
984+
_disposed = true;
966985
}
967986

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

0 commit comments

Comments
 (0)