onSend method

  1. @override
Msg? onSend(
  1. Msg msg
)
override

Called for each message before it is queued.

Return the same message to keep it, a modified message to transform it, or null to drop it.

Implementation

@override
Msg? onSend(Msg msg) {
  final innerResult = inner?.onSend(msg);
  if (inner != null && innerResult == null) return null;

  final forwarded = innerResult ?? msg;
  if (forwarded is _KeyChordTimeoutMsg) {
    if (_active?.sessionId == forwarded.sessionId) {
      final active = _active!;
      _clearActive();
      return KeyChordCancelledMsg(prefix: active.prefix, timedOut: true);
    }
    return null;
  }

  if (forwarded is! KeyMsg) return forwarded;

  final key = forwarded.key;
  final active = _active;

  if (active != null) {
    final resolved = _matchContinuation(active, key);
    if (resolved != null) {
      _clearActive();
      return KeyChordResolvedMsg(
        id: resolved.id,
        prefix: active.prefix,
        key: key,
      );
    }

    if (_matchesAnyPrefix(key)) {
      final previousPrefix = active.prefix;
      _clearActive();
      _startChord(key);
      return BatchMsg([
        KeyChordCancelledMsg(prefix: previousPrefix, key: key),
        KeyChordPrefixMsg(key),
      ]);
    }

    final cancelled = KeyChordCancelledMsg(prefix: active.prefix, key: key);
    _clearActive();
    return BatchMsg([cancelled, forwarded]);
  }

  if (_matchesAnyPrefix(key)) {
    _startChord(key);
    return KeyChordPrefixMsg(key);
  }

  return forwarded;
}