reloadAssets function
Reloads an asset (img, audi or video), forcing reload of asset URL.
assetsURLAndTag
A Map of URL as key and tag as value. Accepts '?' as tag (will be defined by URL extension).
Implementation
Future<bool> reloadAssets(Map<String, String> assetsURLAndTag,
{Duration? timeout}) async {
if (assetsURLAndTag.isEmpty) return false;
var doc = '<html><body>';
var docAssetsCount = 0;
for (var e in assetsURLAndTag.entries) {
var url = e.key;
if (isEmptyString(url, trim: true)) continue;
var tag = e.value;
if (isEmptyString(tag, trim: true) || tag == '?') {
var mimeType = MimeType.byExtension(url);
tag = mimeType?.htmlTag ?? 'img';
}
doc += '<$tag src="$url">';
docAssetsCount++;
}
doc += '</body></html>';
if (docAssetsCount == 0) return false;
var iFrame = IFrameElement()
..width = '10'
..height = '10'
..style.display = 'none';
iFrame.setAttribute('loading', 'eager');
var completer = Completer<bool>();
StreamSubscription<Event>? listen;
var reloadCounter = 0;
listen = iFrame.onLoad.listen((event) {
if (reloadCounter == 0) {
reloadCounter++;
Future.microtask(() => iFrame.remove());
} else if (reloadCounter == 1) {
listen?.cancel();
completer.complete(true);
Future.microtask(() => iFrame.remove());
}
});
// ignore: unsafe_html
iFrame.srcdoc = doc;
document.body!.append(iFrame);
if (timeout != null) {
Future.delayed(timeout, () {
if (!completer.isCompleted) {
listen?.cancel();
completer.complete(false);
Future.microtask(() => iFrame.remove());
}
});
}
return completer.future;
}