-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from minhhieugma/master
In JsonStoreHandler, an empty string should be treated similarly to the null string
- Loading branch information
Showing
2 changed files
with
75 additions
and
75 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
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()); | ||
} |
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 |
---|---|---|
@@ -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."); | ||
} | ||
} | ||
} | ||
} |