Skip to content

Commit

Permalink
ICanNotifyPropertyChanged remaned to ISelectiveNotifyPropertyChanged
Browse files Browse the repository at this point in the history
ICanNotifyMethodChanged remaned to ISelectiveNotifyMethodChanged
Code for the support of selective subscriptions to PropertyChanged and MethodChanged events was commented out
  • Loading branch information
IgorBuchelnikov committed Sep 15, 2021
1 parent 3cfddda commit 0179b45
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 82 deletions.
76 changes: 38 additions & 38 deletions src/ObservableComputations.Test/ExpressionWatcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,35 +96,35 @@ protected bool updatePropertyValue<T>(ref T field, T value, [CallerMemberName] s
public event EventHandler<MethodChangedEventArgs> MethodChanged;
}

public class ItemWithConstantPropertyAndMethod : Item, ICanNotifyPropertyChanged, ICanNotifyMethodChanged
{
public int ConstantProperty => 1;
public int ConstantMethod() => 1;
//public class ItemWithConstantPropertyAndMethod : Item, ISelectiveNotifyPropertyChanged, ISelectiveNotifyMethodChanged
//{
// public int ConstantProperty => 1;
// public int ConstantMethod() => 1;

public void RaiseConstantsChanged()
{
onPropertyChanged(nameof(ConstantProperty));
RaiseMethodChanged(nameof(ConstantMethod), objects => true);
}
// public void RaiseConstantsChanged()
// {
// onPropertyChanged(nameof(ConstantProperty));
// RaiseMethodChanged(nameof(ConstantMethod), objects => true);
// }

#region Implementation of ICanNotifyPropertyChanged
// #region Implementation of ISelectiveNotifyPropertyChanged

public bool CanNotifyPropertyChanged(string propertyName, IComputing computing)
{
return propertyName != nameof(ConstantProperty);
}
// public bool CanNotifyPropertyChanged(string propertyName, IComputing computing)
// {
// return propertyName != nameof(ConstantProperty);
// }

#endregion
// #endregion

#region Implementation of ICanNotifyMethodChanged
// #region Implementation of ISelectiveNotifyMethodChanged

public bool CanNotifyMethodChanged(string methodName, int argumentsCount, IComputing computing)
{
return methodName != nameof(ConstantMethod);
}
// public bool CanNotifyMethodChanged(string methodName, int argumentsCount, IComputing computing)
// {
// return methodName != nameof(ConstantMethod);
// }

#endregion
}
// #endregion
//}

[Test]
public void TestRaiseValueChanged1()
Expand Down Expand Up @@ -419,22 +419,22 @@ public void TestRaiseValueChanged13()
expressionWatcher.Dispose();
}

[Test]
public void TestItemWithConstantPropertyAndMethod()
{
int raised = 0;
ItemWithConstantPropertyAndMethod item = new ItemWithConstantPropertyAndMethod();
Expression<Func<string>> expression = () => item.Num + item.ConstantMethod() + item.ConstantProperty;
ExpressionWatcher expressionWatcher = new ExpressionWatcher(null,
ExpressionWatcher.GetExpressionInfo(expression));
expressionWatcher.ValueChanged = (ew, sender, eventArgs) => { raised++; };
item.Num = item.Num + 1;
Assert.AreEqual(raised, 1);
raised = 0;
item.RaiseConstantsChanged();
Assert.AreEqual(raised, 0);
expressionWatcher.Dispose();
}
//[Test]
//public void TestItemWithConstantPropertyAndMethod()
//{
// int raised = 0;
// ItemWithConstantPropertyAndMethod item = new ItemWithConstantPropertyAndMethod();
// Expression<Func<string>> expression = () => item.Num + item.ConstantMethod() + item.ConstantProperty;
// ExpressionWatcher expressionWatcher = new ExpressionWatcher(null,
// ExpressionWatcher.GetExpressionInfo(expression));
// expressionWatcher.ValueChanged = (ew, sender, eventArgs) => { raised++; };
// item.Num = item.Num + 1;
// Assert.AreEqual(raised, 1);
// raised = 0;
// item.RaiseConstantsChanged();
// Assert.AreEqual(raised, 0);
// expressionWatcher.Dispose();
//}

//[Test]
//public void TestWeakEventHandler()
Expand Down
40 changes: 16 additions & 24 deletions src/ObservableComputations/Common/ExpressionWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -743,9 +743,16 @@ private void workWithCallTreeNode(CallTreeNode node, object holder, WorkWithCall
switch (node._call.Type)
{
case CallType.PropertyOrField:


void subscribePropertyChanged(INotifyPropertyChanged notifyPropertyChanged)

//if (node._holder is ISelectiveNotifyPropertyChanged canNotifyPropertyChanged)
//{
// if (canNotifyPropertyChanged.CanNotifyPropertyChanged(memberName, _rootExpressionWatcher._owner))
// subscribePropertyChanged(canNotifyPropertyChanged);
//}
//else

if (node._holder is INotifyPropertyChanged notifyPropertyChanged)
{
node._propertyChangedEventHandler = (sender, args) =>
{
Expand All @@ -761,22 +768,17 @@ void subscribePropertyChanged(INotifyPropertyChanged notifyPropertyChanged)
_propertyChangedEventSubscriptions[callIndex] =
new PropertyChangedEventSubscription(notifyPropertyChanged, node._propertyChangedEventHandler);
}


if (node._holder is ICanNotifyPropertyChanged canNotifyPropertyChanged)
{
if (canNotifyPropertyChanged.CanNotifyPropertyChanged(memberName, _rootExpressionWatcher._owner))
subscribePropertyChanged(canNotifyPropertyChanged);
}
else if (node._holder is INotifyPropertyChanged notifyPropertyChanged)
{
subscribePropertyChanged(notifyPropertyChanged);
}
break;
case CallType.Method:
int argumentsCount = node._call.GetArgumentValues.Length;

void subscribeMethodChanged(INotifyMethodChanged notifyMethodChanged)
//if (node._holder is ISelectiveNotifyMethodChanged canNotifyMethodChanged)
//{
// if (canNotifyMethodChanged.CanNotifyMethodChanged(memberName, argumentsCount, _rootExpressionWatcher._owner))
// subscribeMethodChanged(canNotifyMethodChanged);
//}
//else
if (node._holder is INotifyMethodChanged notifyMethodChanged)
{
node._methodChangedEventHandler = (sender, args) =>
{
Expand All @@ -801,16 +803,6 @@ void subscribeMethodChanged(INotifyMethodChanged notifyMethodChanged)
new MethodChangedEventSubscription(notifyMethodChanged, node._methodChangedEventHandler);
}

if (node._holder is ICanNotifyMethodChanged canNotifyMethodChanged)
{
if (canNotifyMethodChanged.CanNotifyMethodChanged(memberName, argumentsCount, _rootExpressionWatcher._owner))
subscribeMethodChanged(canNotifyMethodChanged);
}
else if (node._holder is INotifyMethodChanged notifyMethodChanged)
{
subscribeMethodChanged(notifyMethodChanged);
}

ExpressionWatcher[] nodeCallArguments = node._callArguments;
if (nodeCallArguments != null)
{
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.ComponentModel;

namespace ObservableComputations
{
//public interface ISelectiveNotifyMethodChanged : INotifyMethodChanged
//{
// bool CanNotifyMethodChanged(string methodName, int argumentsCount, IComputing computing);
//}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.ComponentModel;

namespace ObservableComputations
{
//public interface ISelectiveNotifyPropertyChanged : INotifyPropertyChanged
//{
// bool CanNotifyPropertyChanged(string propertyName, IComputing computing);
//}
}
3 changes: 1 addition & 2 deletions src/ObservableComputations/ObservableComputations.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
<PackageReleaseNotes>* Bug fixes
- Ordering, ThenOrdering: Error if IComparer.Compare returns a number greater than 1 or smaller than -1
- Aggregating: IsDefaulted is not set to true if source collection is null
* New features
- ICanNotifyMethodChanged and ICanNotifyPropertyChanged interfaces</PackageReleaseNotes>
- Aggregating: Custom default value (DefaultValue property) is ignored if source collection is null</PackageReleaseNotes>
<Version>2.2.0</Version>
<AssemblyName>ObservableComputations</AssemblyName>
<PackageId>ObservableComputations</PackageId>
Expand Down

0 comments on commit 0179b45

Please sign in to comment.