first implementation finished
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:kagi_bang_bang/features/kagi/data/services/autosuggest.dart';
|
||||
import 'package:kagi_bang_bang/utils/lru_cache.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
|
||||
part 'autosuggest.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
class AutosuggestRepository extends _$AutosuggestRepository {
|
||||
final LRUCache<String, List<String>> _cache;
|
||||
|
||||
late StreamController<String> _queryStreamController;
|
||||
|
||||
AutosuggestRepository() : _cache = LRUCache(100);
|
||||
|
||||
void addQuery(String query) {
|
||||
_queryStreamController.add(query);
|
||||
}
|
||||
|
||||
@override
|
||||
Raw<Stream<List<String>>> build() {
|
||||
_queryStreamController = StreamController();
|
||||
ref.onDispose(() async {
|
||||
await _queryStreamController.close();
|
||||
});
|
||||
|
||||
return _queryStreamController.stream
|
||||
.sampleTime(const Duration(milliseconds: 100))
|
||||
.switchMap<List<String>>(
|
||||
(query) {
|
||||
if (query.isEmpty) {
|
||||
return Stream.value([]);
|
||||
}
|
||||
|
||||
final cached = _cache.get(query);
|
||||
if (cached != null) {
|
||||
return Stream.value(cached);
|
||||
}
|
||||
|
||||
return ref
|
||||
.read(kagiAutosuggestServiceProvider.notifier)
|
||||
// ignore: discarded_futures
|
||||
.getSuggestions(query)
|
||||
// ignore: discarded_futures
|
||||
.then((result) {
|
||||
result.onSuccess((result) {
|
||||
_cache.set(query, result);
|
||||
});
|
||||
|
||||
return result.value;
|
||||
}).asStream();
|
||||
},
|
||||
).asBroadcastStream();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'autosuggest.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$autosuggestRepositoryHash() =>
|
||||
r'48d6bf45d648871f8570edfe1e0b394019084695';
|
||||
|
||||
/// See also [AutosuggestRepository].
|
||||
@ProviderFor(AutosuggestRepository)
|
||||
final autosuggestRepositoryProvider =
|
||||
NotifierProvider<AutosuggestRepository, Raw<Stream<List<String>>>>.internal(
|
||||
AutosuggestRepository.new,
|
||||
name: r'autosuggestRepositoryProvider',
|
||||
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$autosuggestRepositoryHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef _$AutosuggestRepository = Notifier<Raw<Stream<List<String>>>>;
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member
|
||||
Reference in New Issue
Block a user