Skip to content

Commit

Permalink
format: flutter format .
Browse files Browse the repository at this point in the history
  • Loading branch information
ali77gh committed Apr 1, 2023
1 parent 1b356d5 commit e7f41c9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class DependObservableAsyncSampleLayoutState

bmi = Telescope.dependsOnAsync(0, [height, weight], () async {
return await calculateBMI(height.value, weight.value);
}, isCalculating: loadingBMI,
enableCaching: true,
cacheExpireTime: const Duration(seconds: 10)
);
},
isCalculating: loadingBMI,
enableCaching: true,
cacheExpireTime: const Duration(seconds: 10));

showingText = Telescope.dependsOn([height, weight, bmi, loadingBMI], () {
var bmis = bmi.value.toString();
Expand Down
27 changes: 14 additions & 13 deletions lib/src/telescope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,43 +58,44 @@ class Telescope<T> {
bool enableCaching = false,
Duration? cacheExpireTime,
}) {

var hashmap = HashMap<int, T>();
var expireTimeMap = HashMap<int, DateTime>();

bool isExpired(int key){
if(cacheExpireTime == null) return false;
if(!expireTimeMap.containsKey(key)) return true;
return expireTimeMap[key]!.millisecondsSinceEpoch < DateTime.now().millisecondsSinceEpoch;
bool isExpired(int key) {
if (cacheExpireTime == null) return false;
if (!expireTimeMap.containsKey(key)) return true;
return expireTimeMap[key]!.millisecondsSinceEpoch <
DateTime.now().millisecondsSinceEpoch;
}

int getDependenciesHash(){
return dependencies.map((e) => e.value.hashCode).reduce((value,
element) => value * element);
int getDependenciesHash() {
return dependencies
.map((e) => e.value.hashCode)
.reduce((value, element) => value * element);
}

Future<T> cal() async {
if (!enableCaching) return await calculate();
var key = getDependenciesHash();
if (hashmap.containsKey(key) && !isExpired(key)){
if (hashmap.containsKey(key) && !isExpired(key)) {
return hashmap[key] as T;
}
return await calculate();
}

void calAndUpdate(){
void calAndUpdate() {
isCalculating?.value = true;
int dh = getDependenciesHash();
cal().then((value) {
if(enableCaching){
if (enableCaching) {
hashmap[dh] = value;
if(cacheExpireTime!=null){
if (cacheExpireTime != null) {
var expTime = DateTime.now().add(cacheExpireTime);
expireTimeMap[dh] = expTime;
}
}
int cdh = getDependenciesHash();
if(dh!=cdh) return;
if (dh != cdh) return;
holden = value;
isCalculating?.value = false;
notifyAll();
Expand Down

0 comments on commit e7f41c9

Please sign in to comment.