-
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 #132 from WalletConnect/refactor/event-overhaul
Overhaul events to use EventHandler
- Loading branch information
Showing
72 changed files
with
920 additions
and
2,104 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
Core Modules/WalletConnectSharp.Common/Events/EventHandlerMap.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,109 @@ | ||
namespace WalletConnectSharp.Common.Events; | ||
|
||
/// <summary> | ||
/// A mapping of eventIds to EventHandler objects. This using a Dictionary as the backing datastore | ||
/// </summary> | ||
/// <typeparam name="TEventArgs">The type of EventHandler's argument to store</typeparam> | ||
public class EventHandlerMap<TEventArgs> : IDisposable | ||
{ | ||
private Dictionary<string, EventHandler<TEventArgs>> mapping = new(); | ||
|
||
private readonly object _mappingLock = new(); | ||
|
||
private EventHandler<TEventArgs> BeforeEventExecuted; | ||
|
||
/// <summary> | ||
/// Create a new EventHandlerMap with an initial EventHandler to append onto | ||
/// </summary> | ||
/// <param name="callbackBeforeExecuted">The initial EventHandler to use as the EventHandler.</param> | ||
public EventHandlerMap(EventHandler<TEventArgs> callbackBeforeExecuted = null) | ||
{ | ||
if (callbackBeforeExecuted == null) | ||
{ | ||
callbackBeforeExecuted = CallbackBeforeExecuted; | ||
} | ||
|
||
this.BeforeEventExecuted = callbackBeforeExecuted; | ||
} | ||
|
||
private void CallbackBeforeExecuted(object sender, TEventArgs e) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Get an EventHandler by its eventId. If the provided eventId does not exist, then the | ||
/// initial EventHandler is returned and tracking begins | ||
/// </summary> | ||
/// <param name="eventId">The eventId of the EventHandler</param> | ||
public EventHandler<TEventArgs> this[string eventId] | ||
{ | ||
get | ||
{ | ||
lock (_mappingLock) | ||
{ | ||
mapping.TryAdd(eventId, BeforeEventExecuted); | ||
|
||
return mapping[eventId]; | ||
} | ||
} | ||
set | ||
{ | ||
lock (_mappingLock) | ||
{ | ||
if (mapping.ContainsKey(eventId)) | ||
{ | ||
mapping.Remove(eventId); | ||
} | ||
|
||
mapping.Add(eventId, value); | ||
} | ||
} | ||
} | ||
|
||
public void ListenOnce(string eventId, EventHandler<TEventArgs> eventHandler) | ||
{ | ||
EventHandler<TEventArgs> internalHandler = null; | ||
internalHandler = (src, args) => | ||
{ | ||
this[eventId] -= internalHandler; | ||
eventHandler(src, args); | ||
}; | ||
this[eventId] += internalHandler; | ||
} | ||
|
||
/// <summary> | ||
/// Check if a given eventId has any EventHandlers registered yet. | ||
/// </summary> | ||
/// <param name="eventId">The eventId to check for</param> | ||
/// <returns>true if the eventId has any EventHandlers, false otherwise</returns> | ||
public bool Contains(string eventId) | ||
{ | ||
lock (_mappingLock) | ||
{ | ||
return mapping.ContainsKey(eventId); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Clear an eventId from the mapping | ||
/// </summary> | ||
/// <param name="eventId">The eventId to clear</param> | ||
public void Clear(string eventId) | ||
{ | ||
lock (_mappingLock) | ||
{ | ||
if (mapping.ContainsKey(eventId)) | ||
{ | ||
mapping.Remove(eventId); | ||
} | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
lock (_mappingLock) | ||
{ | ||
mapping.Clear(); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
Core Modules/WalletConnectSharp.Common/Events/GenericEventHolder.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,19 @@ | ||
namespace WalletConnectSharp.Common.Events; | ||
|
||
public class GenericEventHolder | ||
{ | ||
private Dictionary<Type, object> dynamicTypeMapping = new(); | ||
|
||
public EventHandlerMap<T> OfType<T>() | ||
{ | ||
Type t = typeof(T); | ||
|
||
if (dynamicTypeMapping.TryGetValue(t, out var value)) | ||
return (EventHandlerMap<T>)value; | ||
|
||
var mapping = new EventHandlerMap<T>(); | ||
dynamicTypeMapping.Add(t, mapping); | ||
|
||
return mapping; | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
Core Modules/WalletConnectSharp.Common/Utils/RpcPayloadId.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,22 @@ | ||
namespace WalletConnectSharp.Common.Utils; | ||
|
||
/// <summary> | ||
/// A static class that can generate random JSONRPC ids using the current time as a source of randomness | ||
/// </summary> | ||
public static class RpcPayloadId | ||
{ | ||
private static readonly Random rng = new Random(); | ||
private static readonly DateTime UnixEpoch = | ||
new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); | ||
|
||
/// <summary> | ||
/// Generate a new random JSON-RPC id. The clock is used as a source of randomness | ||
/// </summary> | ||
/// <returns>A random JSON-RPC id</returns> | ||
public static long Generate() | ||
{ | ||
var date = (long)((DateTime.UtcNow - UnixEpoch).TotalMilliseconds) * (10L * 10L * 10L); | ||
var extra = (long)Math.Floor(rng.NextDouble() * (10.0 * 10.0 * 10.0)); | ||
return date + extra; | ||
} | ||
} |
Oops, something went wrong.