Skip to content

Commit

Permalink
Merge pull request #12 from minhhieugma/master
Browse files Browse the repository at this point in the history
In JsonStoreHandler, an empty string should be treated similarly to the null string
  • Loading branch information
Tailslide authored Apr 20, 2023
2 parents 7856642 + 29351fb commit c9bc947
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 75 deletions.
45 changes: 22 additions & 23 deletions Fluxor.Persist/Storage/DictionaryStoreHandler.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
using Fluxor.Persist.Middleware;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;

namespace Fluxor.Persist.Storage
namespace Fluxor.Persist.Storage;

public sealed class DictionaryStoreHandler : IStoreHandler
{
public class DictionaryStoreHandler : IStoreHandler
{
private readonly IObjectStateStorage _store;
private ILogger<PersistMiddleware> _logger;
private readonly IObjectStateStorage _store;
private readonly ILogger _logger;

public DictionaryStoreHandler(IObjectStateStorage store, ILogger<DictionaryStoreHandler> logger)
{
_store = store;
_logger = logger;
}

public DictionaryStoreHandler(IObjectStateStorage store, ILogger<PersistMiddleware> logger)
public async Task<object> GetState(IFeature feature)
{
_logger?.LogDebug("Rehydrating state {FeatureName}", feature.GetName());
var state = await _store.GetStateAsync(feature.GetName());
if (state == null)
{
_store = store;
_logger = logger;
_logger?.LogDebug("No saved state for {FeatureName}, skipping", feature.GetName());
return feature.GetState(); //get initial state
}

public async Task<object> GetState(IFeature feature)
{
_logger?.LogDebug($"Rehydrating state {feature.GetName()}");
var state = await _store.GetStateAsync(feature.GetName());
if (state == null)
{
_logger?.LogDebug($"No saved state for {feature.GetName()}, skipping");
return feature.GetState(); //get initial state
}
return state;
}
public async Task SetState(IFeature feature) => await _store.StoreStateAsync(feature.GetName(), feature.GetState());
return state;
}
}

public async Task SetState(IFeature feature) => await _store.StoreStateAsync(feature.GetName(), feature.GetState());
}
105 changes: 53 additions & 52 deletions Fluxor.Persist/Storage/JsonStoreHandler.cs
Original file line number Diff line number Diff line change
@@ -1,71 +1,72 @@
using Fluxor.Persist.Middleware;
using Microsoft.Extensions.Logging;
using System;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace Fluxor.Persist.Storage;

namespace Fluxor.Persist.Storage
public sealed class JsonStoreHandler : IStoreHandler
{
public class JsonStoreHandler : IStoreHandler
private readonly IStringStateStorage _localStorage;
private readonly ILogger _logger;

public JsonStoreHandler(IStringStateStorage localStorage, ILogger<JsonStoreHandler> logger)
{
private IStringStateStorage LocalStorage;
private ILogger<PersistMiddleware> Logger;
public JsonStoreHandler(IStringStateStorage localStorage, ILogger<PersistMiddleware> logger)
_localStorage = localStorage;
_logger = logger;
}

public async Task<object> GetState(IFeature feature)
{
_logger?.LogDebug("Rehydrating state {FeatureName}", feature.GetName());
string json = await _localStorage.GetStateJsonAsync(feature.GetName());
if (string.IsNullOrEmpty(json))
{
LocalStorage = localStorage;
Logger = logger;
_logger?.LogDebug("No saved state for {FeatureName}, skipping", feature.GetName());
}

public async Task<object> GetState(IFeature feature)
else
{
Logger?.LogDebug($"Rehydrating state {feature.GetName()}");
string json = await LocalStorage.GetStateJsonAsync(feature.GetName());
if (json == null)
////Logger?.LogDebug($"Deserializing type {feature.GetStateType().ToString()} from json {json}");
_logger?.LogDebug("Deserializing type {StateType}", feature.GetStateType());
try
{
Logger?.LogDebug($"No saved state for {feature.GetName()}, skipping");
}
else
{
////Logger?.LogDebug($"Deserializing type {feature.GetStateType().ToString()} from json {json}");
Logger?.LogDebug($"Deserializing type {feature.GetStateType().ToString()}");
try
{
object stronglyTypedFeatureState = JsonSerializer.Deserialize(
json,
feature.GetStateType());
if (stronglyTypedFeatureState == null)
{
Logger?.LogError($"Deserialize returned null");
}
else
// Now set the feature's state to the deserialized object
return stronglyTypedFeatureState;
}
catch (Exception ex)
object stronglyTypedFeatureState = JsonSerializer.Deserialize(
json,
feature.GetStateType());
if (stronglyTypedFeatureState == null)
{
Logger?.LogError(ex, "Failed to deserialize state. Skipping.");
_logger?.LogError($"Deserialize returned null");
}
else
// Now set the feature's state to the deserialized object
return stronglyTypedFeatureState;
}
catch (Exception ex)
{
_logger?.LogError(ex, "Failed to deserialize state. Skipping.");
}
return feature.GetState(); //get initial state
}

public async Task SetState(IFeature feature)
return feature.GetState(); //get initial state
}

public async Task SetState(IFeature feature)
{
try
{
try
var state = feature.GetState();
var options = new JsonSerializerOptions
{
var state = feature.GetState();
var options = new JsonSerializerOptions
{
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
string serializedState = JsonSerializer.Serialize(state, options);
await LocalStorage.StoreStateJsonAsync(feature.GetName(), serializedState); }
catch (Exception e)
{
Logger?.LogError(e, "Failed to serialize state. Skipping.");
}
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
string serializedState = JsonSerializer.Serialize(state, options);
await _localStorage.StoreStateJsonAsync(feature.GetName(), serializedState);
}
catch (Exception e)
{
_logger?.LogError(e, "Failed to serialize state. Skipping.");
}
}
}
}

0 comments on commit c9bc947

Please sign in to comment.