Negotiate exact hot-body transport demand
This commit is contained in:
parent
693df0901d
commit
3e223ceecd
16 changed files with 872 additions and 68 deletions
|
|
@ -3,6 +3,7 @@ using System.Collections;
|
|||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
|
@ -1002,6 +1003,27 @@ namespace GameCult.Mesh
|
|||
return Watch().Subscribe(onNext);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Projects one typed document field into a stable reactive state pointer. The expression is
|
||||
/// structural metadata: only member access and constant collection indices are accepted.
|
||||
/// </summary>
|
||||
public CultMeshStatePointer<TValue> Field<TValue>(Expression<Func<TDocument, TValue>> field)
|
||||
{
|
||||
if (field == null) throw new ArgumentNullException(nameof(field));
|
||||
var path = CultMeshFieldPath.Parse(field.Body, field.Parameters[0]);
|
||||
var read = field.Compile();
|
||||
return new CultMeshStatePointer<TValue>(
|
||||
DocumentId + "#" + path,
|
||||
async context => read(await Feed
|
||||
.SnapshotAsync(CultMeshDocumentQueryParameters.Empty, context)
|
||||
.ConfigureAwait(false)),
|
||||
context => Feed
|
||||
.Watch(CultMeshDocumentQueryParameters.Empty, context)
|
||||
.Select(read),
|
||||
RouteHint,
|
||||
Sources);
|
||||
}
|
||||
|
||||
/// <summary>Replaces the underlying document when this handle is backed by mutable state.</summary>
|
||||
public Task ReplaceAsync(TDocument value)
|
||||
{
|
||||
|
|
@ -1122,6 +1144,62 @@ namespace GameCult.Mesh
|
|||
payload);
|
||||
}
|
||||
|
||||
private static class CultMeshFieldPath
|
||||
{
|
||||
public static string Parse(Expression expression, ParameterExpression root)
|
||||
{
|
||||
var segments = new Stack<string>();
|
||||
while (expression != root)
|
||||
{
|
||||
if (expression is MemberExpression member)
|
||||
{
|
||||
segments.Push(member.Member.Name);
|
||||
expression = member.Expression
|
||||
?? throw Invalid();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (expression is MethodCallExpression call &&
|
||||
call.Method.Name == "get_Item" &&
|
||||
call.Object != null &&
|
||||
call.Arguments.Count == 1)
|
||||
{
|
||||
segments.Push("[" + ConstantIndex(call.Arguments[0]) + "]");
|
||||
expression = call.Object;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (expression is BinaryExpression binary && binary.NodeType == ExpressionType.ArrayIndex)
|
||||
{
|
||||
segments.Push("[" + ConstantIndex(binary.Right) + "]");
|
||||
expression = binary.Left;
|
||||
continue;
|
||||
}
|
||||
|
||||
throw Invalid();
|
||||
}
|
||||
|
||||
if (segments.Count == 0) throw Invalid();
|
||||
var path = string.Empty;
|
||||
foreach (var segment in segments)
|
||||
path += segment.StartsWith("[", StringComparison.Ordinal) || path.Length == 0
|
||||
? segment
|
||||
: "." + segment;
|
||||
return path;
|
||||
}
|
||||
|
||||
private static string ConstantIndex(Expression expression)
|
||||
{
|
||||
if (expression is ConstantExpression constant && constant.Value != null)
|
||||
return Convert.ToString(constant.Value, CultureInfo.InvariantCulture)!;
|
||||
throw Invalid();
|
||||
}
|
||||
|
||||
private static ArgumentException Invalid() => new(
|
||||
"CultMesh field selectors may contain only member access and constant collection indices.",
|
||||
"field");
|
||||
}
|
||||
|
||||
internal static TTarget ConvertDocument<TSource, TTarget>(TSource document)
|
||||
where TSource : class
|
||||
where TTarget : class
|
||||
|
|
|
|||
Loading…
Reference in a new issue