///<summary> /// Activates a child whenever the specified parent is activated. /// Use with caution, it is better to use explicit <see cref="IConductor"/> implementations. ///</summary> ///<param name="child">The child to (de)activate.</param> ///<param name="parent">The parent whose activation triggers the child's activation.</param> public static void ActivateWith(this IActivate child, IActivate parent) { if (parent == null) { return; } // Note on the disposal, in a hierarchical composition we'll always want to close the child(ren) first. // Eg in a multilevel tree the leaf nodes will be closed first, working our way back to the root. child.Activator.AddDisposable( parent.WhenAnyValue(x => x.IsActive) .Where(isActive => isActive) .Select(_ => child.ActivateAsync(CancellationToken.None)) // Tasks are executed eagerly, fire and forget .Subscribe() ); }