Skip to content

Commit

Permalink
Add standartProLong
Browse files Browse the repository at this point in the history
  • Loading branch information
petrnymsa committed Jun 18, 2024
1 parent c279316 commit 7f5a98b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/netglade_utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

## 2.4.0
- Add `standartProLong` future extension.

## 2.3.0
- Add more String extensions.

Expand Down
17 changes: 15 additions & 2 deletions packages/netglade_utils/lib/src/extensions/future_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ import 'package:clock/clock.dart';
typedef OnTakingTooLongCallback = void Function();

extension FutureExtensions<T> on Future<T> {
static const standardProlongTimeMs = 300;

// ignore: comment_references, see https://github.com/dart-lang/linter/issues/2079
/// If [this] future taking longer than [duration] to execute - [callback] is called.
Future<T> onTakingTooLong(Duration duration, OnTakingTooLongCallback callback) async {
Future<T> onTakingTooLong(
Duration duration,
OnTakingTooLongCallback callback,
) async {
final timer = Timer(duration, callback);

try {
Expand All @@ -33,9 +38,17 @@ extension FutureExtensions<T> on Future<T> {
final diff = DateTime.now().millisecondsSinceEpoch - started;

if (diff < duration.inMilliseconds) {
await Future<void>.delayed(Duration(milliseconds: duration.inMilliseconds - diff));
await Future<void>.delayed(
Duration(milliseconds: duration.inMilliseconds - diff),
);
}

return result;
}

Future<T> standardProlong([int? milliseconds]) {
return withMinimalLoadTime(
Duration(milliseconds: milliseconds ?? standardProlongTimeMs),
);
}
}
2 changes: 1 addition & 1 deletion packages/netglade_utils/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: netglade_utils
version: 2.3.0
version: 2.4.0
description: Dart utils used internally at netglade.
repository: https://github.com/netglade/flutter_core/tree/main/packages/netglade_utils
issue_tracker: https://github.com/netglade/flutter_core/issues
Expand Down

0 comments on commit 7f5a98b

Please sign in to comment.