Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/cached_cqrs/.fvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"flutter": "3.27.3"
}
34 changes: 34 additions & 0 deletions packages/cached_cqrs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
build/

# FVM Version Cache
.fvm/
10 changes: 10 additions & 0 deletions packages/cached_cqrs/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: "c519ee916eaeb88923e67befb89c0f1dabfa83e6"
channel: "stable"

project_type: package
3 changes: 3 additions & 0 deletions packages/cached_cqrs/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dart.flutterSdkPath": ".fvm/versions/3.27.3"
}
3 changes: 3 additions & 0 deletions packages/cached_cqrs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.0.1

* TODO: Describe initial release.
1 change: 1 addition & 0 deletions packages/cached_cqrs/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO: Add your license here.
39 changes: 39 additions & 0 deletions packages/cached_cqrs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!--
This README describes the package. If you publish this package to pub.dev,
this README's contents appear on the landing page for your package.

For information about how to write a good package README, see the guide for
[writing package pages](https://dart.dev/tools/pub/writing-package-pages).

For general information about developing packages, see the Dart guide for
[creating packages](https://dart.dev/guides/libraries/create-packages)
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/to/develop-packages).
-->

TODO: Put a short description of the package here that helps potential users
know whether this package might be useful for them.

## Features

TODO: List what your package can do. Maybe include images, gifs, or videos.

## Getting started

TODO: List prerequisites and provide or point to information on how to
start using the package.

## Usage

TODO: Include short and useful examples for package users. Add longer examples
to `/example` folder.

```dart
const like = 'sample';
```

## Additional information

TODO: Tell users more about the package: where to find more information, how to
contribute to the package, how to file issues, what response they can expect
from the package authors, and more.
4 changes: 4 additions & 0 deletions packages/cached_cqrs/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include: package:flutter_lints/flutter.yaml

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
2 changes: 2 additions & 0 deletions packages/cached_cqrs/lib/cached_cqrs.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'src/cqrs.dart';
export 'src/queries.dart';
81 changes: 81 additions & 0 deletions packages/cached_cqrs/lib/src/cqrs.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import 'package:cached_cqrs/src/queries.dart';
import 'package:cached_cqrs/src/state.dart';
import 'package:cached_query/cached_query.dart' as cq;
import 'package:cqrs/cqrs.dart';

class CachedCqrs implements Cqrs {
CachedCqrs({
required Cqrs baseCqrs,
cq.CachedQuery? cache,
}) : _baseCqrs = baseCqrs,
_cache = cache;

final Cqrs _baseCqrs;
final cq.CachedQuery? _cache;

@override
void addMiddleware(CqrsMiddleware middleware) {
_baseCqrs.addMiddleware(middleware);
}

@override
Future<QueryResult<T>> get<T>(
Query<T> query, {
Map<String, String> headers = const {},
cq.QueryConfig? config,
T? initialData,
}) async {
final result = await CqrsQuery(
key: query,
cqrs: this,
config: config,
cache: _cache,
initialData: initialData,
).result.then(QueryState.new);

return switch (result.status) {
cq.QueryStatus.success => QuerySuccess(result.data as T),
cq.QueryStatus.error => QueryFailure(result.error!),
_ => throw StateError('Result is in invalid state: ${result.status}'),
};
}

void invalidateQuery(Query<Object?> query) {
cq.CachedQuery.instance.invalidateCache(key: query);
}

@override
Future<OperationResult<T>> perform<T>(
Operation<T> operation, {
Map<String, String> headers = const {},
Iterable<Query<Object?>>? queriesToInvalidate,
}) async {
final result = await _baseCqrs.perform(operation, headers: headers);
if (result.isSuccess) {
for (final query in queriesToInvalidate ?? Iterable.empty()) {
invalidateQuery(query);
}
}
return result;
}

@override
void removeMiddleware(CqrsMiddleware middleware) {
_baseCqrs.removeMiddleware(middleware);
}

@override
Future<CommandResult> run(
Command command, {
Map<String, String> headers = const {},
Iterable<Query<Object?>>? queriesToInvalidate,
}) async {
final result = await _baseCqrs.run(command, headers: headers);
if (result.isSuccess) {
for (final query in queriesToInvalidate ?? Iterable.empty()) {
invalidateQuery(query);
}
}
return result;
}
}
Loading