-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathremovable.dart
36 lines (30 loc) · 1002 Bytes
/
removable.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import 'dart:async';
import 'dart:math';
import 'package:fquery/fquery.dart';
mixin Removable {
Duration? _cacheDuration;
Timer? _garbageCollectionTimer;
/// Sets the cache duration
/// Max cacheDuration given by any observer is used
/// Reschedules the garbage collection timer
void setCacheDuration(Duration cacheDuration) {
_cacheDuration = Duration(
milliseconds: max(
(_cacheDuration ?? Duration.zero).inMilliseconds,
cacheDuration.inMilliseconds,
));
scheduleGarbageCollection();
}
/// This is called when garbage collection timer fires
// Defined by the child class
void onGarbageCollection() {}
void scheduleGarbageCollection() {
_garbageCollectionTimer?.cancel();
final duration = _cacheDuration ?? DefaultQueryOptions().cacheDuration;
_garbageCollectionTimer = Timer(duration, onGarbageCollection);
}
void cancelGarbageCollection() {
_garbageCollectionTimer?.cancel();
_garbageCollectionTimer = null;
}
}