Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: setup workflows, lints and dependencies #3

Merged
merged 9 commits into from
Jul 15, 2024
Merged
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
26 changes: 26 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: /
schedule:
interval: monthly
groups:
github-actions:
patterns:
- "*"
- package-ecosystem: pub
directory: /example
schedule:
interval: monthly
groups:
example-pub:
patterns:
- "*"
- package-ecosystem: pub
directory: /
schedule:
interval: monthly
groups:
root-pub:
patterns:
- "*"
7 changes: 0 additions & 7 deletions .github/no-response.yml

This file was deleted.

31 changes: 31 additions & 0 deletions .github/workflows/cached_value.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: cached_value

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

on:
pull_request:
paths:
- ".github/workflows/cached_value.yaml"
- "lib/**"
- "test/**"
- "pubspec.yaml"
push:
branches:
- main
paths:
- ".github/workflows/cached_value.yaml"
- "lib/**"
- "test/**"
- "pubspec.yaml"

jobs:
semantic-pull-request:
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/semantic_pull_request.yml@v1

build:
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/dart_package.yml@v1
with:
coverage_excludes: '**/*.g.dart'
dart_sdk: 'stable'
16 changes: 0 additions & 16 deletions .github/workflows/test.yml

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,5 @@ build/
!**/ios/**/default.mode2v3
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3

pubspec.lock
20 changes: 9 additions & 11 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
include: package:effective_dart/analysis_options.1.3.0.yaml
renancaraujo marked this conversation as resolved.
Show resolved Hide resolved

include: package:very_good_analysis/analysis_options.yaml
linter:
rules:
- annotate_overrides
- await_only_futures
- camel_case_types
- cancel_subscriptions
- close_sinks
- comment_references
- constant_identifier_names
- control_flow_in_finally
- empty_statements
public_member_api_docs: true
one_member_abstracts: false


analyzer:
exclude:
- "**/*.g.dart"
- "lib/src/version.dart"
18 changes: 10 additions & 8 deletions example/example.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// ignore_for_file: avoid_print

import 'package:cached_value/cached_value.dart';

int factorial(int n) {
if (n < 0) throw ('Negative numbers are not allowed.');
if (n < 0) throw Exception('Negative numbers are not allowed.');
return n <= 1 ? 1 : n * factorial(n - 1);
}

Expand All @@ -11,9 +13,9 @@ void main() {
}

void withDependency() {
print("with dependency");
print('with dependency');

int originalValue = 1;
var originalValue = 1;
final factorialCache = CachedValue(
() => factorial(originalValue),
).withDependency(
Expand All @@ -29,21 +31,21 @@ void withDependency() {
print(factorialCache.value); // 720
}

void withTimeToLive() async {
print("with TTL:");
Future<void> withTimeToLive() async {
print('with TTL:');

int originalValue = 1;
var originalValue = 1;
final factorialCache = CachedValue(
() => factorial(originalValue),
).withTimeToLive(
lifetime: Duration(seconds: 3),
lifetime: const Duration(seconds: 3),
);

originalValue = 6;

print(factorialCache.value); // 1

await Future.delayed(Duration(seconds: 3));
await Future<void>.delayed(const Duration(seconds: 3));

print(factorialCache.value); // 720
}
26 changes: 0 additions & 26 deletions example/pubspec.lock

This file was deleted.

6 changes: 0 additions & 6 deletions format.sh

This file was deleted.

96 changes: 38 additions & 58 deletions lib/src/cached_value.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dependent_cached_value.dart';
import 'simple_cached_value.dart';
import 'single_child_cached_value.dart';
import 'time_to_live_cached_value.dart';
import 'package:cached_value/src/dependent_cached_value.dart';
import 'package:cached_value/src/simple_cached_value.dart';
import 'package:cached_value/src/single_child_cached_value.dart';
import 'package:cached_value/src/time_to_live_cached_value.dart';

/// A signature for functions that computes the value to be cached.
///
Expand All @@ -28,6 +28,40 @@ typedef ComputeCacheCallback<CacheContentType> = CacheContentType Function();
/// - [TimeToLiveCachedValue] creates a cache that is invalidated after
/// some given [Duration].
abstract class CachedValue<CacheContentType> {
/// Creates a [CachedValue] that is only manually invalidated.
///
/// The implementation type for the returned value is [SimpleCachedValue].
///
/// {@macro simple_cache}
///
/// Usage example:
/// ```dart
/// int factorial(int n) {
/// if (n < 0) throw ('Negative numbers are not allowed.');
/// return n <= 1 ? 1 : n * factorial(n - 1);
/// }
///
/// int originalValue =1;
/// final factorialCache = CachedValue(() => factorial(originalValue));
/// print(factorialCache.value); // 1
///
/// originalValue = 6;
///
/// print(factorialCache.value); // 1
/// factorialCache.invalidate();
///
/// print(factorialCache.value); // 720
/// ```
///
/// See also:
/// - [DependentCachedValue] creates a cache that is updated if a
/// dependency changes.
/// - [TimeToLiveCachedValue] creates a cache that is invalidated after
/// some given [Duration].
factory CachedValue(ComputeCacheCallback<CacheContentType> callback) {
return SimpleCachedValue<CacheContentType>(callback);
}

/// Access the current cache value.
///
/// If the cache is considered invalid, calls [refresh].
Expand Down Expand Up @@ -74,58 +108,4 @@ abstract class CachedValue<CacheContentType> {
/// The returned value should be the new cache value.
/// {@endtemplate}
CacheContentType refresh();

/// Creates a [CachedValue] that is only manually invalidated.
///
/// The implementation type for the returned value is [SimpleCachedValue].
///
/// {@macro simple_cache}
///
/// Usage example:
/// ```dart
/// int factorial(int n) {
/// if (n < 0) throw ('Negative numbers are not allowed.');
/// return n <= 1 ? 1 : n * factorial(n - 1);
/// }
///
/// int originalValue =1;
/// final factorialCache = CachedValue(() => factorial(originalValue));
/// print(factorialCache.value); // 1
///
/// originalValue = 6;
///
/// print(factorialCache.value); // 1
/// factorialCache.invalidate();
///
/// print(factorialCache.value); // 720
/// ```
///
/// See also:
/// - [DependentCachedValue] creates a cache that is updated if a
/// dependency changes.
/// - [TimeToLiveCachedValue] creates a cache that is invalidated after
/// some given [Duration].
factory CachedValue(ComputeCacheCallback<CacheContentType> callback) {
return SimpleCachedValue<CacheContentType>(callback);
}

/// Creates a [CachedValue] that is only manually invalidated.
///
/// Use [new CachedValue] instead.
@Deprecated('Use the constructor instead')
static SimpleCachedValue<CacheContentType> simple<CacheContentType>(
ComputeCacheCallback<CacheContentType> callback) {
return SimpleCachedValue<CacheContentType>(callback);
}

/// Creates a [CachedValue] that its validity is defined by a dependency.
///
/// Use `CachedValue.withDependency` instead.
@Deprecated('Use "withDependency" instead')
static DependentCachedValue<A, B> dependent<A, B>({
required ComputeCacheDependency<B> on,
required ComputeCacheCallback<A> compute,
}) {
return CachedValue<A>(compute).withDependency<B>(on);
}
}
26 changes: 17 additions & 9 deletions lib/src/dependent_cached_value.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import 'package:cached_value/src/cached_value.dart';
import 'package:cached_value/src/single_child_cached_value.dart';
import 'package:collection/collection.dart';

import 'cached_value.dart';
import 'single_child_cached_value.dart';

/// A signature for functions that provides dependency of a
/// [DependentCachedValue].
///
Expand All @@ -28,6 +27,10 @@ typedef ComputeCacheDependency<DependencyType> = DependencyType Function();
/// It can be created via `CachedValue.withDependency`
class DependentCachedValue<CacheContentType, DependencyType>
extends SingleChildCachedValue<CacheContentType> {
DependentCachedValue._(
CachedValue<CacheContentType> child,
this._getDependency,
) : super(child);
late DependencyType _dependencyCache = _getDependency();

@override
Expand All @@ -44,10 +47,6 @@ class DependentCachedValue<CacheContentType, DependencyType>

final ComputeCacheDependency<DependencyType> _getDependency;

DependentCachedValue._(
CachedValue<CacheContentType> child, this._getDependency)
: super(child);

/// {@template dependent_refresh}
/// Calls refresh on its child and updates the local cache of dependency.
/// {@endtemplate}
Expand Down Expand Up @@ -101,6 +100,15 @@ extension DependentExtension<CacheContentType>
DependentCachedValue<CacheContentType, DependencyType>
withDependency<DependencyType>(
ComputeCacheDependency<DependencyType> on,
) =>
DependentCachedValue._(this, on);
) {
DependencyType getDependency() {
final dependencyValue = on();
if (dependencyValue is Iterable && dependencyValue is! List) {
return dependencyValue.toList() as DependencyType;
}
return dependencyValue;
}

return DependentCachedValue._(this, getDependency);
}
}
Loading