Skip to content

Commit

Permalink
Merge pull request #261 from megafetis/master
Browse files Browse the repository at this point in the history
replace BlazorStateComponent to IBlazorStateComponent in Subscriptions
  • Loading branch information
StevenTCramer authored Aug 15, 2021
2 parents ee9f5c0 + e47f29b commit 0a04755
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions Source/BlazorState/Components/IBlazorStateComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace BlazorState
/// </example>
public interface IBlazorStateComponent // TODO: evaluate if this interface is even needed
{
public string Id { get; }
IMediator Mediator { get; set; }
IStore Store { get; set; }

Expand Down
16 changes: 8 additions & 8 deletions Source/BlazorState/Subscriptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ public Subscriptions(ILogger<Subscriptions> aLogger)
BlazorStateComponentReferencesList = new List<Subscription>();
}

public Subscriptions Add<T>(BlazorStateComponent aBlazorStateComponent)
public Subscriptions Add<T>(IBlazorStateComponent aBlazorStateComponent)
{
Type type = typeof(T);

return Add(type, aBlazorStateComponent);
}

public Subscriptions Add(Type aType, BlazorStateComponent aBlazorStateComponent)
public Subscriptions Add(Type aType, IBlazorStateComponent aBlazorStateComponent)
{
// Add only once.
if (!BlazorStateComponentReferencesList.Any(aSubscription => aSubscription.StateType == aType && aSubscription.ComponentId == aBlazorStateComponent.Id))
{
var subscription = new Subscription(
aType,
aBlazorStateComponent.Id,
new WeakReference<BlazorStateComponent>(aBlazorStateComponent));
new WeakReference<IBlazorStateComponent>(aBlazorStateComponent));

BlazorStateComponentReferencesList.Add(subscription);
}
Expand All @@ -47,7 +47,7 @@ aObject is Subscriptions subscriptions &&

public override int GetHashCode() => HashCode.Combine(Logger, BlazorStateComponentReferencesList);

public Subscriptions Remove(BlazorStateComponent aBlazorStateComponent)
public Subscriptions Remove(IBlazorStateComponent aBlazorStateComponent)
{
Logger.LogDebug($"Removing Subscription for {aBlazorStateComponent.Id}");
BlazorStateComponentReferencesList.RemoveAll(aRecord => aRecord.ComponentId == aBlazorStateComponent.Id);
Expand Down Expand Up @@ -77,7 +77,7 @@ public void ReRenderSubscribers(Type aType)
IEnumerable<Subscription> subscriptions = BlazorStateComponentReferencesList.Where(aRecord => aRecord.StateType == aType);
foreach (Subscription subscription in subscriptions.ToList())
{
if (subscription.BlazorStateComponentReference.TryGetTarget(out BlazorStateComponent target))
if (subscription.BlazorStateComponentReference.TryGetTarget(out IBlazorStateComponent target))
{
Logger.LogDebug($"ReRender ComponentId:{subscription.ComponentId} StateType.Name:{subscription.StateType.Name}");
target.ReRender();
Expand All @@ -94,13 +94,13 @@ public void ReRenderSubscribers(Type aType)

private readonly struct Subscription : IEquatable<Subscription>
{
public WeakReference<BlazorStateComponent> BlazorStateComponentReference { get; }
public WeakReference<IBlazorStateComponent> BlazorStateComponentReference { get; }

public string ComponentId { get; }

public Type StateType { get; }

public Subscription(Type aStateType, string aComponentId, WeakReference<BlazorStateComponent> aBlazorStateComponentReference)
public Subscription(Type aStateType, string aComponentId, WeakReference<IBlazorStateComponent> aBlazorStateComponentReference)
{
StateType = aStateType;
ComponentId = aComponentId;
Expand All @@ -114,7 +114,7 @@ public Subscription(Type aStateType, string aComponentId, WeakReference<BlazorSt
public bool Equals(Subscription aSubscription) =>
EqualityComparer<Type>.Default.Equals(StateType, aSubscription.StateType) &&
ComponentId == aSubscription.ComponentId &&
EqualityComparer<WeakReference<BlazorStateComponent>>.Default.Equals(BlazorStateComponentReference, aSubscription.BlazorStateComponentReference);
EqualityComparer<WeakReference<IBlazorStateComponent>>.Default.Equals(BlazorStateComponentReference, aSubscription.BlazorStateComponentReference);

public override bool Equals(object aObject) => this.Equals((Subscription)aObject);

Expand Down

0 comments on commit 0a04755

Please sign in to comment.