-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add EverCachedState class to represent the state of the cached …
…value
- Loading branch information
1 parent
31a36ca
commit 37de158
Showing
9 changed files
with
184 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// ignore_for_file: avoid_equals_and_hash_code_on_mutable_classes | ||
|
||
final class EverCachedState<T> { | ||
factory EverCachedState(T value) { | ||
return EverCachedState._( | ||
value: value, | ||
computedAt: DateTime.now(), | ||
); | ||
} | ||
|
||
factory EverCachedState.empty() { | ||
return EverCachedState._( | ||
computedAt: DateTime.now(), | ||
isEmpty: true, | ||
); | ||
} | ||
|
||
factory EverCachedState.placeholder(T value) { | ||
return EverCachedState._( | ||
value: value, | ||
computedAt: DateTime.now(), | ||
isPlaceholder: true, | ||
); | ||
} | ||
|
||
factory EverCachedState.disposed() { | ||
return const EverCachedState._( | ||
disposed: true, | ||
); | ||
} | ||
|
||
const EverCachedState._({ | ||
this.value, | ||
this.computedAt, | ||
this.isPlaceholder = false, | ||
this.isEmpty = false, | ||
this.disposed = false, | ||
}); | ||
|
||
final T? value; | ||
final DateTime? computedAt; | ||
final bool isPlaceholder; | ||
final bool isEmpty; | ||
final bool disposed; | ||
|
||
bool get hasValue { | ||
return !isEmpty && value != null; | ||
} | ||
|
||
bool get hasComputedValue { | ||
return hasValue && computedAt != null && !isPlaceholder; | ||
} | ||
|
||
@override | ||
int get hashCode => value.hashCode; | ||
|
||
@override | ||
bool operator ==(covariant EverCachedState<T> other) { | ||
if (identical(this, other)) return true; | ||
|
||
return other.value == value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
import 'ever_cached_state.dart'; | ||
|
||
abstract class IEverValue<T> { | ||
T get value; | ||
|
||
EverCachedState<T> get state; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.