declare static method

Queryable declare(
  1. Pointer<Void> loanedSession,
  2. String keyExprStr, {
  3. bool complete = false,
})

Creates a queryable on the given session and key expression.

This is called internally by Session.declareQueryable.

Implementation

static Queryable declare(
  Pointer<Void> loanedSession,
  String keyExprStr, {
  bool complete = false,
}) {
  final size = bindings.zd_queryable_sizeof();
  final Pointer<Void> ptr = calloc.allocate(size);

  final receivePort = ReceivePort();
  final controller = StreamController<Query>();

  receivePort.listen((dynamic message) {
    if (message is List) {
      final queryPtr = message[0] as int;
      final keyExpr = message[1] as String;
      final params = message[2] as String;
      final payloadBytes = message[3] as Uint8List?;
      final attachmentBytes = message[4] as Uint8List?;

      final query = Query(
        handle: queryPtr,
        keyExpr: keyExpr,
        parameters: params,
        payloadBytes: payloadBytes,
        attachmentBytes: attachmentBytes,
      );
      controller.add(query);
    }
  });

  final keyExprNative = keyExprStr.toNativeUtf8();
  try {
    final rc = bindings.zd_declare_queryable(
      ptr.cast(),
      loanedSession.cast(),
      keyExprNative.cast(),
      receivePort.sendPort.nativePort,
      complete ? 1 : 0,
    );

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

  return Queryable._(ptr, receivePort, controller, keyExprStr);
}