setHsiCallback method

void setHsiCallback(
  1. void onHsi(
    1. String json
    ), {
  2. bool preferBuffered = true,
})

Register a callback for real-time HSI state updates.

The onHsi function is called on each HSI frame (typically 1Hz during an active session) with the raw JSON string.

Only one delivery path can be active. Call clearHsiCallback to unregister.

Prefers buffered (pull-based) delivery when the runtime supports it (≥ 0.31.1): init_hsi_buffered retires any native callback, waits for an in-flight dispatch, and from then on the runtime queues frames in a ring this bridge drains on hsiDrainInterval. Falls back to the legacy push callback on an older runtime — same behaviour as before, including its hot-restart crash exposure, until the vendored library is updated.

Implementation

void setHsiCallback(
  void Function(String hsiJson) onHsi, {
  bool preferBuffered = true,
}) {
  clearHsiCallback();
  if (preferBuffered && _initHsiBuffered(onHsi)) return;

  void nativeCallback(Pointer<Utf8> jsonPtr, Pointer<Void> _) {
    if (jsonPtr == nullptr) return;
    final text = _readFfiStringAndFree(jsonPtr, _ffi.coreFreeString);
    if (text != null) onHsi(text);
  }

  _hsiCallable =
      NativeCallable<Void Function(Pointer<Utf8>, Pointer<Void>)>.listener(
        nativeCallback,
      );
  _ffi.setHsiCallback(_handle, _hsiCallable!.nativeFunction, nullptr);
}