feat: more plugin work and intra-scope communication
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
|
||||
namespace GodotHostTest.GodotDI;
|
||||
|
||||
/// <summary>
|
||||
/// An object that allows communication through scopes, by providing a couple getter/setter that can be injected in
|
||||
/// different scopes.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type that's been passed through scopes.</typeparam>
|
||||
/// <seealso cref="BuilderExtensions.AddValueProvider" />
|
||||
public class ValueProvider<T>
|
||||
{
|
||||
private bool _isSet;
|
||||
private T? _value;
|
||||
|
||||
public ValueProvider()
|
||||
{
|
||||
Setter = new SetterImplementation(this);
|
||||
}
|
||||
|
||||
public IValueSetter<T> Setter { get; }
|
||||
|
||||
public IValueGetter<T> Getter => !_isSet
|
||||
? throw new InvalidOperationException($"Value of type {typeof(T)} has not been set yet.")
|
||||
: new GetterImplementation(_value!);
|
||||
|
||||
private class SetterImplementation(ValueProvider<T> valueProvider) : IValueSetter<T>
|
||||
{
|
||||
public void Set(T value)
|
||||
{
|
||||
if (valueProvider._isSet)
|
||||
throw new InvalidOperationException($"A value (of type {typeof(T)}) cannot be set more than once.");
|
||||
|
||||
valueProvider._value = value;
|
||||
valueProvider._isSet = true;
|
||||
}
|
||||
}
|
||||
|
||||
private class GetterImplementation(T value) : IValueGetter<T>
|
||||
{
|
||||
public T Get()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user