nbr 1.0.3
nbr: ^1.0.3 copied to clipboard
Generic implementation of the network-bound-resource algorithm in Dart
Network Bound Resource #
Generic implementation of the network-bound-resource algorithm in Dart.
Usage #
Your repository class should extend [NetworkBoundResource].
// For example ...
class MyRepositoryImpl extends NetworkBoundResource implements IMyRepository {}
Then, you can use the fetch()
method which returns a stream of Resource
objects. For example
Stream<Resource<MapEntry<String, List<String>>>> fetchPackageDependencies(
String package,
) async* {
yield* fetch<MapEntry<String, List<String>>, List<String>>(
fetchFromAPI: () async => await _fetchDependencies(package),
loadFromDB: () {
final versions = _mockDB[package];
return versions != null ? MapEntry(package, versions) : null;
},
storeToDB: (entry) => _persistDependencies(entry.key, entry.value),
shouldFetch: (entry) => entry == null || !_mockDB.containsKey(package),
mapDTOToEntity: (versions) => MapEntry(package, versions),
);
}