This repository has been archived by the owner on Nov 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 234
/
WeakEventHandler.cs
142 lines (124 loc) · 4.6 KB
/
WeakEventHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Input;
using Telerik.Core.Data;
using Windows.Foundation.Collections;
using Windows.UI.Xaml.Data;
namespace Telerik.Core
{
internal class WeakEventHandler<TArgs> where TArgs : class
{
private WeakReference eventListener;
private WeakReference eventSender;
private KnownEvents knownEvent;
public WeakEventHandler(object sender, IWeakEventListener listener, KnownEvents knownEvent)
{
this.eventListener = new WeakReference(listener);
this.eventSender = new WeakReference(sender);
this.knownEvent = knownEvent;
this.Update(sender, true);
}
public void Unsubscribe()
{
if (this.eventSender == null || !this.eventSender.IsAlive)
{
return;
}
this.Update(this.eventSender.Target, false);
this.eventSender = null;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily", Justification = "There is only one cast, which is cheaper than a cast plus a local variable declaration.")]
private void Update(object sender, bool subscribe)
{
switch (this.knownEvent)
{
case KnownEvents.CollectionChanged:
if (subscribe)
{
(sender as INotifyCollectionChanged).CollectionChanged += this.OnCollectionChanged;
}
else
{
(sender as INotifyCollectionChanged).CollectionChanged -= this.OnCollectionChanged;
}
break;
case KnownEvents.PropertyChanged:
if (subscribe)
{
(sender as INotifyPropertyChanged).PropertyChanged += this.OnPropertyChanged;
}
else
{
(sender as INotifyPropertyChanged).PropertyChanged -= this.OnPropertyChanged;
}
break;
case KnownEvents.VectorChanged:
if (subscribe)
{
(sender as ICollectionView).VectorChanged += this.OnVectorChanged;
}
else
{
(sender as ICollectionView).VectorChanged -= this.OnVectorChanged;
}
break;
case KnownEvents.CurrentItemChanged:
if (subscribe)
{
(sender as ICollectionView).CurrentChanged += this.OnCurrentChanged;
}
else
{
(sender as ICollectionView).CurrentChanged -= this.OnCurrentChanged;
}
break;
case KnownEvents.CanExecuteChanged:
if (subscribe)
{
(sender as ICommand).CanExecuteChanged += this.OnCanExecuteChanged;
}
else
{
(sender as ICommand).CanExecuteChanged -= this.OnCanExecuteChanged;
}
break;
}
}
private void OnEvent(object sender, TArgs e)
{
this.ProcessEvent(sender, e);
}
private void ProcessEvent(object sender, object args)
{
if (this.eventListener.IsAlive)
{
(this.eventListener.Target as IWeakEventListener).ReceiveEvent(sender, args);
}
else
{
this.Unsubscribe();
}
}
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
this.ProcessEvent(sender, e);
}
private void OnCurrentChanged(object sender, object e)
{
this.ProcessEvent(sender, new NotifyCurrentItemChangedEventArgs());
}
private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
this.ProcessEvent(sender, e);
}
private void OnVectorChanged(IObservableVector<object> sender, IVectorChangedEventArgs e)
{
this.ProcessEvent(sender, e);
}
private void OnCanExecuteChanged(object sender, EventArgs e)
{
this.ProcessEvent(sender, e);
}
}
}