callWithMeta static method

Future<BridgeResult> callWithMeta(
  1. String functionName, {
  2. List? positional,
  3. Map<String, dynamic>? named,
  4. Duration timeout = const Duration(seconds: 10),
})

Call a registered Python function by name, returning the value and metadata.

Implementation

static Future<BridgeResult<dynamic>> callWithMeta(
  String functionName, {
  List<dynamic>? positional,
  Map<String, dynamic>? named,
  Duration timeout = const Duration(seconds: 10),
}) async {
  if (!_initialized || _process == null) {
    throw StateError('PythonBridge.init() must be called before call().');
  }

  final id = _nextId++;
  final completer = Completer<Map<String, dynamic>>();
  _pending[id] = completer;

  final payload = <String, dynamic>{
    'id': id,
    'func': functionName,
    'args': _encodeValue(positional ?? const []),
    'kwargs': _encodeValue(named ?? const <String, dynamic>{}),
    'codec': _codec.name,
    'issued_at': DateTime.now().millisecondsSinceEpoch / 1000,
  };

  _sendFrame(_codec.encode(payload));

  try {
    final response = await completer.future.timeout(timeout);
    if (response['ok'] == true) {
      final result = _decodeValue(response['result']);
      final meta = (response['meta'] as Map?)?.cast<String, dynamic>() ??
          const <String, dynamic>{};
      return BridgeResult<dynamic>(
        result: result,
        meta: meta,
      );
    }

    throw BridgeException(
      response['error'] as String? ?? 'Unknown Python error',
      details: {
        'pythonType': response['type'],
        'id': response['id'],
      }..removeWhere((_, value) => value == null),
      meta: (response['meta'] as Map?)?.cast<String, dynamic>(),
      traceback: response['traceback'] as String?,
    );
  } on TimeoutException {
    throw TimeoutException(
        'Python call timed out for $functionName', timeout);
  } finally {
    _pending.remove(id);
  }
}