Skip to content

Commit 2542767

Browse files
committed
Generate calls to new property hook overloads
1 parent 4350cef commit 2542767

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

src/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.Execute.cs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -683,19 +683,15 @@ public static MemberDeclarationSyntax GetPropertySyntax(PropertyInfo propertyInf
683683
string name => IdentifierName(name)
684684
};
685685

686-
if (propertyInfo.NotifyPropertyChangedRecipients)
687-
{
688-
// If broadcasting changes are required, also store the old value.
689-
// This code generates a statement as follows:
690-
//
691-
// <PROPERTY_TYPE> __oldValue = <FIELD_EXPRESSIONS>;
692-
setterStatements.Add(
693-
LocalDeclarationStatement(
694-
VariableDeclaration(propertyType)
695-
.AddVariables(
696-
VariableDeclarator(Identifier("__oldValue"))
697-
.WithInitializer(EqualsValueClause(fieldExpression)))));
698-
}
686+
// Store the old value for later. This code generates a statement as follows:
687+
//
688+
// <PROPERTY_TYPE> __oldValue = <FIELD_EXPRESSIONS>;
689+
setterStatements.Add(
690+
LocalDeclarationStatement(
691+
VariableDeclaration(propertyType)
692+
.AddVariables(
693+
VariableDeclarator(Identifier("__oldValue"))
694+
.WithInitializer(EqualsValueClause(fieldExpression)))));
699695

700696
// Add the OnPropertyChanging() call first:
701697
//
@@ -705,6 +701,14 @@ public static MemberDeclarationSyntax GetPropertySyntax(PropertyInfo propertyInf
705701
InvocationExpression(IdentifierName($"On{propertyInfo.PropertyName}Changing"))
706702
.AddArgumentListArguments(Argument(IdentifierName("value")))));
707703

704+
// Also call the overload after that:
705+
//
706+
// On<PROPERTY_NAME>Changing(__oldValue, value);
707+
setterStatements.Add(
708+
ExpressionStatement(
709+
InvocationExpression(IdentifierName($"On{propertyInfo.PropertyName}Changing"))
710+
.AddArgumentListArguments(Argument(IdentifierName("__oldValue")), Argument(IdentifierName("value")))));
711+
708712
// Gather the statements to notify dependent properties
709713
foreach (string propertyName in propertyInfo.PropertyChangingNames)
710714
{
@@ -751,6 +755,14 @@ public static MemberDeclarationSyntax GetPropertySyntax(PropertyInfo propertyInf
751755
InvocationExpression(IdentifierName($"On{propertyInfo.PropertyName}Changed"))
752756
.AddArgumentListArguments(Argument(IdentifierName("value")))));
753757

758+
// Do the same for the overload, as above:
759+
//
760+
// On<PROPERTY_NAME>Changed(__oldValue, value);
761+
setterStatements.Add(
762+
ExpressionStatement(
763+
InvocationExpression(IdentifierName($"On{propertyInfo.PropertyName}Changed"))
764+
.AddArgumentListArguments(Argument(IdentifierName("__oldValue")), Argument(IdentifierName("value")))));
765+
754766
// Gather the statements to notify dependent properties
755767
foreach (string propertyName in propertyInfo.PropertyChangedNames)
756768
{

0 commit comments

Comments
 (0)