drainHsi method

void drainHsi()

Deliver every frame pending in the runtime's ring to the registered sink, oldest first. No-op outside buffered mode.

Safe — and cheap — to call from the host's own tick loop as well as from the periodic pump: an empty ring returns NULL (never an empty array) and costs one FFI call. Frames a host already received as a tick / tick_all return value are deduplicated downstream by hsi_id.

Implementation

void drainHsi() {
  if (!_hsiBufferedMode || _disposed) return;
  final sink = _hsiSink;
  final drain = _ffi.drainHsi;
  if (sink == null || drain == null) return;
  Pointer<Utf8> ptr;
  try {
    ptr = drain(_handle);
  } catch (_) {
    return;
  }
  if (ptr == nullptr) return; // nothing pending — the normal idle case
  final blob = _readFfiStringAndFree(ptr, _ffi.coreFreeString);
  if (blob == null || blob.isEmpty) return;
  final List<dynamic> frames;
  try {
    final decoded = jsonDecode(blob);
    if (decoded is! List) return;
    frames = decoded;
  } catch (_) {
    return;
  }
  for (final frame in frames) {
    // The delivery path is string-based end to end (the deduper reads
    // `meta.ids.hsi_id` off the raw text and `HSIState` keeps `rawJson`),
    // so hand each element on as its own JSON document.
    sink(jsonEncode(frame));
  }
  _reportDroppedHsiIfChanged();
}