withCameraWhenAvailable<T> static method

Future<T?> withCameraWhenAvailable<T>(
  1. CameraPosition position,
  2. CameraOwner owner,
  3. Future<T> operation(
    1. Camera camera
    ), [
  4. int? timeoutMs,
])

Execute camera operations, waiting for ownership if necessary

Implementation

static Future<T?> withCameraWhenAvailable<T>(
  CameraPosition position,
  CameraOwner owner,
  Future<T> Function(Camera camera) operation, [
  int? timeoutMs,
]) async {
  // Try to get ownership, wait if necessary
  final acquired = await requestOwnership(position, owner, timeoutMs);
  if (!acquired) {
    SdkLogger.info('CameraOwnershipHelper', 'withCameraWhenAvailable',
        'Could not acquire camera ownership for ${owner.id} within timeout');
    return null;
  }

  final camera = Camera.atPosition(position);
  if (camera == null) {
    SdkLogger.info('CameraOwnershipHelper', 'withCameraWhenAvailable', 'Camera not available at position $position');
    return null;
  }

  try {
    final result = await operation(camera);
    return result;
  } catch (error) {
    SdkLogger.info(
        'CameraOwnershipHelper', 'withCameraWhenAvailable', 'Camera operation failed for ${owner.id}: $error');
    rethrow;
  }
}