Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
beyaz committed Feb 3, 2024
1 parent 2316a30 commit a363af7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ const RootNode = '$RootNode';
const ClientTasks = '$ClientTasks';
const SyncId = '$SyncId';
const DotNetState = '$State';
const HasComponentDidMountMethod = '$HasComponentDidMountMethod';
const ComponentDidMountMethod = '$ComponentDidMountMethod';
const DotNetComponentUniqueIdentifier = '$DotNetComponentUniqueIdentifier';
const DotNetComponentUniqueIdentifiers = '$DotNetComponentUniqueIdentifiers';

const ON_COMPONENT_DESTROY = '$ON_COMPONENT_DESTROY';
const CUSTOM_EVENT_LISTENER_MAP = '$CUSTOM_EVENT_LISTENER_MAP';
const DotNetProperties = 'DotNetProperties';
Expand Down Expand Up @@ -1848,24 +1847,6 @@ function DefineComponent(componentDeclaration)
initialState[SyncId] = ShouldBeNumber(props[SyncId]);
}

// old way todo: check and remove
//initialState[DotNetState] = NotNull(props.$jsonNode[DotNetState]);
//initialState[SyncId] = ShouldBeNumber(props[SyncId]);
//initialState[RootNode] = props.$jsonNode[RootNode];
//initialState[DotNetProperties] = NotNull(props.$jsonNode[DotNetProperties]);
//initialState[DotNetComponentUniqueIdentifier] = NotNull(props.$jsonNode[DotNetComponentUniqueIdentifier]);

//if (props.$jsonNode[HasComponentDidMountMethod]) {
// initialState[HasComponentDidMountMethod] = props.$jsonNode[HasComponentDidMountMethod];
//}

//if (props.$jsonNode[ClientTasks]) {
// initialState[ClientTasks] = props.$jsonNode[ClientTasks];
//}

//initialState.$CachedMethods = props.$jsonNode.$CachedMethods;


instance.state = initialState;

initialState[DotNetTypeOfReactComponent] = instance[DotNetTypeOfReactComponent] = dotNetTypeOfReactComponent;
Expand All @@ -1892,8 +1873,8 @@ function DefineComponent(componentDeclaration)

function HandleHasComponentDidMount(isDirectCall)
{
const hasComponentDidMountMethod = component.state[HasComponentDidMountMethod];
if (hasComponentDidMountMethod !== true)
const componentDidMountMethod = component.state[ComponentDidMountMethod];
if (componentDidMountMethod === undefined || componentDidMountMethod === null)
{
if (isDirectCall !== true)
{
Expand All @@ -1912,7 +1893,7 @@ function DefineComponent(componentDeclaration)

const clientTasks = newState[ClientTasks];

newState[HasComponentDidMountMethod] = null;
newState[ComponentDidMountMethod] = null;
newState[ClientTasks] = null;

function stateCallback()
Expand All @@ -1930,13 +1911,13 @@ function DefineComponent(componentDeclaration)

const partialState = {};

partialState[HasComponentDidMountMethod] = null;
partialState[ComponentDidMountMethod] = null;

function stateCallBack()
{
const actionArguments = {
component: component,
remoteMethodName: 'componentDidMount',
remoteMethodName: componentDidMountMethod,
remoteMethodArguments: []
};
StartAction(actionArguments);
Expand Down
45 changes: 28 additions & 17 deletions ReactWithDotNet/ElementSerializer.ToJsonMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,23 +304,23 @@ public static async Task<IReadOnlyJsonMap> ToJsonMap(this Element element, Eleme
node.Stopwatch.Start();
}

FunctionalComponent functionalComponent = null;

if (node.IsFunctionalComponent is null)
{
functionalComponent = reactStatefulComponent as FunctionalComponent;
if (functionalComponent is not null)
node.FunctionalComponent = reactStatefulComponent as FunctionalComponent;
if (node.FunctionalComponent is not null)
{
context.FunctionalComponentStack ??= new();

context.FunctionalComponentStack.Push(functionalComponent);
context.FunctionalComponentStack.Push(node.FunctionalComponent);

node.IsFunctionalComponent = true;
}
else
{
node.IsFunctionalComponent = false;
}


}

var stateTree = context.StateTree;
Expand Down Expand Up @@ -421,19 +421,19 @@ public static async Task<IReadOnlyJsonMap> ToJsonMap(this Element element, Eleme

node.DotNetComponentRootElement = await reactStatefulComponent.InvokeRender();

if (functionalComponent is not null)
if (node.IsFunctionalComponent == true)
{
if (functionalComponent.Constructor is not null)
if (node.FunctionalComponent.Constructor is not null)
{
await functionalComponent.Constructor.Invoke();
await node.FunctionalComponent.Constructor.Invoke();

// recalculate scope because maybe fields have changed
functionalComponent.Scope = SerializationHelperForCompilerGeneratedClasss.Serialize(functionalComponent.Constructor.Target);
node.FunctionalComponent.Scope = SerializationHelperForCompilerGeneratedClasss.Serialize(node.FunctionalComponent.Constructor.Target);
}
else
{
// recalculate scope because maybe fields have changed
functionalComponent.Scope = SerializationHelperForCompilerGeneratedClasss.Serialize(functionalComponent._target);
node.FunctionalComponent.Scope = SerializationHelperForCompilerGeneratedClasss.Serialize(node.FunctionalComponent._target);
}
}

Expand Down Expand Up @@ -525,9 +525,17 @@ public static async Task<IReadOnlyJsonMap> ToJsonMap(this Element element, Eleme
map.Add("$ReactAttributeNames", reactAttributeNames);
}

if (typeInfo.HasComponentDidMountMethod)
if (typeInfo.ComponentDidMountMethod is not null)
{
map.Add(___HasComponentDidMountMethod___, true);
map.Add(___ComponentDidMountMethod___, typeInfo.ComponentDidMountMethod);
}

if (node.IsFunctionalComponent == true)
{
if (node.FunctionalComponent.ComponentDidMount is not null)
{
map.Add(___ComponentDidMountMethod___, node.FunctionalComponent.ComponentDidMount.Method.GetNameWithToken());
}
}

if (reactStatefulComponent._client is not null && reactStatefulComponent._client.TaskList.Count > 0)
Expand Down Expand Up @@ -1103,26 +1111,28 @@ internal static TypeInfo GetTypeInfo(Type type)

GetPropertyValueForSerializeToClient = getPropertyValueForSerializeToClientFunc,

HasComponentDidMountMethod = HasComponentDidMountMethod(type)
ComponentDidMountMethod = GetComponentDidMountMethod(type)
};

TypeInfoMap.TryAdd(type, typeInfo);
}

return typeInfo;

static bool HasComponentDidMountMethod(Type componentType)


static string GetComponentDidMountMethod(Type componentType)
{
var didMountMethodInfo = componentType.FindMethod("componentDidMount", BindingFlags.NonPublic | BindingFlags.Instance);
if (didMountMethodInfo != null)
{
if (didMountMethodInfo.DeclaringType != typeof(ReactComponentBase))
{
return true;
return didMountMethodInfo.GetNameWithToken();
}
}

return false;
return null;
}
}

Expand Down Expand Up @@ -1435,6 +1445,7 @@ sealed class Node

public Node Parent { get; set; }
public Stopwatch Stopwatch { get; set; }
public FunctionalComponent FunctionalComponent;

public bool? IsFunctionalComponent;
}
Expand All @@ -1448,7 +1459,7 @@ internal sealed class TypeInfo
public IReadOnlyList<MethodInfo> ParameterizedCacheableMethodInfoList { get; init; }
public IReadOnlyList<PropertyAccessInfo> ReactAttributedPropertiesOfType { get; init; }
public PropertyAccessInfo StateProperty { get; init; }
public bool HasComponentDidMountMethod { get; init; }
public string ComponentDidMountMethod { get; init; }
}
}

Expand Down
2 changes: 1 addition & 1 deletion ReactWithDotNet/ElementSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ sealed class ElementSerializerContext

static partial class ElementSerializer
{
const string ___HasComponentDidMountMethod___ = "$HasComponentDidMountMethod";
const string ___ComponentDidMountMethod___ = "$ComponentDidMountMethod";
const string ___RootNode___ = "$RootNode";
const string ___Type___ = "$Type";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ const RootNode = '$RootNode';
const ClientTasks = '$ClientTasks';
const SyncId = '$SyncId';
const DotNetState = '$State';
const HasComponentDidMountMethod = '$HasComponentDidMountMethod';
const ComponentDidMountMethod = '$ComponentDidMountMethod';
const DotNetComponentUniqueIdentifier = '$DotNetComponentUniqueIdentifier';
const DotNetComponentUniqueIdentifiers = '$DotNetComponentUniqueIdentifiers';

const ON_COMPONENT_DESTROY = '$ON_COMPONENT_DESTROY';
const CUSTOM_EVENT_LISTENER_MAP = '$CUSTOM_EVENT_LISTENER_MAP';
const DotNetProperties = 'DotNetProperties';
Expand Down Expand Up @@ -1874,8 +1873,8 @@ function DefineComponent(componentDeclaration)

function HandleHasComponentDidMount(isDirectCall)
{
const hasComponentDidMountMethod = component.state[HasComponentDidMountMethod];
if (hasComponentDidMountMethod !== true)
const componentDidMountMethod = component.state[ComponentDidMountMethod];
if (componentDidMountMethod === undefined || componentDidMountMethod === null)
{
if (isDirectCall !== true)
{
Expand All @@ -1894,7 +1893,7 @@ function DefineComponent(componentDeclaration)

const clientTasks = newState[ClientTasks];

newState[HasComponentDidMountMethod] = null;
newState[ComponentDidMountMethod] = null;
newState[ClientTasks] = null;

function stateCallback()
Expand All @@ -1912,13 +1911,13 @@ function DefineComponent(componentDeclaration)

const partialState = {};

partialState[HasComponentDidMountMethod] = null;
partialState[ComponentDidMountMethod] = null;

function stateCallBack()
{
const actionArguments = {
component: component,
remoteMethodName: 'componentDidMount',
remoteMethodName: componentDidMountMethod,
remoteMethodArguments: []
};
StartAction(actionArguments);
Expand Down

0 comments on commit a363af7

Please sign in to comment.