call method

Future<void> call([
  1. String? parentId
])

Invokes the onStartTyping callback and schedules a timer to invoke the onStopTyping callback.

This is meant to be called every time the user presses a key. The method will manage requests and timer as needed.

Implementation

Future<void> call([String? parentId]) async {
  final completer = _resetKeyStrokeCompleter();

  _cancelKeyStrokeTimer();

  _keyStrokeTimer = Timer(Duration(seconds: startTypingEventTimeout), () {
    _stopTyping(parentId).then((_) {
      if (completer.isCompleted) return;
      completer.complete();
    }).onError((error, stackTrace) {
      if (completer.isCompleted) return;
      completer.completeError(error!, stackTrace);
    });
  });

  // If the user is typing too long, it should call [onStartTyping] again.
  final now = DateTime.now();
  final lastTypingEvent = _lastTypingEvent;
  if (lastTypingEvent == null ||
      now.difference(lastTypingEvent).inMilliseconds >
          // startTypingResendInterval in milliseconds
          startTypingResendInterval * 1000) {
    _startTyping(parentId).onError((error, stackTrace) {
      _cancelKeyStrokeTimer();
      if (completer.isCompleted) return;
      completer.completeError(error!, stackTrace);
    });
  }

  return completer.future;
}