mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-08 17:11:14 +00:00
refactor: Optimistic state update when removing all finished jobs
This commit is contained in:
parent
831a0e95eb
commit
bdd00683cd
|
@ -61,16 +61,7 @@ class ServerJobsBloc extends Bloc<ServerJobsEvent, ServerJobsState> {
|
|||
final RemoveAllFinishedJobs event,
|
||||
final Emitter<ServerJobsState> emit,
|
||||
) async {
|
||||
final List<ServerJob> finishedJobs = state.serverJobList
|
||||
.where(
|
||||
(final ServerJob job) =>
|
||||
job.status == JobStatusEnum.finished ||
|
||||
job.status == JobStatusEnum.error,
|
||||
)
|
||||
.toList();
|
||||
for (final ServerJob job in finishedJobs) {
|
||||
await getIt<ApiConnectionRepository>().removeServerJob(job.uid);
|
||||
}
|
||||
await getIt<ApiConnectionRepository>().removeAllFinishedServerJobs();
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
|
@ -42,6 +42,29 @@ class ApiConnectionRepository {
|
|||
_dataStream.add(_apiData);
|
||||
}
|
||||
|
||||
Future<void> removeAllFinishedServerJobs() async {
|
||||
final List<ServerJob> finishedJobs = _apiData.serverJobs.data
|
||||
?.where(
|
||||
(final ServerJob element) =>
|
||||
element.status == JobStatusEnum.finished ||
|
||||
element.status == JobStatusEnum.error,
|
||||
)
|
||||
.toList() ??
|
||||
[];
|
||||
// Optimistically remove the jobs from the list
|
||||
_apiData.serverJobs.data?.removeWhere(
|
||||
(final ServerJob element) =>
|
||||
element.status == JobStatusEnum.finished ||
|
||||
element.status == JobStatusEnum.error,
|
||||
);
|
||||
_dataStream.add(_apiData);
|
||||
|
||||
await Future.forEach<ServerJob>(
|
||||
finishedJobs,
|
||||
(final ServerJob job) async => removeServerJob(job.uid),
|
||||
);
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_dataStream.close();
|
||||
_connectionStatusStream.close();
|
||||
|
|
Loading…
Reference in a new issue