add method

void add(
  1. AliceLog log
)

Implementation

void add(AliceLog log) {
  int index;
  if (logs.isEmpty || !log.timestamp.isBefore(logs.last.timestamp)) {
    // Quick path as new logs are usually more recent.
    index = logs.length;
  } else {
    // Binary search to find the insertion index.
    var min = 0;
    var max = logs.length;
    while (min < max) {
      final mid = min + ((max - min) >> 1);
      final item = logs[mid];
      if (log.timestamp.isBefore(item.timestamp)) {
        max = mid;
      } else {
        min = mid + 1;
      }
    }
    assert(min == max, '');
    index = min;
  }

  var startIndex = 0;
  if (maximumSize != null && logs.length >= maximumSize!) {
    if (index == 0) return;
    startIndex = logs.length - maximumSize! + 1;
  }
  _logs.value = [
    ...logs.sublist(startIndex, index),
    log,
    ...logs.sublist(index, logs.length),
  ];
}