Skip to content

Commit

Permalink
feat: allow two-phase dependency resolution notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
jolexxa committed Oct 19, 2023
1 parent 18618a9 commit 9665638
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion Chickensoft.AutoInject.Tests/src/Dependent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ public interface IDependent : ISuperNode {
/// <summary>Event invoked when dependencies have been resolved.</summary>
event Action? OnDependenciesResolved;

/// <summary>
/// True if the node is being unit-tested. When unit-tested, setup callbacks
/// will not be invoked.
/// </summary>
bool IsTesting { get; set; }

/// <summary>
/// Called after dependencies are resolved, but before
/// <see cref="OnResolved" /> is called if (and only if)
/// <see cref="IsTesting" /> is false. This allows you to initialize
/// properties that depend on dependencies separate from using those
/// properties to facilitate easier testing.
/// </summary>
void Setup() { }

/// <summary>
/// Method that is invoked when all of the dependent node's dependencies are
/// resolved (after _Ready() but before _Process()).
Expand Down Expand Up @@ -53,7 +68,7 @@ void _AnnounceDependenciesResolved() { }
/// <returns>
/// The resolved dependency value, the fallback value, or throws an exception
/// if the provider wasn't found during dependency resolution and a fallback
/// value was not given
/// value was not given.
/// </returns>
/// <exception cref="ProviderNotFoundException">Thrown if the provider for
/// the requested value could not be found and when no fallback value is
Expand All @@ -73,6 +88,12 @@ public abstract partial class Dependent : Node, IDependent {
// These static stubs don't need to be copied over because we'll be copied
// into a SuperNode that declares these.

/// <summary>
/// True if the node is being unit-tested. When unit-tested, setup callbacks
/// will not be invoked.
/// </summary>
public bool IsTesting { get; set; } = false;

[PowerUpIgnore]
public static ImmutableDictionary<string, ScriptPropertyOrField>
ScriptPropertiesAndFields { get; } =
Expand Down Expand Up @@ -395,6 +416,9 @@ ImmutableDictionary<string, ScriptPropertyOrField> dependenciesToResolve
var providersInitializing = 0;

void resolve() {
if (!dependent.IsTesting) {
dependent.Setup();
}
dependent.OnResolved();
dependent._AnnounceDependenciesResolved();
}
Expand Down

0 comments on commit 9665638

Please sign in to comment.