Add Future.delays to json->obj conversion in Notifications and Timelines to reduce freezing

codemagic-setup
Hank Grabowski 2023-04-28 21:07:05 -04:00
rodzic c2ab53b65e
commit 330a19692f
1 zmienionych plików z 42 dodań i 18 usunięć

Wyświetl plik

@ -41,6 +41,9 @@ import '../services/network_status_service.dart';
import '../utils/network_utils.dart';
import 'paging_data.dart';
const _maxProcessingMillis = 3;
const _processingSleep = Duration(milliseconds: 1);
class DirectMessagingClient extends FriendicaClient {
static final _logger = Logger('$DirectMessagingClient');
@ -378,13 +381,22 @@ class NotificationsClient extends FriendicaClient {
final url = 'https://$serverName/api/v1/notifications?include_all=true';
final request = Uri.parse('$url&${page.toQueryParameters()}');
_logger.finest(() => 'Getting new notifications');
final result = await _getApiListRequest(request);
final result =
await _getApiListRequest(request).transformAsync((response) async {
final notifications = <UserNotification>[];
final st = Stopwatch()..start();
for (final json in response.data) {
if (st.elapsedMilliseconds > _maxProcessingMillis) {
await Future.delayed(_processingSleep, () => st.reset());
}
notifications.add(NotificationMastodonExtension.fromJson(json));
}
return PagedResponse(notifications,
id: response.id, previous: response.previous, next: response.next);
});
_networkStatusService.finishNotificationUpdate();
return result
.andThenSuccess((response) => response.map((jsonArray) => jsonArray
.map((json) => NotificationMastodonExtension.fromJson(json))
.toList()))
.execErrorCast();
return result.execErrorCast();
}
FutureResult<bool, ExecError> clearNotifications() async {
@ -894,13 +906,12 @@ class TimelineClient extends FriendicaClient {
? '$baseUrl$pagingData'
: '${baseUrl}screen_name=$userId$pagingData';
final request = Uri.parse(url);
final result = (await _getApiListRequest(request).andThenSuccessAsync(
(postsJson) async => postsJson.data
.map((json) => TimelineEntryMastodonExtensions.fromJson(json))
.toList()))
.execErrorCast();
final result = await _getApiListRequest(request).transformAsync(
(postsJson) async => await _timelineEntriesFromJson(postsJson.data),
);
_networkStatusService.finishTimelineLoading();
return result;
return result.execErrorCast();
}
FutureResult<List<TimelineEntry>, ExecError> getTimeline(
@ -913,13 +924,26 @@ class TimelineClient extends FriendicaClient {
'$baseUrl?exclude_replies=true&${page.toQueryParameters()}&$timelineQPs';
final request = Uri.parse(url);
_logger.finest(() => 'Getting ${type.toHumanKey()} with paging data $page');
final result = (await _getApiListRequest(request).andThenSuccessAsync(
(postsJson) async => postsJson.data
.map((json) => TimelineEntryMastodonExtensions.fromJson(json))
.toList()))
.execErrorCast();
final result = await _getApiListRequest(request).transformAsync(
(postsJson) async => await _timelineEntriesFromJson(postsJson.data),
);
_networkStatusService.finishTimelineLoading();
return result;
return result.execErrorCast();
}
Future<List<TimelineEntry>> _timelineEntriesFromJson(
List<dynamic> postsJson,
) async {
final finalResult = <TimelineEntry>[];
final st = Stopwatch()..start();
for (final json in postsJson) {
if (st.elapsedMilliseconds > _maxProcessingMillis) {
await Future.delayed(_processingSleep, () => st.reset());
}
finalResult.add(TimelineEntryMastodonExtensions.fromJson(json));
}
return finalResult;
}
String _typeToTimelinePath(TimelineIdentifiers type) {