Skip to content

Commit fffc6fc

Browse files
committed
message [nfc]: Have editMessage return Future<void> instead of void
1 parent 11ee6e9 commit fffc6fc

File tree

4 files changed

+41
-32
lines changed

4 files changed

+41
-32
lines changed

lib/model/message.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,15 @@ mixin MessageStore {
5858
/// Should only be called when there is no current edit request for [messageId],
5959
/// i.e., [getEditMessageErrorStatus] returns null for [messageId].
6060
///
61+
/// The returned [Future] settles when the edit-message response is received.
62+
/// The [Future] resolves if the request succeeded and rejects if it failed,
63+
/// unless the event already arrived or the message was deleted,
64+
/// in which case it resolves.
65+
///
6166
/// See also:
6267
/// * [getEditMessageErrorStatus]
6368
/// * [takeFailedMessageEdit]
64-
void editMessage({
69+
Future<void> editMessage({
6570
required int messageId,
6671
required String originalRawContent,
6772
required String newContent,
@@ -104,7 +109,7 @@ mixin ProxyMessageStore on MessageStore {
104109
return messageStore.getEditMessageErrorStatus(messageId);
105110
}
106111
@override
107-
void editMessage({
112+
Future<void> editMessage({
108113
required int messageId,
109114
required String originalRawContent,
110115
required String newContent,
@@ -315,7 +320,7 @@ class MessageStoreImpl extends HasRealmStore with MessageStore, _OutboxMessageSt
315320
final Map<int, _EditMessageRequestStatus> _editMessageRequests = {};
316321

317322
@override
318-
void editMessage({
323+
Future<void> editMessage({
319324
required int messageId,
320325
required String originalRawContent,
321326
required String newContent,
@@ -349,6 +354,7 @@ class MessageStoreImpl extends HasRealmStore with MessageStore, _OutboxMessageSt
349354
}
350355
status.hasError = true;
351356
_notifyMessageListViewsForOneMessage(messageId);
357+
rethrow;
352358
}
353359
}
354360

lib/widgets/compose_box.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1821,7 +1821,7 @@ class _EditMessageBanner extends _Banner {
18211821
store.editMessage(
18221822
messageId: messageId,
18231823
originalRawContent: originalRawContent,
1824-
newContent: newContent);
1824+
newContent: newContent).onError((_, _) {}); // TODO show error feedback
18251825
}
18261826

18271827
@override

test/model/message_test.dart

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:crypto/crypto.dart';
77
import 'package:fake_async/fake_async.dart';
88
import 'package:http/http.dart' as http;
99
import 'package:test/scaffolding.dart';
10+
import 'package:zulip/api/exception.dart';
1011
import 'package:zulip/api/model/events.dart';
1112
import 'package:zulip/api/model/model.dart';
1213
import 'package:zulip/api/model/submessage.dart';
@@ -603,8 +604,8 @@ void main() {
603604

604605
connection.prepare(
605606
json: UpdateMessageResult().toJson(), delay: Duration(seconds: 1));
606-
store.editMessage(messageId: message.id,
607-
originalRawContent: 'old content', newContent: 'new content');
607+
unawaited(store.editMessage(messageId: message.id,
608+
originalRawContent: 'old content', newContent: 'new content'));
608609
checkRequest(message.id,
609610
prevContent: 'old content',
610611
content: 'new content');
@@ -634,8 +635,8 @@ void main() {
634635

635636
connection.prepare(
636637
json: UpdateMessageResult().toJson(), delay: Duration(seconds: 1));
637-
store.editMessage(messageId: message.id,
638-
originalRawContent: 'old content', newContent: 'new content');
638+
unawaited(store.editMessage(messageId: message.id,
639+
originalRawContent: 'old content', newContent: 'new content'));
639640
checkRequest(message.id,
640641
prevContent: 'old content',
641642
content: 'new content');
@@ -647,8 +648,8 @@ void main() {
647648
check(store.getEditMessageErrorStatus(otherMessage.id)).isNull();
648649
connection.prepare(
649650
json: UpdateMessageResult().toJson(), delay: Duration(seconds: 1));
650-
store.editMessage(messageId: otherMessage.id,
651-
originalRawContent: 'other message old content', newContent: 'other message new content');
651+
unawaited(store.editMessage(messageId: otherMessage.id,
652+
originalRawContent: 'other message old content', newContent: 'other message new content'));
652653
checkRequest(otherMessage.id,
653654
prevContent: 'other message old content',
654655
content: 'other message new content');
@@ -682,8 +683,8 @@ void main() {
682683
check(store.getEditMessageErrorStatus(message.id)).isNull();
683684

684685
connection.prepare(apiException: eg.apiBadRequest(), delay: Duration(seconds: 1));
685-
store.editMessage(messageId: message.id,
686-
originalRawContent: 'old content', newContent: 'new content');
686+
unawaited(check(store.editMessage(messageId: message.id,
687+
originalRawContent: 'old content', newContent: 'new content')).throws<ZulipApiException>());
687688
checkNotifiedOnce();
688689
async.elapse(Duration(seconds: 1));
689690
check(store.getEditMessageErrorStatus(message.id)).isNotNull().isTrue();
@@ -695,8 +696,8 @@ void main() {
695696
check(store.getEditMessageErrorStatus(message.id)).isNull();
696697

697698
connection.prepare(apiException: eg.apiBadRequest(), delay: Duration(seconds: 1));
698-
store.editMessage(messageId: message.id,
699-
originalRawContent: 'old content', newContent: 'new content');
699+
unawaited(check(store.editMessage(messageId: message.id,
700+
originalRawContent: 'old content', newContent: 'new content')).throws<ZulipApiException>());
700701
checkNotifiedOnce();
701702
async.elapse(Duration(seconds: 1));
702703
check(store.getEditMessageErrorStatus(message.id)).isNotNull().isTrue();
@@ -718,8 +719,8 @@ void main() {
718719

719720
connection.prepare(
720721
json: UpdateMessageResult().toJson(), delay: Duration(seconds: 1));
721-
store.editMessage(messageId: message.id,
722-
originalRawContent: 'old content', newContent: 'new content');
722+
unawaited(store.editMessage(messageId: message.id,
723+
originalRawContent: 'old content', newContent: 'new content'));
723724
async.elapse(Duration(milliseconds: 500));
724725
check(connection.takeRequests()).length.equals(1);
725726
checkNotifiedOnce();
@@ -738,8 +739,8 @@ void main() {
738739

739740
connection.prepare(
740741
httpException: const SocketException('failed'), delay: Duration(seconds: 1));
741-
store.editMessage(messageId: message.id,
742-
originalRawContent: 'old content', newContent: 'new content');
742+
unawaited(store.editMessage(messageId: message.id,
743+
originalRawContent: 'old content', newContent: 'new content'));
743744
checkNotifiedOnce();
744745

745746
async.elapse(Duration(milliseconds: 500));
@@ -760,8 +761,8 @@ void main() {
760761

761762
connection.prepare(
762763
httpException: const SocketException('failed'), delay: Duration(seconds: 1));
763-
store.editMessage(messageId: message.id,
764-
originalRawContent: 'old content', newContent: 'new content');
764+
unawaited(check(store.editMessage(messageId: message.id,
765+
originalRawContent: 'old content', newContent: 'new content')).throws<NetworkException>());
765766
checkNotifiedOnce();
766767

767768
async.elapse(Duration(seconds: 1));
@@ -781,8 +782,8 @@ void main() {
781782

782783
connection.prepare(
783784
httpException: const SocketException('failed'), delay: Duration(seconds: 1));
784-
store.editMessage(messageId: message.id,
785-
originalRawContent: 'old content', newContent: 'new content');
785+
unawaited(check(store.editMessage(messageId: message.id,
786+
originalRawContent: 'old content', newContent: 'new content')).throws<NetworkException>());
786787
checkNotifiedOnce();
787788

788789
async.elapse(Duration(seconds: 1));
@@ -801,8 +802,8 @@ void main() {
801802
check(store.getEditMessageErrorStatus(message.id)).isNull();
802803

803804
connection.prepare(apiException: eg.apiBadRequest(), delay: Duration(seconds: 1));
804-
store.editMessage(messageId: message.id,
805-
originalRawContent: 'old content', newContent: 'new content');
805+
unawaited(check(store.editMessage(messageId: message.id,
806+
originalRawContent: 'old content', newContent: 'new content')).throws<ZulipApiException>());
806807
checkNotifiedOnce();
807808
async.elapse(Duration(seconds: 1));
808809
check(store.getEditMessageErrorStatus(message.id)).isNotNull().isTrue();
@@ -818,8 +819,8 @@ void main() {
818819
check(store.getEditMessageErrorStatus(message.id)).isNull();
819820

820821
connection.prepare(apiException: eg.apiBadRequest(), delay: Duration(seconds: 1));
821-
store.editMessage(messageId: message.id,
822-
originalRawContent: 'old content', newContent: 'new content');
822+
unawaited(store.editMessage(messageId: message.id,
823+
originalRawContent: 'old content', newContent: 'new content'));
823824
checkNotifiedOnce();
824825

825826
async.elapse(Duration(milliseconds: 500));
@@ -843,8 +844,8 @@ void main() {
843844

844845
connection.prepare(
845846
json: UpdateMessageResult().toJson(), delay: Duration(seconds: 1));
846-
store.editMessage(messageId: message.id,
847-
originalRawContent: 'old content', newContent: 'new content');
847+
unawaited(store.editMessage(messageId: message.id,
848+
originalRawContent: 'old content', newContent: 'new content'));
848849
checkNotifiedOnce();
849850

850851
async.elapse(Duration(milliseconds: 500));

test/widgets/message_list_test.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'dart:async';
12
import 'dart:convert';
23
import 'dart:io';
34

@@ -9,6 +10,7 @@ import 'package:flutter/rendering.dart';
910
import 'package:flutter_checks/flutter_checks.dart';
1011
import 'package:flutter_test/flutter_test.dart';
1112
import 'package:http/http.dart' as http;
13+
import 'package:zulip/api/exception.dart';
1214
import 'package:zulip/api/model/events.dart';
1315
import 'package:zulip/api/model/initial_snapshot.dart';
1416
import 'package:zulip/api/model/model.dart';
@@ -2274,9 +2276,9 @@ void main() {
22742276
messages: [message]);
22752277

22762278
connection.prepare(json: UpdateMessageResult().toJson());
2277-
store.editMessage(messageId: message.id,
2279+
unawaited(store.editMessage(messageId: message.id,
22782280
originalRawContent: 'foo',
2279-
newContent: 'bar');
2281+
newContent: 'bar'));
22802282
await tester.pump(Duration.zero);
22812283
checkEditInProgress(tester);
22822284
await store.handleEvent(eg.updateMessageEditEvent(message));
@@ -2291,9 +2293,9 @@ void main() {
22912293
messages: [message]);
22922294

22932295
connection.prepare(apiException: eg.apiBadRequest(), delay: Duration(seconds: 1));
2294-
store.editMessage(messageId: message.id,
2296+
unawaited(check(store.editMessage(messageId: message.id,
22952297
originalRawContent: 'foo',
2296-
newContent: 'bar');
2298+
newContent: 'bar')).throws<ZulipApiException>());
22972299
await tester.pump(Duration.zero);
22982300
checkEditInProgress(tester);
22992301
await tester.pump(Duration(seconds: 1));

0 commit comments

Comments
 (0)