concurrentComplete method

void concurrentComplete(
  1. HlsSegment hlsSegment,
  2. Map<String, String> headers, {
  3. DownloadStatus? status,
})

Marks a segment as completed and triggers the next download if available.

Implementation

void concurrentComplete(
  HlsSegment hlsSegment,
  Map<String, String> headers, {
  DownloadStatus? status,
}) {
  int index = _list.indexWhere((e) => e.url == hlsSegment.url);
  if (index == -1) return;
  _list[index].status = status ?? DownloadStatus.COMPLETED;
  HlsSegment? latest = _list.where((e) => e.url == _latestUrl).firstOrNull;
  if (latest != null) {
    List<HlsSegment> list = _list.where((e) => e.key == latest.key).toList();
    int index = list.indexWhere((e) => e.url == latest.url);
    if (index != -1 && index + 1 < list.length) {
      concurrentLoop(list[index + 1], headers);
      return;
    }
  }
  Set<String?> keys = _list.map((e) => e.key).toSet();
  String? key = keys.elementAt(Random().nextInt(keys.length));
  HlsSegment? idleSegment = _list
      .where((e) => e.key == key)
      .where((e) => e.status == DownloadStatus.IDLE)
      .firstOrNull;
  if (idleSegment == null) {
    _list.removeWhere((e) => e.key == key);
    concurrentComplete(hlsSegment, headers);
    return;
  }
  concurrentLoop(idleSegment, headers);
}