Fix DartVM not exiting even when closing the youtube client.

Closes #61
This commit is contained in:
Mattia 2020-09-03 20:44:35 +02:00
parent a56ca657c4
commit 1a5a03cabb
1 changed files with 39 additions and 8 deletions

View File

@ -112,20 +112,51 @@ class PlayerSource {
///
static Future<PlayerSource> get(
YoutubeHttpClient httpClient, String url) async {
if (_cache[url] == null) {
Timer(const Duration(minutes: 10), () {
_cache[url] = null;
});
return _cache[url] = await retry(() async {
if (_cache[url]?.expired ?? true) {
var val = await retry(() async {
var raw = await httpClient.getString(url);
return PlayerSource.parse(raw);
});
if (_cache[url] == null) {
_cache[url] = _CachedValue(val);
} else {
_cache[url].update(val);
}
}
return _cache[url];
return _cache[url].value;
}
static final Map<String, PlayerSource> _cache = {};
static final Map<String, _CachedValue<PlayerSource>> _cache = {};
}
class _CachedValue<T> {
T _value;
int expireTime;
final int cacheTime;
T get value {
if (expired) {
throw StateError('Value $value is expired!');
}
return _value;
}
bool get expired {
final now = DateTime.now().millisecondsSinceEpoch;
return now > expireTime;
}
set value(T other) => _value = other;
_CachedValue(this._value, [this.expireTime, this.cacheTime = 600000]) {
expireTime ??= DateTime.now().millisecondsSinceEpoch + cacheTime;
}
void update(T newValue) {
var now = DateTime.now().millisecondsSinceEpoch;
expireTime = now + cacheTime;
value = newValue;
}
}
extension on String {