diff --git a/lib/src/features/movies/presentation/movies/movies_search_query_notifier.dart b/lib/src/features/movies/presentation/movies/movies_search_query_notifier.dart index b8c2099..278f57b 100644 --- a/lib/src/features/movies/presentation/movies/movies_search_query_notifier.dart +++ b/lib/src/features/movies/presentation/movies/movies_search_query_notifier.dart @@ -7,39 +7,26 @@ part 'movies_search_query_notifier.g.dart'; @riverpod class MoviesSearchQueryNotifier extends _$MoviesSearchQueryNotifier { /// Used to debounce the input queries - final _searchQueryController = StreamController.broadcast(); Timer? _debounceTimer; - late final StreamSubscription _subscription; @override String build() { - // Listen to the stream of input queries - _subscription = _searchQueryController.stream.listen((query) { - // Cancel existing timer if there is one - _debounceTimer?.cancel(); - // Set a new timer to debounce the query - _debounceTimer = Timer(const Duration(milliseconds: 500), () { - _updateState(query); - }); - }); - // don't forget to close the StreamController and cancel the subscriptions on dispose ref.onDispose(() { - _searchQueryController.close(); - _subscription.cancel(); _debounceTimer?.cancel(); }); - // by default, return an empty query return ''; } - void _updateState(String query) { - // only update the state once the query has been debounced - state = query; - } - void setQuery(String query) { - _searchQueryController.sink.add(query); + // Cancel the timer if it is active + if (_debounceTimer != null) { + _debounceTimer!.cancel(); + } + _debounceTimer = Timer(const Duration(milliseconds: 500), () { + // only update the state once the query has been debounced + state = query; + }); } } diff --git a/lib/src/features/movies/presentation/movies/movies_search_query_notifier.g.dart b/lib/src/features/movies/presentation/movies/movies_search_query_notifier.g.dart index be16f87..2e93fc2 100644 --- a/lib/src/features/movies/presentation/movies/movies_search_query_notifier.g.dart +++ b/lib/src/features/movies/presentation/movies/movies_search_query_notifier.g.dart @@ -7,7 +7,7 @@ part of 'movies_search_query_notifier.dart'; // ************************************************************************** String _$moviesSearchQueryNotifierHash() => - r'e5feb3e8f1c81a63519db4e67c615c169b380710'; + r'0940cd9749f13da2d9122b8210ea3eb98a9de90c'; /// A notifier class to keep track of the search query (with debouncing) ///