Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed private WeakEventListener #4450

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

puppetsw
Copy link
Contributor

NetworkConnectionStateTrigger.cs had a private duplicate version of WeakEventListener. This has now been removed and uses the existing WeakEventListener in the toolkit.

Fixes

Issue #4059

PR Type

What kind of change does this PR introduce?

Bugfix

What is the current behavior?

"NetworkConnectionStateTrigger is using a private copy of WeakEventListener, it's identical to the one we already have in the Toolkit, so we should update the source to use the existing code within the Toolkit."

What is the new behavior?

"Use existing code over shadow copy."

PR Checklist

Please check if your PR fulfills the following requirements:

  • Tested code with current supported SDKs
  • New component
    • Pull Request has been submitted to the documentation repository instructions. Link:
    • Added description of major feature to project description for NuGet package (4000 total character limit, so don't push entire description over that)
    • If control, added to Visual Studio Design project
  • Sample in sample app has been added / updated (for bug fixes / features)
  • New major technical changes in the toolkit have or will be added to the Wiki e.g. build changes, source generators, testing infrastructure, sample creation changes, etc...
  • Tests for the changes have been added (for bug fixes / features) (if applicable)
  • Header has been added to all new source files (run build/UpdateHeaders.bat)
  • Contains NO breaking changes

Other information

NetworkConnectionStateTrigger.cs had a private duplicate version of WeakEventListener. This has now been removed and uses the existing WeakEventListener in the toolkit.
@ghost
Copy link

ghost commented Jan 10, 2022

Thanks puppetsw for opening a Pull Request! The reviewers will test the PR and highlight if there is any conflict or changes required. If the PR is approved we will proceed to merge the pull request 🙌

@ghost ghost requested review from michael-hawker and azchohfi January 10, 2022 00:39
Copy link
Member

@michael-hawker michael-hawker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think this approach is still capturing the object.

OnDetachAction = listener => { NetworkInformation.NetworkStatusChanged -= OnNetworkEvent; }
};

NetworkInformation.NetworkStatusChanged += OnNetworkEvent;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should just be NetworkInformation.NetworkStatusChanged += _weakEvent.OnEvent - otherwise we capture the event and defeat the purpose of the WeakEventListener, no?

I wonder if we need to look at the history, the original developer probably had the copy because of the strange delegate from NetworkStatusChanged... Trying to think of how we can work around this...

Copy link
Member

@Arlodotexe Arlodotexe Jan 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NetworkInformation.NetworkStatusChanged += _weakEvent.OnEvent isn't possible because NetworkInformation.NetworkStatusChanged is a custom delgate, not a normal event handler.

We can probably use the sender to access fields on NetworkConnectionStateTrigger and make this method static, that might help?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@puppetsw Let's do the same thing for the OnDetachAction if we can.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Arlodotexe I'm sorry, but I'm not sure how to go about this, or I'm not understanding. If you could give a pointer I'm happy to make the change.

Copy link
Member

@Arlodotexe Arlodotexe Sep 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dug into this a bit. Looks like OnDetachAction doesn't supply the access to the instance inside the delegate. Though it does null out the OnDetachAction after invoking it, so it should release the delegate and all references captured by it.

@michael-hawker does that sound right? Should be able to move forward if so.

@ghost
Copy link

ghost commented Jan 19, 2022

This PR has been marked as "needs attention 👋" and awaiting a response from the team.

@Arlodotexe
Copy link
Member

Copied from the original issue:

While closing #4450, we noticed that the code is not identical to what we have in the toolkit. It removes TEventArgs to allow us to do NetworkInformation.NetworkStatusChanged += weakEvent.OnEvent;.

After discussing with @michael-hawker, we won't be removing the private event listener for now. Instead, we'll use the more flexible CommunityToolkit/dotnet#404 when it arrives.

@michael-hawker michael-hawker modified the milestones: 7.1.3, 8.0 Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants