forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reusable function for raising events (MonoGame#5713)
* Reusable function for raising events Before raising an event, you must first check the handler to see if it is null in order to check that an object is subscribed to handle the event. This is best done if the event handler is stored in a local variable just in case the last subscriber unsubscribes in a different thread between the time the event handler is checked and the handler is actually invoked. This reusable function makes it easy to raise events by storing a local copy of the handler and checking for null before invoking it. Many classes already had a helper function for this. This change refactors those into a single function. There are some instances where more complex logic was being done after the event handler was checked. In those scenarios, the code was mostly left alone except for the introduction of a local handler variable. * Safely raising events through common function - Updated events being raised throughout the framework with the new EventHelpers.Raise function - Updated the name of EventHelper to be plural in case more helper methods are needed in the future.
- Loading branch information
1 parent
d9603eb
commit aa98ee6
Showing
34 changed files
with
180 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// MonoGame - Copyright (C) The MonoGame Team | ||
// This file is subject to the terms and conditions defined in | ||
// file 'LICENSE.txt', which is part of this source code package. | ||
|
||
using System; | ||
|
||
namespace Microsoft.Xna.Framework | ||
{ | ||
/// <summary> | ||
/// Provides helper methods to make it easier | ||
/// to safely raise events. | ||
/// </summary> | ||
internal static class EventHelpers | ||
{ | ||
/// <summary> | ||
/// Safely raises an event by storing a copy of the event's delegate | ||
/// in the <paramref name="handler"/> parameter and checking it for | ||
/// null before invoking it. | ||
/// </summary> | ||
/// <typeparam name="TEventArgs"></typeparam> | ||
/// <param name="sender">The object raising the event.</param> | ||
/// <param name="handler"><see cref="EventHandler{TEventArgs}"/> to be invoked</param> | ||
/// <param name="e">The <typeparamref name="TEventArgs"/> passed to <see cref="EventHandler{TEventArgs}"/></param> | ||
internal static void Raise<TEventArgs>(object sender, EventHandler<TEventArgs> handler, TEventArgs e) | ||
where TEventArgs : EventArgs | ||
{ | ||
if (handler != null) | ||
handler(sender, e); | ||
} | ||
|
||
/// <summary> | ||
/// Safely raises an event by storing a copy of the event's delegate | ||
/// in the <paramref name="handler"/> parameter and checking it for | ||
/// null before invoking it. | ||
/// </summary> | ||
/// <param name="sender">The object raising the event.</param> | ||
/// <param name="handler"><see cref="EventHandler"/> to be invoked</param> | ||
/// <param name="e">The <see cref="EventArgs"/> passed to <see cref="EventHandler"/></param> | ||
internal static void Raise(object sender, EventHandler handler, EventArgs e) | ||
{ | ||
if (handler != null) | ||
handler(sender, e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.