declareBackgroundSubscriber method

Stream<Sample> declareBackgroundSubscriber(
  1. String keyExpr
)

Declares a background subscriber on the given keyExpr.

Returns a Stream of Samples. Unlike declareSubscriber, the background subscriber has no handle and cannot be explicitly closed. It lives until the session is closed, at which point the stream completes automatically.

Throws ZenohException if the key expression is invalid. Throws StateError if the session has been closed.

Implementation

Stream<Sample> declareBackgroundSubscriber(String keyExpr) {
  _ensureOpen();
  final (receivePort, controller) = Subscriber.createSampleChannel();
  final loanedSession =
      bindings.zd_session_loan(_ptr.cast()) as Pointer<Void>;
  final keyExprNative = keyExpr.toNativeUtf8();

  try {
    final rc = bindings.zd_declare_background_subscriber(
      loanedSession.cast(),
      keyExprNative.cast(),
      receivePort.sendPort.nativePort,
    );

    if (rc != 0) {
      receivePort.close();
      controller.close();
      throw ZenohException('Failed to declare background subscriber', rc);
    }
  } finally {
    calloc.free(keyExprNative);
  }

  return controller.stream;
}