adjustSession function

Session adjustSession(
  1. Session session, {
  2. String? nodeId,
  3. required CountAdjustment adjustment,
})

Returns a copy of session with an incremental adjustment applied.

When nodeId is given the node's file counters are adjusted too (words and characters only — per-file history has no no-spaces column), keeping its original_* (session start) values untouched. Totals are always adjusted.

Implementation

Session adjustSession(
  Session session, {
  String? nodeId,
  required CountAdjustment adjustment,
}) {
  SessionTotal total = adjustSessionTotal(session.metadata.total, adjustment);

  Map<String, SessionFileCounters> files = session.metadata.files;
  if (nodeId != null) {
    final SessionFileCounters? current = files[nodeId];
    final SessionFileCounters updated = SessionFileCounters(
      originalWords: current?.originalWords ?? 0,
      originalCharacters: current?.originalCharacters ?? 0,
      words: _atLeastZero((current?.words ?? 0) + adjustment.words),
      characters:
          _atLeastZero((current?.characters ?? 0) + adjustment.characters),
    );
    files = <String, SessionFileCounters>{...files, nodeId: updated};
  }

  return session.copyWith(
    metadata: SessionMetadata(files: files, total: total),
  );
}