translationProducerClosed function

Future<void> translationProducerClosed(
  1. TranslationProducerClosedOptions options
)

Handles the translation:producerClosed socket event. Called when a translation producer is closed.

Implementation

Future<void> translationProducerClosed(
    TranslationProducerClosedOptions options) async {
  try {
    final data = options.data;

    // Remove from producer map
    options.updateTranslationProducerMap?.call((prev) {
      final next = Map<String, Map<String, String>>.from(prev);
      for (final entry in next.entries.toList()) {
        final langMap = entry.value;
        if (langMap[data.language] == data.producerId) {
          langMap.remove(data.language);
          if (langMap.isEmpty) {
            next.remove(entry.key);
          }
        }
      }
      return next;
    });

    // Stop consuming
    if (options.stopConsumingTranslation != null) {
      await options.stopConsumingTranslation!(data.producerId);
    }

    // Resume original producer
    if (options.resumeOriginalProducer != null) {
      await options.resumeOriginalProducer!(data.speakerId);
    }

    if (data.reason != null) {
      options.showAlert?.call(
        message: 'Translation stopped: ${data.reason}',
        type: 'info',
        duration: 2000,
      );
    }
  } catch (e) {
    // Handle error silently
  }
}