Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow two-phase dependency resolution notifications #8

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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