Skip to content

Commit

Permalink
basic cache (not tested)
Browse files Browse the repository at this point in the history
  • Loading branch information
ali77gh committed Mar 30, 2023
1 parent e049bf8 commit 03f1683
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions lib/src/telescope.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:collection';

import 'package:flutter/material.dart';
import 'package:telescope/src/fs/save_and_load.dart';
import 'package:telescope/src/type_check.dart';
Expand Down Expand Up @@ -50,17 +52,42 @@ class Telescope<T> {
Future<T> Function() calculate, {
this.iWillCallNotifyAll = false,
Telescope<bool>? isCalculating,
bool cache = false,
Duration cacheTime = Duration.zero, // TODO
}) {

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

HashMap<int, T>? hashmap;
if(cache){
hashmap = HashMap<int, T>();
}
Future<T> cal() async {
if (!cache) return await calculate();
var key = getDependenciesHash();
if (hashmap!.containsKey(key)){
return hashmap[key] as T;
}
return await calculate();
}

isCalculating?.value = true;
calculate().then((value) {
int dh = getDependenciesHash();
cal().then((value) {
hashmap?[dh] = value;
holden = value;
isCalculating?.value = false;
notifyAll();
});
for (var o in dependencies) {
o.subscribe(() {
isCalculating?.value = true;
calculate().then((value) {
int dh = getDependenciesHash();
cal().then((value) {
hashmap?[dh] = value;
holden = value;
isCalculating?.value = false;
notifyAll();
Expand Down

0 comments on commit 03f1683

Please sign in to comment.