|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Reflection; |
| 6 | + |
| 7 | +namespace CoreAppUAP.Common |
| 8 | +{ |
| 9 | + public partial class WeakEvent<TEventArgs> : IList<Action<TEventArgs>> |
| 10 | + { |
| 11 | + private class Method(Action<TEventArgs> callback) : IEquatable<Method>, IEquatable<Action<TEventArgs>> |
| 12 | + { |
| 13 | + private readonly bool _isStatic = callback.Target == null; |
| 14 | + private readonly WeakReference _reference = new(callback.Target); |
| 15 | + private readonly MethodInfo _method = callback.GetMethodInfo(); |
| 16 | + |
| 17 | + public bool IsDead => !(_isStatic || _reference.IsAlive); |
| 18 | + |
| 19 | + public void Invoke(TEventArgs arg) |
| 20 | + { |
| 21 | + if (!IsDead) |
| 22 | + { |
| 23 | + _method.Invoke(_reference.Target, [arg]); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + public bool Equals(Method other) => |
| 28 | + other != null |
| 29 | + && _reference.Target == other._reference.Target |
| 30 | + && _method == other._method; |
| 31 | + |
| 32 | + public bool Equals(Action<TEventArgs> callback) => |
| 33 | + callback != null |
| 34 | + && _reference.Target == callback.Target |
| 35 | + && _method == callback.GetMethodInfo(); |
| 36 | + |
| 37 | + public override bool Equals(object obj) => obj switch |
| 38 | + { |
| 39 | + Method other => Equals(other), |
| 40 | + Action<TEventArgs> callback => Equals(callback), |
| 41 | + _ => false, |
| 42 | + }; |
| 43 | + |
| 44 | + public override int GetHashCode() => (_reference, _method).GetHashCode(); |
| 45 | + |
| 46 | + public static implicit operator Method(Action<TEventArgs> callback) => new(callback); |
| 47 | + |
| 48 | + public static explicit operator Action<TEventArgs>(Method method) => method.IsDead ? null : method._method.CreateDelegate(typeof(Action<TEventArgs>), method._reference.Target) as Action<TEventArgs>; |
| 49 | + } |
| 50 | + |
| 51 | + private readonly List<Method> _list; |
| 52 | + |
| 53 | + public WeakEvent() => _list = []; |
| 54 | + |
| 55 | + public WeakEvent(IEnumerable<Action<TEventArgs>> collection) => _list = [.. collection.Select<Action<TEventArgs>, Method>(x => x)]; |
| 56 | + |
| 57 | + public WeakEvent(int capacity) => _list = new List<Method>(capacity); |
| 58 | + |
| 59 | + public WeakEvent(ReadOnlySpan<Action<TEventArgs>> callbacks) => _list = [.. callbacks]; |
| 60 | + |
| 61 | + public int Count => _list.Count; |
| 62 | + |
| 63 | + public bool IsReadOnly => ((ICollection<Method>)_list).IsReadOnly; |
| 64 | + |
| 65 | + public Action<TEventArgs> this[int index] |
| 66 | + { |
| 67 | + get => (Action<TEventArgs>)_list[index]; |
| 68 | + set => _list[index] = value; |
| 69 | + } |
| 70 | + |
| 71 | + public void Invoke(TEventArgs arg) |
| 72 | + { |
| 73 | + for (int i = _list.Count; --i >= 0;) |
| 74 | + { |
| 75 | + if (_list[i].IsDead) |
| 76 | + { |
| 77 | + _list.RemoveAt(i); |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + _list[i].Invoke(arg); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public void Add(Action<TEventArgs> callback) => _list.Add(callback); |
| 87 | + |
| 88 | + public void AddRange(IEnumerable<Action<TEventArgs>> collection) => _list.AddRange(collection.Select<Action<TEventArgs>, Method>(x => x)); |
| 89 | + |
| 90 | + public void Insert(int index, Action<TEventArgs> item) => _list.Insert(index, item); |
| 91 | + |
| 92 | + public void CopyTo(Action<TEventArgs>[] array, int arrayIndex) => Array.Copy(_list.Select(x => (Action<TEventArgs>)x).ToArray(), 0, array, arrayIndex, _list.Count); |
| 93 | + |
| 94 | + public void Remove(Action<TEventArgs> callback) |
| 95 | + { |
| 96 | + for (int i = _list.Count; --i >= 0;) |
| 97 | + { |
| 98 | + if (_list[i].IsDead) |
| 99 | + { |
| 100 | + _list.RemoveAt(i); |
| 101 | + } |
| 102 | + else if (_list[i].Equals(callback)) |
| 103 | + { |
| 104 | + _list.RemoveAt(i); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + bool ICollection<Action<TEventArgs>>.Remove(Action<TEventArgs> callback) |
| 110 | + { |
| 111 | + for (int i = _list.Count; --i >= 0;) |
| 112 | + { |
| 113 | + if (_list[i].IsDead) |
| 114 | + { |
| 115 | + _list.RemoveAt(i); |
| 116 | + } |
| 117 | + else if (_list[i].Equals(callback)) |
| 118 | + { |
| 119 | + _list.RemoveAt(i); |
| 120 | + return true; |
| 121 | + } |
| 122 | + } |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + public void RemoveAt(int index) => _list.RemoveAt(index); |
| 127 | + |
| 128 | + public int RemoveAll(Predicate<Action<TEventArgs>> predicate) => _list.RemoveAll(x => predicate((Action<TEventArgs>)x)); |
| 129 | + |
| 130 | + public void Clear() => _list.Clear(); |
| 131 | + |
| 132 | + public bool Contains(Action<TEventArgs> callback) |
| 133 | + { |
| 134 | + for (int i = _list.Count; --i >= 0;) |
| 135 | + { |
| 136 | + if (_list[i].IsDead) |
| 137 | + { |
| 138 | + _list.RemoveAt(i); |
| 139 | + } |
| 140 | + else if (_list[i].Equals(callback)) |
| 141 | + { |
| 142 | + return true; |
| 143 | + } |
| 144 | + } |
| 145 | + return false; |
| 146 | + } |
| 147 | + |
| 148 | + public int IndexOf(Action<TEventArgs> callback) |
| 149 | + { |
| 150 | + for (int i = _list.Count; --i >= 0;) |
| 151 | + { |
| 152 | + if (_list[i].IsDead) |
| 153 | + { |
| 154 | + _list.RemoveAt(i); |
| 155 | + } |
| 156 | + else if (_list[i].Equals(callback)) |
| 157 | + { |
| 158 | + return i; |
| 159 | + } |
| 160 | + } |
| 161 | + return -1; |
| 162 | + } |
| 163 | + |
| 164 | + public IEnumerator<Action<TEventArgs>> GetEnumerator() => _list.Select(x => (Action<TEventArgs>)x).GetEnumerator(); |
| 165 | + |
| 166 | + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| 167 | + |
| 168 | + public static WeakEvent<TEventArgs> operator +(WeakEvent<TEventArgs> weakEvent, Action<TEventArgs> callback) |
| 169 | + { |
| 170 | + weakEvent.Add(callback); |
| 171 | + return weakEvent; |
| 172 | + } |
| 173 | + |
| 174 | + public static WeakEvent<TEventArgs> operator -(WeakEvent<TEventArgs> weakEvent, Action<TEventArgs> callback) |
| 175 | + { |
| 176 | + weakEvent.Remove(callback); |
| 177 | + return weakEvent; |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments