@@ -94,12 +94,16 @@ class MessageStoreImpl extends PerAccountStoreBase with MessageStore {
94
94
95
95
@override
96
96
void registerMessageList (MessageListView view) {
97
+ assert (! _disposed);
97
98
final added = _messageListViews.add (view);
98
99
assert (added);
99
100
}
100
101
101
102
@override
102
103
void unregisterMessageList (MessageListView view) {
104
+ // TODO: Add `assert(!_disposed);` here once we ensure [PerAccountStore] is
105
+ // only disposed after [MessageListView]s with references to it are
106
+ // disposed. See [dispose] for details.
103
107
final removed = _messageListViews.remove (view);
104
108
assert (removed);
105
109
}
@@ -122,6 +126,8 @@ class MessageStoreImpl extends PerAccountStoreBase with MessageStore {
122
126
}
123
127
}
124
128
129
+ bool _disposed = false ;
130
+
125
131
void dispose () {
126
132
// Not disposing the [MessageListView]s here, because they are owned by
127
133
// (i.e., they get [dispose]d by) the [_MessageListState], including in the
@@ -137,10 +143,14 @@ class MessageStoreImpl extends PerAccountStoreBase with MessageStore {
137
143
// [InheritedNotifier] to rebuild in the next frame) before the owner's
138
144
// `dispose` or `onNewStore` is called. Discussion:
139
145
// https://chat.zulip.org/#narrow/channel/243-mobile-team/topic/MessageListView.20lifecycle/near/2086893
146
+
147
+ assert (! _disposed);
148
+ _disposed = true ;
140
149
}
141
150
142
151
@override
143
152
Future <void > sendMessage ({required MessageDestination destination, required String content}) {
153
+ assert (! _disposed);
144
154
// TODO implement outbox; see design at
145
155
// https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/.23M3881.20Sending.20outbox.20messages.20is.20fraught.20with.20issues/near/1405739
146
156
return _apiSendMessage (connection,
@@ -152,6 +162,7 @@ class MessageStoreImpl extends PerAccountStoreBase with MessageStore {
152
162
153
163
@override
154
164
void reconcileMessages (List <Message > messages) {
165
+ assert (! _disposed);
155
166
// What to do when some of the just-fetched messages are already known?
156
167
// This is common and normal: in particular it happens when one message list
157
168
// overlaps another, e.g. a stream and a topic within it.
@@ -185,6 +196,7 @@ class MessageStoreImpl extends PerAccountStoreBase with MessageStore {
185
196
required String originalRawContent,
186
197
required String newContent,
187
198
}) async {
199
+ assert (! _disposed);
188
200
if (_editMessageRequests.containsKey (messageId)) {
189
201
throw StateError ('an edit request is already in progress' );
190
202
}
@@ -202,6 +214,8 @@ class MessageStoreImpl extends PerAccountStoreBase with MessageStore {
202
214
} catch (e) {
203
215
// TODO(log) if e is something unexpected
204
216
217
+ if (_disposed) return ;
218
+
205
219
final status = _editMessageRequests[messageId];
206
220
if (status == null ) {
207
221
// The event actually arrived before this request failed
@@ -216,6 +230,7 @@ class MessageStoreImpl extends PerAccountStoreBase with MessageStore {
216
230
217
231
@override
218
232
({String originalRawContent, String newContent}) takeFailedMessageEdit (int messageId) {
233
+ assert (! _disposed);
219
234
final status = _editMessageRequests.remove (messageId);
220
235
_notifyMessageListViewsForOneMessage (messageId);
221
236
if (status == null ) {
0 commit comments