streambox_core 1.4.2
streambox_core: ^1.4.2 copied to clipboard
Core for stream-based caching and data strategy architecture.
/// Example usage of streambox_core
///
/// ```dart
/// // π data layer
/// // response from backend
/// class ItemResponse {
/// // fromJson...
/// // toJson...
/// }
///
/// @RestApi
/// abstract interface class ExampleApiInterface {
/// factory ExampleApiInterface(Dio dio) = _ExampleApiInterface;
///
/// @GET('items')
/// Future`<ItemResponse>` fetchItems({
/// @Query('page') required int page,
/// @Query('size') required int size,
/// });
/// }
///
/// final class ExampleDataSource
/// extends BaseDataSource`<FetchParams, ItemResponse>` {
/// ExampleDataSource({
/// required ExampleApiInterface api,
/// required super.cacheStrategy,
/// }) : _api = api;
///
/// final ExampleApiInterface _api;
///
/// @override
/// Future`<ItemResponse>` request(FetchParams? params) {
/// return _api.fetchItems(page: params!.page, size: params.size);
/// }
/// }
///
/// final class ExampleCache extends BaseKeyValueCache`<ItemResponse>` {
/// const ExampleCache({required super.store});
///
/// @override
/// String get keyPrefix => 'items';
///
/// @override
/// ItemResponse deserialize(String source) =>
/// ItemResponse.fromJson(decodeAsMap(source));
///
/// @override
/// String serialize(ItemResponse value) => encode(value.toJson());
/// }
///
/// final class ExampleRepoImpl
/// extends SingleSourceRepo`<FetchParams, ItemResponse, ExampleEntity>`
/// implements ExampleRepo {
/// ExampleRepoImpl({required super.dataSource});
///
/// @override
/// ExampleEntity map(FetchParams? params, ItemResponse value) =>
/// ExampleMapper(value).toEntity();
/// }
///
/// // π domain layer
/// class FetchParams implements RequestParams {
/// FetchParams({required this.page, required this.size});
///
/// final int page;
/// final int size;
///
/// @override
/// String get cacheKey => 'cacheKey: $page-$size';
/// }
///
/// class ExampleEntity {
/// //
/// }
///
/// abstract interface class ExampleRepo
/// implements Repo`<FetchParams, ExampleEntity>` {}
///
/// // π di module
/// final exampleRepo = ExampleRepoImpl(
/// dataSource: ExampleDataSource(
/// api: ExampleApiInterface(dio),
/// cacheStrategy: CacheThenRefreshStrategy(
/// cache: ExampleCache(store: MemoryStoreAdapter()),
/// ),
/// ),
/// );
/// ```
void main() {}