Skip to content

Commit

Permalink
RX State
Browse files Browse the repository at this point in the history
Disable with parameter
  • Loading branch information
Bernhard Straub committed Feb 9, 2024
1 parent d028998 commit 531feab
Show file tree
Hide file tree
Showing 26 changed files with 363 additions and 128 deletions.
29 changes: 14 additions & 15 deletions RxBlazorLightCore/Component/RxBLServiceStateSubscriber.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
using Microsoft.AspNetCore.Components;

namespace RxBlazorLightCore
namespace RxBlazorLightCore;

public class RxBLServiceChangeSubscriber<T> : ComponentBase where T : IRxBLService
{
public class RxBLServiceChangeSubscriber<T> : ComponentBase where T : IRxBLService
{
[CascadingParameter]
public required T Service { get; init; }
[CascadingParameter]
public required T Service { get; init; }

[Parameter]
public double SampleRateMS { get; set; } = 100;
[Parameter]
public double SampleRateMS { get; set; } = 100;

protected override void OnInitialized()
{
base.OnInitialized();
Service.Subscribe(cr => ServiceStateHasChanged(cr.ID, cr.Reason), SampleRateMS);
}
protected override void OnInitialized()
{
base.OnInitialized();
Service.Subscribe(cr => ServiceStateHasChanged(cr.ID, cr.Reason), SampleRateMS);
}

protected virtual void ServiceStateHasChanged(Guid id, ChangeReason changeReason)
{
}
protected virtual void ServiceStateHasChanged(Guid id, ChangeReason changeReason)
{
}
}
7 changes: 5 additions & 2 deletions RxBlazorLightCore/Core/Interfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public void EnterScope() { }
public void LeaveScope() { }
}


public interface IRxBLService
{
public ValueTask OnContextReadyAsync();
Expand Down Expand Up @@ -81,14 +80,18 @@ public interface IStateProvideTransformBase
public interface IStateTransformer<T> : IStateProvideTransformBase
{
public void Transform(T value);

public bool CanTransform(T value);
}

public interface IStateProvider<T> : IStateProvideTransformBase
{
public void Provide();

public bool CanProvide(T value);
}

public interface IServiceStateProvider<T> : IStateTransformer<T>
public interface IServiceStateTransformer<T> : IStateTransformer<T>
{
}

Expand Down
2 changes: 1 addition & 1 deletion RxBlazorLightCore/Core/ServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static class ServiceExtensions
return services;
}

public static IServiceCollection AddRxBLService<T>(this IServiceCollection services, Func<System.IServiceProvider, T> serviceFactory, double sampleMS = 100) where T : RxBLService
public static IServiceCollection AddRxBLService<T>(this IServiceCollection services, Func<IServiceProvider, T> serviceFactory, double sampleMS = 100) where T : RxBLService
{
services.TryAddSingleton<RxServiceCollector>();

Expand Down
107 changes: 86 additions & 21 deletions RxBlazorLightCore/Core/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public class State<S, TInterface, TType> : IState<TInterface, TType>
public bool CanRun => _valueProvider.CanRun;
public bool LongRunning => _valueProvider.LongRunning;
public bool CanCancel => _valueProvider.CanCancel;
public virtual bool CanTransform(TType _)
{
return _valueProvider.CanRun;
}

public StateChangePhase Phase => _valueProvider.Phase;
public Guid ID => _valueProvider.ID;

Expand All @@ -22,14 +27,14 @@ protected internal State(S service, TType? value, Func<IState<TInterface, TType>
{
Value = value;
_valueProvider = valueProviderFactory is null ?
new StateTransformDirect<S, TInterface, TType>(service, this) : valueProviderFactory(this);
new StateTransformerDirect<S, TInterface, TType>(service, this) : valueProviderFactory(this);
}

protected internal State(S service, TType? value, Func<IState<TType>, IStateTransformer<TType>>? valueProviderFactory = null)
{
Value = value;
_valueProvider = valueProviderFactory is null ?
new StateTransformDirect<S, TInterface, TType>(service, this) : valueProviderFactory((IState<TType>)this);
new StateTransformerDirect<S, TInterface, TType>(service, this) : valueProviderFactory((IState<TType>)this);
}

public bool HasValue()
Expand Down Expand Up @@ -76,7 +81,7 @@ public static IState<TType> Create(S service, TType? value, Func<IState<TType>,
public class State<S> : State<S, object?, object?>, IState
where S : RxBLService
{
private State(S service) : base (service, IState.Default, null) { }
private State(S service) : base(service, IState.Default, null) { }

public static IState Create(S service)
{
Expand Down Expand Up @@ -239,31 +244,46 @@ protected void StateChanged(StateChangePhase phase, Exception? exception = null)

}

public class StateTransformDirect<S, TInterface, TType>(S service, IState<TInterface, TType> state) :
public class StateTransformerDirect<S, TInterface, TType>(S service, IState<TInterface, TType> state) :
StateProvideTransformBase<S, TType, TInterface, TType>(service, state, true, true), IStateTransformer<TType>
where S : RxBLService
where TType : TInterface
{
public virtual bool CanTransform(TType _)
{
return CanRun;
}

public void Transform(TType value)
{
TransformBaseSync(value);
}
}

public class StateTransformDirect<S, TType>(S service, IState<TType> state) :
StateProvideTransformBase<S, TType, TType, TType>(service, state, true, true), IStateTransformer<TType>
public class StateTransformerDirect<S, T>(S service, IState<T> state) :
StateProvideTransformBase<S, T, T, T>(service, state, true, true), IStateTransformer<T>
where S : RxBLService
{
public void Transform(TType value)
public virtual bool CanTransform(T _)
{
return CanRun;
}

public void Transform(T value)
{
TransformBaseSync(value);
}
}

public abstract class StateTransformAsync<S, T, TType>(S service, IState<TType> state) :
public abstract class StateTransformerAsync<S, T, TType>(S service, IState<TType> state) :
StateProvideTransformBase<S, T, TType, TType>(service, state, true, true), IStateTransformer<T>
where S : RxBLService
{
public virtual bool CanTransform(T _)
{
return CanRun;
}

public void Transform(T value)
{
TransformBaseAsync(value);
Expand All @@ -282,10 +302,15 @@ public void Transform(T value)
protected abstract Task<TType> TransformStateAsync(T value, CancellationToken cancellationToken);
}

public abstract class StateTransform<S, T, TType>(S service, IState<TType> state) :
public abstract class StateTransformer<S, T, TType>(S service, IState<TType> state) :
StateProvideTransformBase<S, T, TType, TType>(service, state, true, false), IStateTransformer<T>
where S : RxBLService
{
public virtual bool CanTransform(T _)
{
return CanRun;
}

public void Transform(T value)
{
try
Expand All @@ -301,10 +326,15 @@ public void Transform(T value)
protected abstract TType? TransformState(T value);
}

public abstract class ValueProviderAsync<S, T>(S service, IState<T> state) :
public abstract class StateProviderAsync<S, T>(S service, IState<T> state) :
StateProvideTransformBase<S, T, T, T>(service, state, true, true), IStateProvider<T>
where S : RxBLService
{
public virtual bool CanProvide(T _)
{
return CanRun;
}

public void Provide()
{
TransformBaseAsync(default);
Expand All @@ -318,10 +348,15 @@ public void Provide()
protected abstract Task<T?> ProvideValueAsync(CancellationToken cancellationToken);
}

public abstract class ValueProvider<S, T>(S service, IState<T> state) :
public abstract class StateProvider<S, T>(S service, IState<T> state) :
StateProvideTransformBase<S, T, T, T>(service, state, true, false), IStateProvider<T>
where S : RxBLService
{
public virtual bool CanProvide(T _)
{
return CanRun;
}

public void Provide()
{
try
Expand All @@ -337,11 +372,16 @@ public void Provide()
protected abstract T ProvideValue();
}

public abstract class StateRefTransformAsync<S, T, TInterface, TType>(S service, IState<TInterface, TType> state) :
public abstract class StateRefTransformerAsync<S, T, TInterface, TType>(S service, IState<TInterface, TType> state) :
StateProvideTransformBase<S, T, TInterface, TType>(service, state, false, true), IStateTransformer<T>
where S : RxBLService
where TType : class, TInterface
{
public virtual bool CanTransform(T _)
{
return CanRun;
}

public void Transform(T value)
{
TransformBaseAsync(value);
Expand All @@ -362,11 +402,16 @@ public void Transform(T value)
protected abstract Task TransformStateAsync(T value, TType stateRef, CancellationToken cancellationToken);
}

public abstract class StateRefTransform<S, T, TInterface, TType>(S service, IState<TInterface, TType> state) :
public abstract class StateRefTransformer<S, T, TInterface, TType>(S service, IState<TInterface, TType> state) :
StateProvideTransformBase<S, T, TInterface, TType>(service, state, false, false), IStateTransformer<T>
where S : RxBLService
where TType : class, TInterface
{
public virtual bool CanTransform(T _)
{
return CanRun;
}

public void Transform(T value)
{
ArgumentNullException.ThrowIfNull(State.Value);
Expand All @@ -384,11 +429,16 @@ public void Transform(T value)
protected abstract void TransformState(T value, TType stateRef);
}

public abstract class ServiceProviderAsync<S, T>(S service) :
StateProvideTransformBase<S, T?, object?, object?>(service, null, true, true), IServiceStateProvider<T>
public abstract class ServiceStateTransformerAsync<S, T>(S service) :
StateProvideTransformBase<S, T?, object?, object?>(service, null, true, true), IServiceStateTransformer<T>
where T : notnull
where S : RxBLService
{
public virtual bool CanTransform(T _)
{
return CanRun;
}

public void Transform(T value)
{
TransformBaseAsync(value);
Expand All @@ -408,11 +458,16 @@ public void Transform(T value)
protected abstract Task TransformStateAsync(T value, CancellationToken cancellationToken);
}

public abstract class ServiceProvider<S, T>(S service) :
StateProvideTransformBase<S, T?, object?, object?>(service, null, true, false), IServiceStateProvider<T>
where T : notnull
where S : RxBLService
public abstract class ServiceStateTransformer<S, T>(S service) :
StateProvideTransformBase<S, T?, object?, object?>(service, null, true, false), IServiceStateTransformer<T>
where T : notnull
where S : RxBLService
{
public virtual bool CanTransform(T _)
{
return CanRun;
}

public void Transform(T value)
{
try
Expand All @@ -429,10 +484,15 @@ public void Transform(T value)
protected abstract void TransformState(T value);
}

public abstract class ServiceProviderAsync<S>(S service) :
public abstract class ServiceStateProviderAsync<S>(S service) :
StateProvideTransformBase<S, object?, object?, object?>(service, null, true, true), IServiceStateProvider
where S : RxBLService
{
public bool CanProvide(object? _)
{
return CanRun;
}

public void Provide()
{
TransformBaseAsync(IState.Default);
Expand All @@ -450,10 +510,15 @@ public void Provide()
protected abstract Task ProvideStateAsync(CancellationToken cancellationToken);
}

public abstract class ServiceProvider<S>(S service) :
public abstract class ServiceStateProvider<S>(S service) :
StateProvideTransformBase<S, object?, object?, object?>(service, null, false, false), IServiceStateProvider
where S : RxBLService
{
public bool CanProvide(object? _)
{
return CanRun;
}

public void Provide()
{
try
Expand Down
Loading

0 comments on commit 531feab

Please sign in to comment.