Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/NetRx.Store.Monitor.Extension/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("2.1.0.0")]
[assembly: AssemblyFileVersion("2.1.0.0")]
8 changes: 4 additions & 4 deletions src/NetRx.Store.Monitor.Extension/app.config
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" /></startup></configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7"/></startup></configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<AssemblyOriginatorKeyFile>Key.snk</AssemblyOriginatorKeyFile>

<PackageId>NetRx.Store.Monitor.Shared</PackageId>
<Version>2.0.0</Version>
<Version>2.1.0</Version>
<Authors>Vitalii Ilchenko</Authors>
<Description>Includes library which allows to attach NetRx.Store Monitor tool to an application at a runtime</Description>
<RepositoryUrl>https://github.com/ilchenkob/NetRx.Store</RepositoryUrl>
Expand Down
16 changes: 10 additions & 6 deletions src/NetRx.Store.Tests/NetRx.Store.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="System.Collections.Immutable" Version="1.5.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.core" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
<PackageReference Include="System.Collections.Immutable" Version="5.0.0" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.core" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
9 changes: 9 additions & 0 deletions src/NetRx.Store.Tests/State/Actions/TestStateActions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;

using NetRx.Store;

namespace NetRx.Store.Tests.State.TestStateActions
Expand All @@ -14,4 +15,12 @@ public SetItemsAction(List<string> payload) : base(payload)
{
}
}

public class SetReferenceObjectAction : Action<ReferenceObect>
{
public SetReferenceObjectAction(ReferenceObect payload) :base(payload)
{

}
}
}
7 changes: 7 additions & 0 deletions src/NetRx.Store.Tests/State/Reducers/TestStateReducer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ public TestState Reduce(TestState state, TestStateActions.SetItemsAction action)
state.Items = action.Payload.ToImmutableList();
return state;
}

public TestState Reduce(TestState state, TestStateActions.SetReferenceObjectAction action)
{
ReduceCalls.Add(action.GetType().FullName);
state.ReferenceObect = action.Payload;
return state;
}
}

public class SecondaryTestStateReducer : Reducer<SecondaryTestState>
Expand Down
13 changes: 12 additions & 1 deletion src/NetRx.Store.Tests/State/States/TestState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public struct SubState
};
}

public class ReferenceObect
{
public string Name { get; set; }

public string Description { get; set; }
}

public struct TestState
{
public int Id { get; set; }
Expand All @@ -39,13 +46,17 @@ public struct TestState

public ImmutableList<string> Items { get; set; }

public ReferenceObect ReferenceObect { get;set; }

public static TestState Initial => new TestState
{
Id = 1,
Name = "empty_name",
Amount = 0.7m,
IsEnabled = true,
SubState = SubState.Initial
SubState = SubState.Initial,
ReferenceObect = new ReferenceObect()

};
}

Expand Down
36 changes: 24 additions & 12 deletions src/NetRx.Store.Tests/Store/StoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
using NetRx.Store.Tests.State.Effects;
using NetRx.Store.Tests.State.Reducers;
using NetRx.Store.Tests.State.TestStateActions;

using System;
using System.Collections.Generic;
using System.Linq;

using Xunit;

namespace NetRx.Store.Tests
Expand All @@ -24,18 +27,6 @@ public void WithState_Should_Throw_InvalidStateTypeException_When_Reference_stat
Assert.Contains(nameof(InvalidTypeState), exception.Message);
}

[Fact]
public void WithState_Should_Throw_InvalidStatePropertyTypeException_When_Reference_property_type_passed()
{
var exception = Record.Exception(
() => Store.Create().WithState(new InvalidPropertyTypeState(), new InvalidPropertyTypeStateReducer())
);

Assert.NotNull(exception);
Assert.IsType<InvalidStatePropertyTypeException>(exception);
Assert.Contains(nameof(InvalidPropertyTypeState.Model), exception.Message);
}

[Fact]
public void WithState_Should_Throw_InvalidStatePropertyTypeException_When_not_Immutable_collection_passed()
{
Expand Down Expand Up @@ -155,5 +146,26 @@ public void Dispatch_Should_call_Reducer_Reduce_and_Effect_Invoke_method_When_ca
Assert.Single(reducer.ReduceCalls.Where(c => c == typeof(SetItemsAction).FullName));
Assert.Equal(1, effect.CallCount);
}

[Fact]
public void Dispatch_Should_call_Reducer_Reduce_and_observable_should_invoke()
{
var reducer = new TestStateReducer();
var store = Store.Create()
.WithState(TestState.Initial, reducer);

var newObject = new ReferenceObect();
ReferenceObect selectedObect = null;

var disposable = store.Select<TestState, ReferenceObect>(f=> f.ReferenceObect)
.Subscribe(f => selectedObect = f);

store.Dispatch(new SetReferenceObjectAction(newObject));

disposable.Dispose();

Assert.Same(newObject, selectedObect);

}
}
}
11 changes: 6 additions & 5 deletions src/NetRx.Store/NetRx.Store.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>NetRx</RootNamespace>
<PackageId>NetRx.Store</PackageId>
<Version>2.0.0</Version>
<Version>2.1.0</Version>
<Authors>Vitalii Ilchenko</Authors>
<Description>State management for .Net projects, inspired by @ngrx/store</Description>
<RepositoryUrl>https://github.com/ilchenkob/NetRx.Store</RepositoryUrl>
Expand All @@ -17,13 +17,14 @@

<PropertyGroup>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.1" />
<PackageReference Include="System.Reactive" Version="4.1.0" />
<PackageReference Include="System.Collections.Immutable" Version="1.5.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
<PackageReference Include="System.Collections.Immutable" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
25 changes: 17 additions & 8 deletions src/NetRx.Store/Store/StateWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace NetRx.Store
internal sealed class StateWrapper
{
internal const string EnumerableFieldMarker = "#";
internal const string ReferenceFieldMarker = "*";

private static readonly Dictionary<string, Dictionary<string, Func<object, object>>> _gettersCache
= new Dictionary<string, Dictionary<string, Func<object, object>>>();
Expand Down Expand Up @@ -51,6 +52,8 @@ private void BuildGetters(Type type, string prefix)
{
var isString = p.PropertyType == stringType;
var isEnumerable = p.PropertyType.GetInterface(typeof(IEnumerable<>).FullName) != null && !isString;
var isReferenceType = p.PropertyType.IsClass && !isString || (p.PropertyType.IsInterface && p.PropertyType.GetInterface(typeof(IEnumerable<>).FullName) == null);

if (isEnumerable)
{
var hasImmutableInterface = p.PropertyType
Expand All @@ -65,10 +68,12 @@ private void BuildGetters(Type type, string prefix)
throw new InvalidStatePropertyTypeException(
$"'{p.Name}' cannot have reference type '{nonValueType.FullName}'. Should have 'struct' type");
}
else if (!p.PropertyType.IsValueType &&
else if (!isReferenceType &&
!p.PropertyType.IsValueType &&
!isString &&
!isEnumerable)
{

throw new InvalidStatePropertyTypeException($"'{p.Name}' property of {prefix} cannot have reference type. Should have 'struct' type");
}

Expand All @@ -80,21 +85,24 @@ private void BuildGetters(Type type, string prefix)
wrappedObjectParameter
);

string name = isEnumerable ? $"{prefix}.{p.Name}{EnumerableFieldMarker}" : $"{prefix}.{p.Name}";
string name = isEnumerable
? $"{prefix}.{p.Name}{EnumerableFieldMarker}"
: isReferenceType
? $"{prefix}.{p.Name}{ReferenceFieldMarker}"
: $"{prefix}.{p.Name}";

_gettersCache[OriginalTypeName].Add(name, getExpression.Compile());

if (!p.PropertyType.FullName.StartsWith("System.", StringComparison.InvariantCulture)
&& !isEnumerable)
{
BuildGetters(p.PropertyType, name);
}
}
}

public object Get(string name)
{
var key = _gettersCache[OriginalTypeName].ContainsKey(name) ? name : $"{name}{EnumerableFieldMarker}";
var key = _gettersCache[OriginalTypeName].ContainsKey(name)
? name
: _gettersCache[OriginalTypeName].ContainsKey($"{name}{EnumerableFieldMarker}")
? $"{name}{EnumerableFieldMarker}"
: $"{name}{ReferenceFieldMarker}";

var propNamePart = key.Substring(OriginalTypeName.Length);
var pointIndex = propNamePart.LastIndexOf('.');
Expand Down Expand Up @@ -124,6 +132,7 @@ public object Get(string name)
public bool HasGeter(string name) =>
_gettersCache[OriginalTypeName].ContainsKey(name) ||
_gettersCache[OriginalTypeName].ContainsKey($"{name}{EnumerableFieldMarker}") ||
_gettersCache[OriginalTypeName].ContainsKey($"{name}{ReferenceFieldMarker}") ||
string.Equals(name, OriginalTypeName);

public object Original { get; private set; }
Expand Down
8 changes: 6 additions & 2 deletions src/NetRx.Store/Store/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,14 @@ private void DetectChanges(List<(string, StateWrapper)> modifiedStates)

var hasChanges = field.EndsWith(StateWrapper.EnumerableFieldMarker, StringComparison.InvariantCulture)
? !Equals(newValue?.GetHashCode(), prevValue?.GetHashCode())
: !Equals(newValue, prevValue);
: field.EndsWith(StateWrapper.ReferenceFieldMarker, StringComparison.InvariantCulture)
? !ReferenceEquals(newValue,prevValue)
: !Equals(newValue, prevValue);
if (hasChanges)
{
NotifySubscribers(currState, field.Replace(StateWrapper.EnumerableFieldMarker, string.Empty));
NotifySubscribers(currState,
field.Replace(StateWrapper.EnumerableFieldMarker, string.Empty)
.Replace(StateWrapper.ReferenceFieldMarker, string.Empty));
}
}
}
Expand Down