Skip to content

Commit fd90e43

Browse files
Created EventManagerService, Removed BaseWeakEventManager
1 parent d398244 commit fd90e43

File tree

5 files changed

+216
-253
lines changed

5 files changed

+216
-253
lines changed

Src/AsyncAwaitBestPractices/AsyncAwaitBestPractices.csproj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
44
<TargetFramework>netstandard1.0</TargetFramework>
5+
<LangVersion>latest</LangVersion>
56
</PropertyGroup>
67
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
7-
<DebugSymbols>true</DebugSymbols>
8-
<DebugType>full</DebugType>
9-
<DocumentationFile>bin\Release\netstandard1.0\AsyncAwaitBestPractices.xml</DocumentationFile>
8+
<DebugSymbols>true</DebugSymbols>
9+
<DebugType>full</DebugType>
10+
<DocumentationFile>bin\Release\netstandard1.0\AsyncAwaitBestPractices.xml</DocumentationFile>
1011
</PropertyGroup>
12+
<ItemGroup>
13+
<Folder Include="WeakEventManager\" />
14+
</ItemGroup>
1115
</Project>

Src/AsyncAwaitBestPractices/WeakEventManager.cs

Lines changed: 0 additions & 250 deletions
This file was deleted.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
5+
namespace AsyncAwaitBestPractices
6+
{
7+
static class EventManagerService
8+
{
9+
internal static void AddEventHandler(string eventName, object handlerTarget, MethodInfo methodInfo, in Dictionary<string, List<Subscription>> eventHandlers)
10+
{
11+
var doesContainSubscriptions = eventHandlers.TryGetValue(eventName, out List<Subscription> targets);
12+
if (!doesContainSubscriptions)
13+
{
14+
targets = new List<Subscription>();
15+
eventHandlers.Add(eventName, targets);
16+
}
17+
18+
if (handlerTarget is null)
19+
targets.Add(new Subscription(null, methodInfo));
20+
else
21+
targets.Add(new Subscription(new WeakReference(handlerTarget), methodInfo));
22+
}
23+
24+
internal static void RemoveEventHandler(string eventName, object handlerTarget, MemberInfo methodInfo, in Dictionary<string, List<Subscription>> eventHandlers)
25+
{
26+
var doesContainSubscriptions = eventHandlers.TryGetValue(eventName, out List<Subscription> subscriptions);
27+
if (!doesContainSubscriptions)
28+
return;
29+
30+
for (int n = subscriptions.Count; n > 0; n--)
31+
{
32+
Subscription current = subscriptions[n - 1];
33+
34+
if (current.Subscriber?.Target != handlerTarget
35+
|| current.Handler.Name != methodInfo.Name)
36+
{
37+
continue;
38+
}
39+
40+
subscriptions.Remove(current);
41+
break;
42+
}
43+
}
44+
45+
internal static void HandleEvent(string eventName, object sender, object eventArgs, in Dictionary<string, List<Subscription>> eventHandlers)
46+
{
47+
var toRaise = new List<Tuple<object, MethodInfo>>();
48+
var toRemove = new List<Subscription>();
49+
50+
if (eventHandlers.TryGetValue(eventName, out List<Subscription> target))
51+
{
52+
for (int i = 0; i < target.Count; i++)
53+
{
54+
Subscription subscription = target[i];
55+
bool isStatic = subscription.Subscriber is null;
56+
if (isStatic)
57+
{
58+
toRaise.Add(Tuple.Create<object, MethodInfo>(null, subscription.Handler));
59+
continue;
60+
}
61+
62+
object subscriber = subscription.Subscriber.Target;
63+
64+
if (subscriber is null)
65+
toRemove.Add(subscription);
66+
else
67+
toRaise.Add(Tuple.Create(subscriber, subscription.Handler));
68+
}
69+
70+
for (int i = 0; i < toRemove.Count; i++)
71+
{
72+
Subscription subscription = toRemove[i];
73+
target.Remove(subscription);
74+
}
75+
}
76+
77+
for (int i = 0; i < toRaise.Count; i++)
78+
{
79+
Tuple<object, MethodInfo> tuple = toRaise[i];
80+
tuple.Item2.Invoke(tuple.Item1, new[] { sender, eventArgs });
81+
}
82+
}
83+
}
84+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Reflection;
3+
4+
namespace AsyncAwaitBestPractices
5+
{
6+
internal struct Subscription
7+
{
8+
public Subscription(WeakReference subscriber, MethodInfo handler)
9+
{
10+
Subscriber = subscriber;
11+
Handler = handler ?? throw new ArgumentNullException(nameof(handler));
12+
}
13+
14+
public WeakReference Subscriber { get; }
15+
public MethodInfo Handler { get; }
16+
}
17+
}

0 commit comments

Comments
 (0)