Skip to content

Commit

Permalink
RecurringTaskFinishedEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
alexobviously committed May 24, 2023
1 parent 9496e6d commit d1cc6d0
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## 1.0.0
- Upgraded SDK constraint to Dart 3.
- `Secretary.waitForEmpty()`: wait for the task list to be empty. Useful for cases where a fixed number of tasks is added at once, and you just want to wait for all of them to finish.
- Recurring tasks now emit a `RecurringTaskFinishedEvent` when they finish.
- Upgraded SDK constraint to Dart 3.
- Removed `firstWhereOrNull` extension in favour of Dart 3 `firstOrNull`.

## 0.4.1
- `Secretary.link()`: connects a Secretary to another, so that whenever a task successfully completes in the first, one is added to the second.
Expand Down
13 changes: 13 additions & 0 deletions lib/src/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,16 @@ class FailureEvent<K, T> extends ErrorEvent<K, T> {
@override
String toString() => 'FailureEvent($key, $attempts/$attempts, $error)';
}

class RecurringTaskFinishedEvent<K, T> extends SecretaryEvent<K, T> {
final bool invalidated;

const RecurringTaskFinishedEvent({
required super.key,
required super.errors,
this.invalidated = false,
});

@override
String toString() => 'RecurringTaskFinishedEvent($key, $invalidated)';
}
2 changes: 2 additions & 0 deletions lib/src/recurring_task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class RecurringTask<K, T> {
int get numRuns => runs.length;
bool get canRun =>
(maxRuns == 0 || runs.length < maxRuns) && validator(executionParams);
bool get valid => validator(executionParams);
List<Object> get errors => runs.expand((e) => e.errors).toList();

/// Gets the execution params for the next run.
ExecutionParams<K, T> get executionParams =>
Expand Down
6 changes: 5 additions & 1 deletion lib/src/secretary.dart
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,11 @@ class Secretary<K, T> {
_timers[recurringTask.key] = timer;
} else {
recurringTasks.remove(recurringTask.key);
// TODO: emit some sort of event?
_addEvent(RecurringTaskFinishedEvent(
key: recurringTask.key,
errors: recurringTask.errors,
invalidated: !recurringTask.valid,
));
_emitState();
}
}
Expand Down
2 changes: 2 additions & 0 deletions test/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'package:secretary/secretary.dart';
final retryPredicate = predicate<SecretaryEvent>((e) => e.isRetry);
final failurePredicate = predicate<SecretaryEvent>((e) => e.isFailure);
final successPredicate = predicate<SecretaryEvent>((e) => e.isSuccess);
final recurringFinishedPredicate =
predicate<SecretaryEvent>((e) => e is RecurringTaskFinishedEvent);

Matcher hasKey<K, T>(K key) =>
predicate<SecretaryEvent<K, T>>((e) => e.key == key);
Expand Down
1 change: 1 addition & 0 deletions test/recurring_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void main() {
successPredicate,
successPredicate,
failurePredicate,
recurringFinishedPredicate,
]);
});
});
Expand Down

0 comments on commit d1cc6d0

Please sign in to comment.