whenAllActive function

ActivationSetup whenAllActive(
  1. List<AnyFeature> features
)

Activation setup that stays .active only while every parent in features is FeatureStatus.active. Any parent flipping away from .active deactivates the owning feature; a return to "all active" re-activates it.

Every entry must be in the caller's dependsOn / optionalDependsOn list. An empty features list makes the feature permanently active.

final fullyOnlineFeature = createFeature(
  name: 'FullyOnline',
  dependsOn: [networkFeature, sessionFeature],
)..activation(whenAllActive([networkFeature, sessionFeature]));

Implementation

ActivationSetup whenAllActive(List<AnyFeature> features) {
  return (parentApi, toggle, cleanup) {
    if (features.isEmpty) {
      unawaited(toggle(ToggleState.active));
      return;
    }
    final stores = [for (final f in features) parentApi.statusOf(f)];
    void reevaluate() {
      final allActive = stores.every((s) => s.state == FeatureStatus.active);
      unawaited(toggle(allActive ? ToggleState.active : ToggleState.inactive));
    }

    for (final store in stores) {
      cleanup.subscribe(store, (_, _) => reevaluate());
    }
    reevaluate();
  };
}