Future _enqueuePayload()

in lib/src/usage_impl.dart [253:294]


  Future<void> _enqueuePayload(
    String hitType,
    Map<String, dynamic> args,
  ) async {
    if (!enabled) return;
    // TODO(sigurdm): Really all the 'send' methods should not return Futures
    // there is not much point in waiting for it. Only [waitForLastPing].
    final completer = Completer<void>();
    final eventArgs = <String, String>{
      ...args,
      ..._variableMap,
      'v': '1', // protocol version
      'tid': trackingId,
      'cid': clientId,
      't': hitType,
    };

    _sendController.add(eventArgs);
    _batchedEvents.add(postHandler.encodeHit(eventArgs));

    // If [_batchingDelay] is null we don't do batching.
    // TODO(sigurdm): reconsider this.
    final batchingDelay = _batchingDelay;
    if (batchingDelay == null) {
      _trySendBatches(completer);
    } else {
      // First check if we have a full batch - if so, send them immediately.
      if (_batchedEvents.length >= _maxHitsPerBatch ||
          _batchedEvents.fold<int>(0, (s, e) => s + e.length) >=
              _maxBytesPerBatch) {
        _trySendBatches(completer);
      } else if (!_isSendingScheduled) {
        _isSendingScheduled = true;
        // ignore: unawaited_futures
        Future.delayed(batchingDelay).then((value) {
          _isSendingScheduled = false;
          _trySendBatches(completer);
        });
      }
    }
    return completer.future;
  }