stopConsumingTranslation function

Future<String?> stopConsumingTranslation(
  1. StopConsumingTranslationOptions options
)

Stop consuming a translation producer and close its consumer. Returns the original producer ID if found, so caller can resume it.

Flow:

  1. Find the translation producer ID from the translation map
  2. Find the consumer transport for that producer
  3. Close the consumer (both locally and on server)
  4. Return the original producer ID for resuming

Implementation

Future<String?> stopConsumingTranslation(
    StopConsumingTranslationOptions options) async {
  try {
    final language = options.language;
    final translationProducerMap = options.translationProducerMap;
    final parameters = options.parameters;
    final consumerTransports = parameters.consumerTransports;
    final updateConsumerTransports = parameters.updateConsumerTransports;

    // Find the original producer ID and translation producer ID from the map
    String? originalProducerId;
    String? translationProducerId;

    for (final entry in translationProducerMap.entries) {
      final pid = entry.key;
      final meta = entry.value;
      if (meta is TranslationMeta) {
        if (meta.language == language && meta.speakerId == options.speakerId) {
          translationProducerId = pid;
          originalProducerId = meta.originalProducerId;
          break;
        }
      }
    }

    if (translationProducerId == null) {
      return originalProducerId;
    }

    // Find the consumer transport for the translation producer
    final transportIndex = consumerTransports.indexWhere(
      (t) => t.producerId == translationProducerId,
    );

    if (transportIndex == -1) {
      return originalProducerId;
    }

    final transport = consumerTransports[transportIndex];

    // Close the consumer on the server
    transport.socket_.emit(
      'consumer-close',
      {'serverConsumerId': transport.serverConsumerTransportId},
    );

    // Close the consumer locally
    transport.consumer.close();

    // Remove from consumer transports array
    final updatedTransports = List<TransportType>.from(consumerTransports);
    updatedTransports.removeAt(transportIndex);
    updateConsumerTransports(updatedTransports);

    return originalProducerId;
  } catch (e) {
    return null;
  }
}