Skip to content

Instantly share code, notes, and snippets.

@Gaulomatic
Last active February 3, 2023 11:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gaulomatic/a6280806413e8a04bab0abf2433780e9 to your computer and use it in GitHub Desktop.
Save Gaulomatic/a6280806413e8a04bab0abf2433780e9 to your computer and use it in GitHub Desktop.
Reactive Fluxor Middleware
using System;
using Blazor.Fluxor;
namespace Logixware.Web.Blazor.Fluxor
{
public interface IReactiveEffects
{
IObservable<IAction> Actions { get; }
}
}
using System;
namespace Logixware.Web.Blazor.Fluxor
{
public interface IReactiveStore
{
IObservable<Object> States { get; }
}
}
using System;
using Microsoft.Extensions.DependencyInjection;
using Blazor.Fluxor.DependencyInjection;
namespace Logixware.Web.Blazor.Fluxor
{
public static class OptionsExtensions
{
public static Options AddReactiveMiddleware(this Options options, IServiceCollection services)
{
if (options == null) throw new ArgumentNullException(nameof(options));
if (services == null) throw new ArgumentNullException(nameof(services));
options.AddMiddleware<ReactiveMiddleware>();
services.AddScoped<IReactiveEffects>(x => x.GetRequiredService<ReactiveMiddleware>());
services.AddScoped<IReactiveStore>(x => x.GetRequiredService<ReactiveMiddleware>());
return options;
}
}
}
using System;
using System.Linq;
using System.Reactive.Linq;
using System.Collections.Generic;
using Blazor.Fluxor;
namespace Logixware.Web.Blazor.Fluxor
{
public class ReactiveMiddleware : Middleware, IReactiveEffects, IReactiveStore
{
private readonly List<IObserver<IAction>> _ActionSubscribers;
private readonly List<IObserver<Object>> _StateSubscribers;
private Dictionary<Type, Object> _States;
public ReactiveMiddleware()
{
this._ActionSubscribers = new List<IObserver<IAction>>();
this._StateSubscribers = new List<IObserver<Object>>();
this._States = new Dictionary<Type, Object>();
}
public IObservable<IAction> Actions
{
get
{
return Observable.Create<IAction>(o =>
{
this._ActionSubscribers.Add(o);
return () => this._ActionSubscribers.Remove(o);
});
}
}
public IObservable<Object> States
{
get
{
return Observable.Create<Object>(o =>
{
this._StateSubscribers.Add(o);
return () => this._StateSubscribers.Remove(o);
});
}
}
public override void BeforeDispatch(IAction action)
{
base.BeforeDispatch(action);
foreach (var __Feature in base.Store.Features.Values.OrderBy(x => x.GetName()))
{
this._States.Add(__Feature.GetStateType(), __Feature.GetState());
}
}
public override void AfterDispatch(IAction action)
{
base.AfterDispatch(action);
this._ActionSubscribers.ForEach(x => x.OnNext(action));
foreach (var __Feature in base.Store.Features.Values.OrderBy(x => x.GetName()))
{
if (this._States.ContainsKey(__Feature.GetStateType()) && !Object.ReferenceEquals(__Feature.GetState(), this._States[__Feature.GetStateType()]))
{
this._StateSubscribers.ForEach(x => x.OnNext(__Feature.GetState()));
}
}
this._States.Clear();
}
}
}
services.AddFluxor(options => options
....
.AddReactiveMiddleware(services)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment