public static void UseLocalStorage <TState>(this ReduxStoreConfig <TState> config, string key = "AppState") where TState : new() { var storageRegistrations = config.Services.Where(s => s.ServiceType == typeof(IStateStorage)).ToArray(); foreach (var storageRegistration in storageRegistrations) { config.Services.Remove(storageRegistration); } config.Services.AddStorage(); config.Services.AddSingleton <IStateStorage>(s => new LocalStorageProvider(key, s.GetService <ILocalStorage>())); }
public static void AddReduxStore <TRootState>(this IServiceCollection services, Action <ReduxStoreConfig <TRootState> > cfg) where TRootState : new() { services.AddSingleton <IActionResolver, BlazorActionResolver>(); services.AddSingleton <IStateStorage, NullStateStorage>(); var reducerMapping = ReducerMappingBuilder <TRootState> .Create(); var config = new ReduxStoreConfig <TRootState>(services, reducerMapping); cfg(config); var rootReducer = reducerMapping.Build(); if (config.UseDevTools) { services.AddSingleton <IDevToolsInterop, ReduxDevToolsInterop>(); } else { services.AddSingleton <IDevToolsInterop, NullDevToolsInterop>(); } if (config.LocationProperty is object) { services.AddSingleton <INavigationTracker <TRootState> >(s => new NavigationTracker <TRootState>(config.LocationProperty, s.GetService <NavigationManager>())); } else { services.AddSingleton <INavigationTracker <TRootState>, NullNavigationTracker <TRootState> >(); } var storeActivator = config.StoreActivator ?? (s => new Store <TRootState>( rootReducer, s.GetService <IActionResolver>(), s.GetService <IStateStorage>(), s.GetService <INavigationTracker <TRootState> >(), s.GetService <IDevToolsInterop>())); services.AddSingleton(storeActivator); services.AddSingleton <IDispatcher>(s => s.GetRequiredService <IStore <TRootState> >()); services.AddSingleton <IStoreInitializer>(s => s.GetRequiredService <IStore <TRootState> >()); }