kopia lustrzana https://gitlab.com/mysocialportal/relatica
Add Future.delays to json->obj conversion in Notifications and Timelines to reduce freezing
rodzic
c2ab53b65e
commit
330a19692f
|
@ -41,6 +41,9 @@ import '../services/network_status_service.dart';
|
||||||
import '../utils/network_utils.dart';
|
import '../utils/network_utils.dart';
|
||||||
import 'paging_data.dart';
|
import 'paging_data.dart';
|
||||||
|
|
||||||
|
const _maxProcessingMillis = 3;
|
||||||
|
const _processingSleep = Duration(milliseconds: 1);
|
||||||
|
|
||||||
class DirectMessagingClient extends FriendicaClient {
|
class DirectMessagingClient extends FriendicaClient {
|
||||||
static final _logger = Logger('$DirectMessagingClient');
|
static final _logger = Logger('$DirectMessagingClient');
|
||||||
|
|
||||||
|
@ -378,13 +381,22 @@ class NotificationsClient extends FriendicaClient {
|
||||||
final url = 'https://$serverName/api/v1/notifications?include_all=true';
|
final url = 'https://$serverName/api/v1/notifications?include_all=true';
|
||||||
final request = Uri.parse('$url&${page.toQueryParameters()}');
|
final request = Uri.parse('$url&${page.toQueryParameters()}');
|
||||||
_logger.finest(() => 'Getting new notifications');
|
_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();
|
_networkStatusService.finishNotificationUpdate();
|
||||||
return result
|
return result.execErrorCast();
|
||||||
.andThenSuccess((response) => response.map((jsonArray) => jsonArray
|
|
||||||
.map((json) => NotificationMastodonExtension.fromJson(json))
|
|
||||||
.toList()))
|
|
||||||
.execErrorCast();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureResult<bool, ExecError> clearNotifications() async {
|
FutureResult<bool, ExecError> clearNotifications() async {
|
||||||
|
@ -894,13 +906,12 @@ class TimelineClient extends FriendicaClient {
|
||||||
? '$baseUrl$pagingData'
|
? '$baseUrl$pagingData'
|
||||||
: '${baseUrl}screen_name=$userId$pagingData';
|
: '${baseUrl}screen_name=$userId$pagingData';
|
||||||
final request = Uri.parse(url);
|
final request = Uri.parse(url);
|
||||||
final result = (await _getApiListRequest(request).andThenSuccessAsync(
|
final result = await _getApiListRequest(request).transformAsync(
|
||||||
(postsJson) async => postsJson.data
|
(postsJson) async => await _timelineEntriesFromJson(postsJson.data),
|
||||||
.map((json) => TimelineEntryMastodonExtensions.fromJson(json))
|
);
|
||||||
.toList()))
|
|
||||||
.execErrorCast();
|
|
||||||
_networkStatusService.finishTimelineLoading();
|
_networkStatusService.finishTimelineLoading();
|
||||||
return result;
|
return result.execErrorCast();
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureResult<List<TimelineEntry>, ExecError> getTimeline(
|
FutureResult<List<TimelineEntry>, ExecError> getTimeline(
|
||||||
|
@ -913,13 +924,26 @@ class TimelineClient extends FriendicaClient {
|
||||||
'$baseUrl?exclude_replies=true&${page.toQueryParameters()}&$timelineQPs';
|
'$baseUrl?exclude_replies=true&${page.toQueryParameters()}&$timelineQPs';
|
||||||
final request = Uri.parse(url);
|
final request = Uri.parse(url);
|
||||||
_logger.finest(() => 'Getting ${type.toHumanKey()} with paging data $page');
|
_logger.finest(() => 'Getting ${type.toHumanKey()} with paging data $page');
|
||||||
final result = (await _getApiListRequest(request).andThenSuccessAsync(
|
final result = await _getApiListRequest(request).transformAsync(
|
||||||
(postsJson) async => postsJson.data
|
(postsJson) async => await _timelineEntriesFromJson(postsJson.data),
|
||||||
.map((json) => TimelineEntryMastodonExtensions.fromJson(json))
|
);
|
||||||
.toList()))
|
|
||||||
.execErrorCast();
|
|
||||||
_networkStatusService.finishTimelineLoading();
|
_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) {
|
String _typeToTimelinePath(TimelineIdentifiers type) {
|
||||||
|
|
Ładowanie…
Reference in New Issue