registerAdaptiveIntegration method

void registerAdaptiveIntegration({
  1. required String pluginId,
  2. required List<PixaPluginIntegrationCandidate> candidates,
  3. bool requireAvailableCandidate = true,
})

Selects and registers exactly one integration path for a pub plugin.

Implementation

void registerAdaptiveIntegration({
  required String pluginId,
  required List<PixaPluginIntegrationCandidate> candidates,
  bool requireAvailableCandidate = true,
}) {
  final String normalizedPluginId = pluginId.trim();
  if (normalizedPluginId.isEmpty) {
    throw StateError('Pixa adaptive plugin id must not be empty.');
  }
  if (_adaptiveIntegrationSelections.any(
    (PixaPluginIntegrationSelection selection) =>
        selection.pluginId == normalizedPluginId,
  )) {
    throw StateError(
      'Duplicate Pixa adaptive plugin integration "$normalizedPluginId".',
    );
  }
  if (candidates.isEmpty) {
    throw StateError(
      'Pixa adaptive plugin "$normalizedPluginId" declares no integration '
      'candidates.',
    );
  }

  final Set<String> candidateIds = <String>{};
  for (final PixaPluginIntegrationCandidate candidate in candidates) {
    final String candidateId = candidate.id.trim();
    if (candidateId.isEmpty) {
      throw StateError(
        'Pixa adaptive plugin "$normalizedPluginId" has an empty candidate '
        'id.',
      );
    }
    if (!candidateIds.add(candidateId)) {
      throw StateError(
        'Pixa adaptive plugin "$normalizedPluginId" declares duplicate '
        'candidate "$candidateId".',
      );
    }
    if (candidate.requiredIntegration && !candidate.available) {
      throw StateError(
        _adaptiveCandidateUnavailableMessage(
          normalizedPluginId,
          candidate,
          requiredCandidate: true,
        ),
      );
    }
  }

  final List<PixaPluginIntegrationCandidate> available = candidates.where((
    PixaPluginIntegrationCandidate candidate,
  ) {
    return candidate.available;
  }).toList()..sort(_compareAdaptiveCandidates);
  if (available.isEmpty) {
    if (!requireAvailableCandidate) {
      return;
    }
    throw StateError(
      'Pixa adaptive plugin "$normalizedPluginId" has no available '
      'integration candidate. ${_adaptiveCandidateDiagnostics(candidates)}',
    );
  }

  final PixaPluginIntegrationCandidate selected = available.first;
  final _PixaRegistryStateSnapshot snapshot = _PixaRegistryStateSnapshot(
    this,
  );
  final Set<String> beforeFetcherIds = _fetchers.keys.toSet();
  final Set<String> beforeDecoderIds = _decoders.keys.toSet();
  final Set<String> beforeProcessorIds = _processors.keys.toSet();
  final Set<String> beforeCacheStoreIds = _cacheStores.keys.toSet();
  try {
    selected.register(this);
    _validateAdaptiveSelectedHandlers(
      registry: this,
      pluginId: normalizedPluginId,
      candidate: selected,
      beforeFetcherIds: beforeFetcherIds,
      beforeDecoderIds: beforeDecoderIds,
      beforeProcessorIds: beforeProcessorIds,
      beforeCacheStoreIds: beforeCacheStoreIds,
    );
    _adaptiveIntegrationSelections.add(
      PixaPluginIntegrationSelection(
        pluginId: normalizedPluginId,
        candidateId: selected.id.trim(),
        mode: selected.mode,
        priority: selected.priority,
        packageName: selected.packageName,
      ),
    );
  } catch (_) {
    snapshot.restore(this);
    rethrow;
  }
}