Skip to content

Commit

Permalink
Merge pull request #79 from solid-daniilmarchenko/dependencies/upgrad…
Browse files Browse the repository at this point in the history
…e-throttling

Update throttling dependency
  • Loading branch information
solid-yuriiprykhodko committed Jan 30, 2024
2 parents 6a03c0c + c49dc3b commit 4035635
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 20 deletions.
13 changes: 4 additions & 9 deletions lib/implementations/debounce_lang_tool_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class DebounceLangToolService extends LanguageCheckService {
final LanguageCheckService baseService;

/// A debouncing used to debounce the API calls.
final Debouncing debouncing;
final Debouncing<Future<Result<List<Mistake>>?>> debouncing;

/// Creates a new instance of the [DebounceLangToolService] class.
DebounceLangToolService(
Expand All @@ -18,17 +18,12 @@ class DebounceLangToolService extends LanguageCheckService {
) : debouncing = Debouncing(duration: debouncingDuration);

@override
Future<Result<List<Mistake>>> findMistakes(String text) async {
final value =
await debouncing.debounce(() => baseService.findMistakes(text))
as Result<List<Mistake>>;

return value;
}
Future<Result<List<Mistake>>?> findMistakes(String text) async =>
await debouncing.debounce(() => baseService.findMistakes(text));

@override
Future<void> dispose() async {
await debouncing.close();
debouncing.close();
await baseService.dispose();
}
}
14 changes: 4 additions & 10 deletions lib/implementations/throttling_lang_tool_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ThrottlingLangToolService extends LanguageCheckService {
final LanguageCheckService baseService;

/// A throttling used to throttle the API calls.
final Throttling throttling;
final Throttling<Future<Result<List<Mistake>>?>> throttling;

/// Creates a new instance of the [ThrottlingLangToolService] class.
ThrottlingLangToolService(
Expand All @@ -19,18 +19,12 @@ class ThrottlingLangToolService extends LanguageCheckService {
) : throttling = Throttling(duration: throttlingDuration);

@override
Future<Result<List<Mistake>>?> findMistakes(String text) async {
final future = throttling.throttle(() => baseService.findMistakes(text))
as Future<Result<List<Mistake>>?>?;

if (future == null) return null;

return future.then((res) => res ?? Result.success(<Mistake>[].toList()));
}
Future<Result<List<Mistake>>?> findMistakes(String text) async =>
throttling.throttle(() => baseService.findMistakes(text));

@override
Future<void> dispose() async {
await throttling.close();
throttling.close();
await baseService.dispose();
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies:
flutter:
sdk: flutter
http: ^1.0.0
throttling: ^1.0.0
throttling: ^2.0.1

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 4035635

Please sign in to comment.