-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
115 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Microsoft.AspNetCore.Components; | ||
using System; | ||
|
||
namespace Fluxor.Blazor.Web.Components | ||
{ | ||
/// <summary> | ||
/// A layout that auto-subscribes to state changes on all <see cref="IState"/> properties | ||
/// and ensures <see cref="LayoutComponentBase.StateHasChanged"/> is called | ||
/// </summary> | ||
public class FluxorLayout : LayoutComponentBase, IDisposable | ||
{ | ||
private bool Disposed; | ||
private IDisposable StateSubscription; | ||
|
||
/// <summary> | ||
/// Disposes of the component and unsubscribes from any state | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
} | ||
|
||
/// <summary> | ||
/// Subscribes to state properties | ||
/// </summary> | ||
protected override void OnInitialized() | ||
{ | ||
base.OnInitialized(); | ||
StateSubscription = StateSubscriber.Subscribe(this, _ => StateHasChanged()); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!Disposed) | ||
{ | ||
if (disposing) | ||
{ | ||
StateSubscription.Dispose(); | ||
} | ||
Disposed = true; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using GetStateDelegate = System.Func<object, Fluxor.IState>; | ||
|
||
namespace Fluxor.Blazor.Web.Components | ||
{ | ||
internal static class StateSubscriber | ||
{ | ||
private static readonly ConcurrentDictionary<Type, IEnumerable<GetStateDelegate>> ValueDelegatesForType; | ||
|
||
static StateSubscriber() | ||
{ | ||
ValueDelegatesForType = new ConcurrentDictionary<Type, IEnumerable<GetStateDelegate>>(); | ||
} | ||
|
||
public static IDisposable Subscribe(object subject, Action<IState> callback) | ||
{ | ||
if (subject == null) | ||
throw new ArgumentNullException(nameof(subject)); | ||
if (callback == null) | ||
throw new ArgumentNullException(nameof(callback)); | ||
|
||
IEnumerable<GetStateDelegate> getStateDelegates = GetStateDelegatesForType(subject.GetType()); | ||
var subscriptions = new List<(IState State, EventHandler Handler)>(); | ||
foreach(GetStateDelegate getState in getStateDelegates) | ||
{ | ||
var state = (IState)getState(subject); | ||
var handler = new EventHandler((s, a) => callback(state)); | ||
|
||
subscriptions.Add((state, handler)); | ||
state.StateChanged += handler; | ||
} | ||
return new DisposableCallback(() => | ||
{ | ||
foreach (var subscription in subscriptions) | ||
subscription.State.StateChanged -= subscription.Handler; | ||
}); | ||
} | ||
|
||
private static IEnumerable<GetStateDelegate> GetStateDelegatesForType(Type type) | ||
{ | ||
return ValueDelegatesForType.GetOrAdd(type, _ => | ||
{ | ||
var delegates = new List<GetStateDelegate>(); | ||
const BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; | ||
IEnumerable<PropertyInfo> stateProperties = type.GetProperties(bindingFlags) | ||
.Where(p => p.PropertyType.IsGenericType) | ||
.Where(p => p.PropertyType.GetGenericTypeDefinition() == typeof(IState<>)); | ||
foreach(PropertyInfo currentProperty in stateProperties) | ||
{ | ||
Type stateType = currentProperty.PropertyType.GetGenericArguments()[0]; | ||
Type iStateType = typeof(IState<>).MakeGenericType(stateType); | ||
Type getterMethod = typeof(Func<,>).MakeGenericType(type, iStateType); | ||
Delegate stronglyTypedDelegate = Delegate.CreateDelegate(getterMethod, currentProperty.GetGetMethod(true)); | ||
var getValueDelegate = new GetStateDelegate(x => (IState)stronglyTypedDelegate.DynamicInvoke(x)); | ||
delegates.Add(getValueDelegate); | ||
} | ||
return delegates; | ||
}); | ||
} | ||
} | ||
} |