isolateEntryPoint function

void isolateEntryPoint(
  1. IsolateProcessingConfig config
)

Entry point for the isolate that processes files.

This static method runs in a separate isolate and processes files independently of the main thread.

Parameters:

  • config: Configuration containing files and patterns to process

Implementation

void isolateEntryPoint(IsolateProcessingConfig config) async {
  final startTime = DateTime.now();
  final usedTerms = <String>{};
  final termsToCheck = config.translationTerms.toSet();

  int filesProcessed = 0;

  for (final filePath in config.filePaths) {
    if (termsToCheck.isEmpty) {
      // Early termination if all terms are found
      break;
    }

    try {
      // Use streaming for large files to reduce memory usage
      final file = File(filePath);
      final stat = await file.stat();

      String content;
      if (stat.size > 1024 * 1024) {
        // Files > 1MB
        // Stream large files
        content = await readFileStreaming(file);
      } else {
        content = await file.readAsString();
      }

      // Check for term usage with pre-compiled patterns
      final foundTerms = <String>{};
      for (final term in termsToCheck) {
        final pattern = config.regexPatterns[term]!;
        if (pattern.hasMatch(content)) {
          foundTerms.add(term);
        }
      }

      // Add found terms and remove from check list
      usedTerms.addAll(foundTerms);
      termsToCheck.removeAll(foundTerms);

      filesProcessed++;
    } catch (e) {
      // Continue processing other files if one fails
      filesProcessed++;
    }
  }

  final endTime = DateTime.now();
  final result = ProcessingResult(
    usedTerms: usedTerms,
    filesProcessed: filesProcessed,
    chunkId: config.chunkId,
    processingTime: endTime.difference(startTime),
  );

  config.responsePort.send(result);
}