sendProgressNotification method

void sendProgressNotification(
  1. String sessionId,
  2. Object progressToken,
  3. double progress, [
  4. double? total,
  5. String? message,
])

Send progress notification to the client

Progress (basic/utilities/progress): only for a token of a request still in flight on sessionId, only with a value greater than the last, and no more often than progressMinInterval (the final value, reaching total, always goes). Anything else is dropped.

Implementation

void sendProgressNotification(
  String sessionId,
  Object progressToken,
  double progress, [
  double? total,
  String? message,
]) {
  if (!isConnected) return;
  final op =
      _pendingOperations.values
          .where(
            (o) =>
                o.sessionId == sessionId &&
                o.progressToken != null &&
                '${o.progressToken}' == '$progressToken',
          )
          .firstOrNull;
  if (op == null) {
    _logger.debug(
      'Progress for $progressToken dropped: no request in flight with that token',
    );
    return;
  }
  if (op.isCancelled) return; // nothing more for a cancelled request
  final last = op.lastProgress;
  if (last != null && progress <= last) {
    _logger.warning(
      'Progress for $progressToken dropped: $progress does not increase on $last',
    );
    return;
  }
  final now = DateTime.now();
  final finalValue = total != null && progress >= total;
  final lastAt = op.lastProgressAt;
  if (!finalValue &&
      lastAt != null &&
      now.difference(lastAt) < progressMinInterval) {
    return;
  }
  op.lastProgress = progress;
  op.lastProgressAt = now;
  progressToken = op.progressToken!;

  final params = ProgressNotificationParams(
    progressToken: progressToken,
    progress: progress,
    total: total,
    message: message,
  );

  _sendNotification(sessionId, 'notifications/progress', params.toJson());
}