-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #157 from WalletConnect/fix/listenonce-breaks-aot
fix: ListenOnce extension breaks AOT
- Loading branch information
Showing
8 changed files
with
300 additions
and
171 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
Core Modules/WalletConnectSharp.Common/Utils/EventUtils.cs
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,54 @@ | ||
namespace WalletConnectSharp.Common.Utils; | ||
|
||
public class EventUtils | ||
{ | ||
/// <summary> | ||
/// Invoke the given event handler once and then unsubscribe it. | ||
/// Use with abstract events. Otherwise, use the extension method as it is more efficient. | ||
/// </summary> | ||
/// <example> | ||
/// <code> | ||
/// EventUtils.ListenOnce((_, _) => WCLogger.Log("Resubscribed")), | ||
/// h => this.Subscriber.Resubscribed += h, | ||
/// h => this.Subscriber.Resubscribed -= h | ||
/// ); | ||
/// </code> | ||
/// </example> | ||
public static Action ListenOnce( | ||
EventHandler handler, | ||
Action<EventHandler> subscribe, | ||
Action<EventHandler> unsubscribe) | ||
{ | ||
EventHandler internalHandler = null; | ||
internalHandler = (sender, args) => | ||
{ | ||
unsubscribe(internalHandler); | ||
handler(sender, args); | ||
}; | ||
|
||
subscribe(internalHandler); | ||
|
||
return () => unsubscribe(internalHandler); | ||
} | ||
|
||
/// <summary> | ||
/// Invoke the given event handler once and then unsubscribe it. | ||
/// Use with abstract events. Otherwise, use the extension method as it is more efficient. | ||
/// </summary> | ||
public static Action ListenOnce<TEventArgs>( | ||
EventHandler<TEventArgs> handler, | ||
Action<EventHandler<TEventArgs>> subscribe, | ||
Action<EventHandler<TEventArgs>> unsubscribe) | ||
{ | ||
EventHandler<TEventArgs> internalHandler = null; | ||
internalHandler = (sender, args) => | ||
{ | ||
unsubscribe(internalHandler); | ||
handler(sender, args); | ||
}; | ||
|
||
subscribe(internalHandler); | ||
|
||
return () => unsubscribe(internalHandler); | ||
} | ||
} |
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.