Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
beyaz committed Feb 4, 2024
1 parent a8fdb15 commit ad8e46d
Showing 1 changed file with 28 additions and 34 deletions.
62 changes: 28 additions & 34 deletions ReactWithDotNet/ElementSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,32 @@ Task<IReadOnlyJsonMap> convertToReactNode(object item)

return propertyValue;
}
static int? TryFindHandlerComponentUniqueIdentifier(ElementSerializerContext context, object handlerDelegateTarget)
{
if (handlerDelegateTarget is ReactComponentBase target)
{
return target.ComponentUniqueIdentifier;
}

if (context.FunctionalComponentStack?.Count > 0)
{
foreach (var item in context.FunctionalComponentStack)
{
if (item.CompilerGeneratedType == handlerDelegateTarget.GetType())
{
return item.ComponentUniqueIdentifier;
}

var scope = handlerDelegateTarget.GetType().GetFields().FirstOrDefault(f => f.FieldType == typeof(Scope))?.GetValue(handlerDelegateTarget);
if (scope is not null && scope == item)
{
return item.ComponentUniqueIdentifier;
}
}
}

return null;
}
static async Task<object> GetPropertyValueOfHtmlElement(ElementSerializerContext context, HtmlElement instance, HtmlElement.PropertyValueNode propertyValueNode)
{
var propertyDefinition = propertyValueNode.propertyDefinition;
Expand Down Expand Up @@ -558,42 +583,11 @@ static async Task<object> GetPropertyValueOfHtmlElement(ElementSerializerContext
{
throw HandlerMethodShouldBelongToReactComponent(propertyDefinition.name, null);
}

int? handlerComponentUniqueIdentifier = null;

if (handlerDelegateTarget is ReactComponentBase target)
var handlerComponentUniqueIdentifier = TryFindHandlerComponentUniqueIdentifier(context, handlerDelegateTarget);
if (handlerComponentUniqueIdentifier is null)
{
handlerComponentUniqueIdentifier = target.ComponentUniqueIdentifier;
}
else
{
if (context.FunctionalComponentStack?.Count > 0)
{
foreach (var item in context.FunctionalComponentStack)
{
if (item.CompilerGeneratedType == handlerDelegateTarget.GetType())
{
handlerComponentUniqueIdentifier = item.ComponentUniqueIdentifier;
break;
}

var scope = handlerDelegateTarget.GetType().GetFields().FirstOrDefault(f => f.FieldType == typeof(Scope))?.GetValue(handlerDelegateTarget);
if (scope is not null && scope == item)
{
handlerComponentUniqueIdentifier = item.ComponentUniqueIdentifier;
break;
}
}

if (handlerComponentUniqueIdentifier is null)
{
throw HandlerMethodShouldBelongToReactComponent(propertyDefinition.name, handlerDelegateTarget);
}
}
else
{
throw HandlerMethodShouldBelongToReactComponent(propertyDefinition.name, handlerDelegateTarget);
}
throw HandlerMethodShouldBelongToReactComponent(propertyDefinition.name, handlerDelegateTarget);
}

var remoteMethodInfo = new RemoteMethodInfo
Expand Down

0 comments on commit ad8e46d

Please sign in to comment.