Skip to content

Commit

Permalink
feature: fx debounceTime.
Browse files Browse the repository at this point in the history
  • Loading branch information
ali77gh committed Apr 3, 2023
1 parent 8ce65eb commit b826378
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class DependObservableAsyncSampleLayoutState
},
isCalculating: loadingBMI,
enableCaching: true,
cacheExpireTime: const Duration(seconds: 10));
cacheExpireTime: const Duration(seconds: 10),
debounceTime: const Duration(milliseconds: 500));

showingText = Telescope.dependsOn([height, weight, bmi, loadingBMI], () {
var bmis = bmi.value.toString();
Expand Down
31 changes: 19 additions & 12 deletions lib/src/telescope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Telescope<T> {
Telescope<bool>? isCalculating,
bool enableCaching = false,
Duration? cacheExpireTime,
Duration debounceTime = Duration.zero,
}) {
var hashmap = HashMap<int, T>();
var expireTimeMap = HashMap<int, DateTime>();
Expand Down Expand Up @@ -86,19 +87,25 @@ class Telescope<T> {
void calAndUpdate() {
isCalculating?.value = true;
int dh = getDependenciesHash();
cal().then((value) {
if (enableCaching) {
hashmap[dh] = value;
if (cacheExpireTime != null) {
var expTime = DateTime.now().add(cacheExpireTime);
expireTimeMap[dh] = expTime;
}
Future.delayed(debounceTime, () {
if (debounceTime != Duration.zero) {
int cdh = getDependenciesHash();
if (dh != cdh) return;
}
int cdh = getDependenciesHash();
if (dh != cdh) return;
holden = value;
isCalculating?.value = false;
notifyAll();
cal().then((value) {
if (enableCaching) {
hashmap[dh] = value;
if (cacheExpireTime != null) {
var expTime = DateTime.now().add(cacheExpireTime);
expireTimeMap[dh] = expTime;
}
}
int cdh = getDependenciesHash();
if (dh != cdh) return;
holden = value;
isCalculating?.value = false;
notifyAll();
});
});
}

Expand Down

0 comments on commit b826378

Please sign in to comment.