mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-24 01:36:38 +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 RemoveAllFinishedJobs event,
|
||||||
final Emitter<ServerJobsState> emit,
|
final Emitter<ServerJobsState> emit,
|
||||||
) async {
|
) async {
|
||||||
final List<ServerJob> finishedJobs = state.serverJobList
|
await getIt<ApiConnectionRepository>().removeAllFinishedServerJobs();
|
||||||
.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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|
|
@ -42,6 +42,29 @@ class ApiConnectionRepository {
|
||||||
_dataStream.add(_apiData);
|
_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() {
|
void dispose() {
|
||||||
_dataStream.close();
|
_dataStream.close();
|
||||||
_connectionStatusStream.close();
|
_connectionStatusStream.close();
|
||||||
|
|
Loading…
Reference in a new issue