Skip to content

Commit

Permalink
Merge pull request #2 from skclusive/develop
Browse files Browse the repository at this point in the history
5.0.0 from Develop
  • Loading branch information
skclusive authored Nov 10, 2020
2 parents cae8ce4 + 1d92006 commit a7468e7
Show file tree
Hide file tree
Showing 66 changed files with 3,373 additions and 1,739 deletions.
60 changes: 26 additions & 34 deletions Skclusive.Mobx.StateTree.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{B0D5652F-FD5D-4774-90B0-0D76CBA0E029}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StateTree", "src\StateTree.csproj", "{A0230E1F-D1EC-49DB-A685-23EB44B0282C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StateTree", "src\StateTree\StateTree.csproj", "{7E79DA7C-1E75-46A2-A638-6F616893424B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{8835934B-6723-4743-81EF-480FF1783619}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StateTree.Tests", "test\StateTree.Tests\StateTree.Tests.csproj", "{4963BE55-EDF6-414B-BF6E-F830A8513DF9}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StateTree.Tests", "test\StateTree.Tests.csproj", "{6F51EF2B-1BDB-4656-B30D-B8EFE5EFE139}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -24,33 +20,29 @@ Global
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7E79DA7C-1E75-46A2-A638-6F616893424B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7E79DA7C-1E75-46A2-A638-6F616893424B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7E79DA7C-1E75-46A2-A638-6F616893424B}.Debug|x64.ActiveCfg = Debug|Any CPU
{7E79DA7C-1E75-46A2-A638-6F616893424B}.Debug|x64.Build.0 = Debug|Any CPU
{7E79DA7C-1E75-46A2-A638-6F616893424B}.Debug|x86.ActiveCfg = Debug|Any CPU
{7E79DA7C-1E75-46A2-A638-6F616893424B}.Debug|x86.Build.0 = Debug|Any CPU
{7E79DA7C-1E75-46A2-A638-6F616893424B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7E79DA7C-1E75-46A2-A638-6F616893424B}.Release|Any CPU.Build.0 = Release|Any CPU
{7E79DA7C-1E75-46A2-A638-6F616893424B}.Release|x64.ActiveCfg = Release|Any CPU
{7E79DA7C-1E75-46A2-A638-6F616893424B}.Release|x64.Build.0 = Release|Any CPU
{7E79DA7C-1E75-46A2-A638-6F616893424B}.Release|x86.ActiveCfg = Release|Any CPU
{7E79DA7C-1E75-46A2-A638-6F616893424B}.Release|x86.Build.0 = Release|Any CPU
{4963BE55-EDF6-414B-BF6E-F830A8513DF9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4963BE55-EDF6-414B-BF6E-F830A8513DF9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4963BE55-EDF6-414B-BF6E-F830A8513DF9}.Debug|x64.ActiveCfg = Debug|Any CPU
{4963BE55-EDF6-414B-BF6E-F830A8513DF9}.Debug|x64.Build.0 = Debug|Any CPU
{4963BE55-EDF6-414B-BF6E-F830A8513DF9}.Debug|x86.ActiveCfg = Debug|Any CPU
{4963BE55-EDF6-414B-BF6E-F830A8513DF9}.Debug|x86.Build.0 = Debug|Any CPU
{4963BE55-EDF6-414B-BF6E-F830A8513DF9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4963BE55-EDF6-414B-BF6E-F830A8513DF9}.Release|Any CPU.Build.0 = Release|Any CPU
{4963BE55-EDF6-414B-BF6E-F830A8513DF9}.Release|x64.ActiveCfg = Release|Any CPU
{4963BE55-EDF6-414B-BF6E-F830A8513DF9}.Release|x64.Build.0 = Release|Any CPU
{4963BE55-EDF6-414B-BF6E-F830A8513DF9}.Release|x86.ActiveCfg = Release|Any CPU
{4963BE55-EDF6-414B-BF6E-F830A8513DF9}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{7E79DA7C-1E75-46A2-A638-6F616893424B} = {B0D5652F-FD5D-4774-90B0-0D76CBA0E029}
{4963BE55-EDF6-414B-BF6E-F830A8513DF9} = {8835934B-6723-4743-81EF-480FF1783619}
{A0230E1F-D1EC-49DB-A685-23EB44B0282C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A0230E1F-D1EC-49DB-A685-23EB44B0282C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A0230E1F-D1EC-49DB-A685-23EB44B0282C}.Debug|x64.ActiveCfg = Debug|Any CPU
{A0230E1F-D1EC-49DB-A685-23EB44B0282C}.Debug|x64.Build.0 = Debug|Any CPU
{A0230E1F-D1EC-49DB-A685-23EB44B0282C}.Debug|x86.ActiveCfg = Debug|Any CPU
{A0230E1F-D1EC-49DB-A685-23EB44B0282C}.Debug|x86.Build.0 = Debug|Any CPU
{A0230E1F-D1EC-49DB-A685-23EB44B0282C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A0230E1F-D1EC-49DB-A685-23EB44B0282C}.Release|Any CPU.Build.0 = Release|Any CPU
{A0230E1F-D1EC-49DB-A685-23EB44B0282C}.Release|x64.ActiveCfg = Release|Any CPU
{A0230E1F-D1EC-49DB-A685-23EB44B0282C}.Release|x64.Build.0 = Release|Any CPU
{A0230E1F-D1EC-49DB-A685-23EB44B0282C}.Release|x86.ActiveCfg = Release|Any CPU
{A0230E1F-D1EC-49DB-A685-23EB44B0282C}.Release|x86.Build.0 = Release|Any CPU
{6F51EF2B-1BDB-4656-B30D-B8EFE5EFE139}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6F51EF2B-1BDB-4656-B30D-B8EFE5EFE139}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6F51EF2B-1BDB-4656-B30D-B8EFE5EFE139}.Debug|x64.ActiveCfg = Debug|Any CPU
{6F51EF2B-1BDB-4656-B30D-B8EFE5EFE139}.Debug|x64.Build.0 = Debug|Any CPU
{6F51EF2B-1BDB-4656-B30D-B8EFE5EFE139}.Debug|x86.ActiveCfg = Debug|Any CPU
{6F51EF2B-1BDB-4656-B30D-B8EFE5EFE139}.Debug|x86.Build.0 = Debug|Any CPU
{6F51EF2B-1BDB-4656-B30D-B8EFE5EFE139}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6F51EF2B-1BDB-4656-B30D-B8EFE5EFE139}.Release|Any CPU.Build.0 = Release|Any CPU
{6F51EF2B-1BDB-4656-B30D-B8EFE5EFE139}.Release|x64.ActiveCfg = Release|Any CPU
{6F51EF2B-1BDB-4656-B30D-B8EFE5EFE139}.Release|x64.Build.0 = Release|Any CPU
{6F51EF2B-1BDB-4656-B30D-B8EFE5EFE139}.Release|x86.ActiveCfg = Release|Any CPU
{6F51EF2B-1BDB-4656-B30D-B8EFE5EFE139}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
11 changes: 7 additions & 4 deletions src/StateTree/StateTree.csproj → src/StateTree.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<VersionPrefix>2.0.0</VersionPrefix>
<VersionPrefix>5.0.0</VersionPrefix>
<VersionSuffix>$(VersionSuffix)</VersionSuffix>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<AssemblyName>Skclusive.Mobx.StateTree</AssemblyName>
<PackageId>Skclusive.Mobx.StateTree</PackageId>
<RootNamespace>Skclusive.Mobx.StateTree</RootNamespace>
Expand All @@ -18,7 +18,10 @@
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="System.Dynamic.Runtime" Version="4.3.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Skclusive.Mobx.Observable" Version="2.0.0" />
<ItemGroup>
<PackageReference Include="Skclusive.Mobx.Observable" Version="5.0.0" />
</ItemGroup>
<!-- <ItemGroup>
<ProjectReference Include="..\..\Skclusive.Mobx.Observable\src\Observable.csproj" Version="5.0.0" />
</ItemGroup> -->
</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Skclusive.Mobx.StateTree
{
public enum Hooks
public enum Hook
{
AfterCreate,

Expand Down
22 changes: 20 additions & 2 deletions src/StateTree/Action/StateTreeAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,29 @@ public static IDisposable AddMiddleware(IStateTreeNode target, Action<IMiddlewar
return node.AddMiddleware(handler, includeHooks);
}

public static Func<object[], object> CreateHookInvoker(object target, string name, Func<object[], object> action)
{
return (object[] arguments) =>
{
IStateTreeNode node = target.GetStateTree();

if (node != null)
{
return Actions.RunInAction(name, action, new object[] { target }.Concat(arguments).ToArray());
}

throw new Exception($"Target does have associated node {target}");
};
}


public static Func<object[], object> CreateActionInvoker(object target, string name, Func<object[], object> action)
{
return (object[] arguments) =>
{
if (NodeCache.TryGetValue(target, out IStateTreeNode node))
IStateTreeNode node = target.GetStateTree();

if (node != null)
{
var id = GetNextActionId();

Expand Down Expand Up @@ -183,7 +201,7 @@ object InvokeHandler()
}
else
{
if (Enum.TryParse(call.Name, out Hooks hook))
if (Enum.TryParse(call.Name, out Hook hook))
{
return RunMiddlewares(call);
}
Expand Down
3 changes: 2 additions & 1 deletion src/StateTree/Combine/FrozenType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public override INode Instantiate(INode parent, string subpath, IEnvironment env

protected override IValidationError[] IsValidSnapshot(object value, IContextEntry[] context)
{
// TODO: to fix condition
if (Math.Max(0, 1) == 0) // !isSerializable(value)
{
return new IValidationError[]
Expand All @@ -35,7 +36,7 @@ protected override IValidationError[] IsValidSnapshot(object value, IContextEntr
};
}

return new IValidationError[] { };
return Array.Empty<IValidationError>();
}
}
}
12 changes: 12 additions & 0 deletions src/StateTree/Combine/ILateType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Skclusive.Mobx.Observable;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace Skclusive.Mobx.StateTree
{
public interface ILateType : IType
{
IType SubType { get; }
}
}
12 changes: 12 additions & 0 deletions src/StateTree/Combine/IOptionalType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Skclusive.Mobx.Observable;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace Skclusive.Mobx.StateTree
{
public interface IOptionalType : IType
{
IType SubType { get; }
}
}
12 changes: 12 additions & 0 deletions src/StateTree/Combine/IUnionType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Skclusive.Mobx.Observable;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace Skclusive.Mobx.StateTree
{
public interface IUnionType
{
IType[] SubTypes { get; }
}
}
53 changes: 37 additions & 16 deletions src/StateTree/Combine/IdentifierType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Skclusive.Mobx.StateTree
{
public class IdentifierType<T> : Type<T, T>
public class IdentifierType<T> : Type<T, T>, IIdentifierType<T>
{
public IdentifierType(IType<T, T> type) : base($"identifier(${type.Name})")
public IdentifierType(string name, IType<T, T> type) : base(name)
{
_Type = type;

Expand All @@ -17,24 +17,24 @@ public IdentifierType(IType<T, T> type) : base($"identifier(${type.Name})")

private IType<T, T> _Type { set; get; }

public override string Describe => $"identifier({_Type.Describe})";
public override string Describe => Name;

public override INode Instantiate(INode parent, string subpath, IEnvironment environment, object initialValue)
{
if (parent == null || !parent.StoredValue.IsStateTreeNode())
if (parent == null || !(parent.Type.Flags == TypeFlags.Object))
{
throw new InvalidOperationException($"Identifier types can only be instantiated as direct child of a model type");
throw new InvalidOperationException($"{Name} can only be instantiated as direct child of a model type");
}

if (parent is ObjectNode oparent)
{
if (!String.IsNullOrWhiteSpace(oparent.IdentifierAttribute))
{
throw new InvalidOperationException($"Cannot define property '{subpath}' as object identifier, property '{oparent.IdentifierAttribute}' is already defined as identifier property");
}
//if (parent is ObjectNode oparent)
//{
// if (!String.IsNullOrWhiteSpace(oparent.IdentifierAttribute))
// {
// throw new InvalidOperationException($"Cannot define property '{subpath}' as object identifier, property '{oparent.IdentifierAttribute}' is already defined as identifier property");
// }

oparent.IdentifierAttribute = subpath;
}
// oparent.IdentifierAttribute = subpath;
//}

return this.CreateNode(parent as ObjectNode, subpath, environment, initialValue);
}
Expand All @@ -43,15 +43,15 @@ public override INode Reconcile(INode current, object newValue)
{
if (current.StoredValue != newValue)
{
throw new InvalidOperationException($"Tried to change identifier from '{current.StoredValue}' to '{newValue}'. Changing identifiers is not allowed.");
throw new InvalidOperationException($"Tried to change {Name} from '{current.StoredValue}' to '{newValue}'. Changing identifiers is not allowed.");
}

return current;
}

protected override IValidationError[] IsValidSnapshot(object value, IContextEntry[] context)
{
if (value == null || value is string || value is int)
if (value == null || value.GetType() == typeof (T))
{
return _Type.Validate(value, context);
}
Expand All @@ -64,9 +64,30 @@ protected override IValidationError[] IsValidSnapshot(object value, IContextEntr

Value = value,

Message = $"Value is not a valid identifier, which is a string or a number"
Message = $"Value is not a valid {Name}, which should be {typeof(T).Name}"
}
};
}
}

public class StringIdentifierType : IdentifierType<string>
{
public StringIdentifierType() : base("identifier", new StringType())
{
}
}

public class IntIdentifierType : IdentifierType<int>
{
public IntIdentifierType() : base("identifierInt", new IntType())
{
}
}

public class GuidIdentifierType : IdentifierType<Guid>
{
public GuidIdentifierType() : base("identifierGuid", new GuidType())
{
}
}
}
Loading

0 comments on commit a7468e7

Please sign in to comment.