feat: cleanups and API updates
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Godot;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OwofGames.AetherBind;
|
||||
@@ -7,14 +6,31 @@ using OwofGames.GodotLume;
|
||||
|
||||
namespace GodotHostTest.AetherBind;
|
||||
|
||||
/// <summary>
|
||||
/// A node that provides injected services to other nodes.
|
||||
/// </summary>
|
||||
[Icon("res://addons/AetherBind/ServiceSource.svg")]
|
||||
public partial class ServiceSource : Node
|
||||
{
|
||||
private static readonly string RootScopeQualifiedName = typeof(RootScope).AssemblyQualifiedName!;
|
||||
|
||||
/// <summary>
|
||||
/// List of nodes that must be injected. This list will be automatically populated with all nodes in this object's
|
||||
/// editor scene that have at least one [Inject] field or method.
|
||||
/// </summary>
|
||||
[Export] protected Node?[] InjectedNodes = [];
|
||||
|
||||
/// <summary>
|
||||
/// Name of the scope for the services built by this service source.
|
||||
/// </summary>
|
||||
[Export] private string _serviceScope = RootScopeQualifiedName;
|
||||
|
||||
internal void ResolveInjectedNodes(IProvider provider, Delegate? onScopeCreated)
|
||||
/// <summary>
|
||||
/// Inject the required services in <see cref="InjectedNodes" />.
|
||||
/// </summary>
|
||||
/// <param name="provider">The service provider.</param>
|
||||
/// <param name="onScopeCreated">A callback method invoked with this scope's provider, if a scope was created.</param>
|
||||
internal void ResolveInjectedNodes(IProvider provider, Action<IProvider>? onScopeCreated)
|
||||
{
|
||||
var logger = provider.Get<ILogger<ServiceSource>>();
|
||||
|
||||
@@ -24,16 +40,11 @@ public partial class ServiceSource : Node
|
||||
// create the scoped provider
|
||||
var serviceScope = ScopeTypesAndNames.GetType(_serviceScope);
|
||||
provider = provider.GetScopedProvider(serviceScope);
|
||||
// invoke onScopeCreated, if present
|
||||
if (onScopeCreated != null)
|
||||
{
|
||||
var parameters = onScopeCreated.Method
|
||||
.GetParameters()
|
||||
.Select(parameterInfo => provider.Get(parameterInfo.ParameterType))
|
||||
.ToArray();
|
||||
onScopeCreated.DynamicInvoke(parameters);
|
||||
}
|
||||
|
||||
// invoke onScopeCreated, if present
|
||||
onScopeCreated?.Invoke(provider);
|
||||
|
||||
// invoke overridden callback
|
||||
OnScopeCreated(provider);
|
||||
}
|
||||
else if (onScopeCreated != null)
|
||||
@@ -55,6 +66,12 @@ public partial class ServiceSource : Node
|
||||
}
|
||||
|
||||
// TODO: also add source generator to mark methods in derived classes with [OnScopeCreated] and perform parameter injection
|
||||
/// <summary>
|
||||
/// This method is invoked as soon as the scope for the services is created, but before the services are injected into
|
||||
/// the <see cref="InjectedNodes" />. Override this method in derived classes to perform custom initialization steps
|
||||
/// for the services, like setting <see cref="ValueProvider{T}" />s.
|
||||
/// </summary>
|
||||
/// <param name="serviceProvider">Lume's service provider.</param>
|
||||
protected virtual void OnScopeCreated(IProvider serviceProvider)
|
||||
{
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user