-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathObservableState.cs
141 lines (120 loc) · 4.39 KB
/
ObservableState.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
using System;
using System.Collections.Generic;
using System.Reflection;
using Blax;
using Castle.DynamicProxy;
namespace Blax;
public class ObservableState
{
private readonly List<Action> _subscribers = new();
private static readonly ProxyGenerator _generator = new();
private object? _state;
public ObservableState()
{
ValidateObservableProperties(GetType());
}
public static T CreateInstance<T>() where T : ObservableState, new()
{
var instance = new T();
instance.InitializeProxy();
return (T)instance._state!;
}
private void InitializeProxy()
{
if (_state == null)
{
var interceptor = new StateInterceptor();
_state = _generator.CreateClassProxy(GetType(), interceptor);
}
}
public object? State => _state;
internal void Subscribe(Action callback)
{
if (_subscribers.Contains(callback)) return;
_subscribers.Add(callback);
var properties = GetType().GetProperties();
foreach (var property in properties)
{
// Auto-subscribe ObservableList properties
if (Attribute.IsDefined(property, typeof(ObservableAttribute)) && property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(ObservableList<>))
{
var list = property.GetValue(this) as IObservableList;
if (list != null)
{
SubscribeList(list);
}
}
// Auto-subscribe ObservableDictionary properties
else if (Attribute.IsDefined(property, typeof(ObservableAttribute)) && property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(ObservableDictionary<,>))
{
var dict = property.GetValue(this) as IObservableDictionary;
if (dict != null)
{
SubscribeDict(dict);
}
}
}
}
internal void Unsubscribe(Action callback)
{
while (_subscribers.Remove(callback));
var properties = GetType().GetProperties();
foreach (var property in properties)
{
// Auto-subscribe ObservableList properties
if (Attribute.IsDefined(property, typeof(ObservableAttribute)) && property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(ObservableList<>))
{
var list = property.GetValue(this) as IObservableList;
if (list != null)
{
UnsubscribeList(list);
}
}
// Auto-subscribe ObservableDictionary properties
else if (Attribute.IsDefined(property, typeof(ObservableAttribute)) && property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(ObservableDictionary<,>))
{
var dict = property.GetValue(this) as IObservableDictionary;
if (dict != null)
{
UnsubscribeDict(dict);
}
}
}
}
internal void NotifySubscribers()
{
foreach (var subscriber in _subscribers)
{
subscriber.Invoke();
}
}
private void SubscribeList(IObservableList list)
{
list.ListChanged += NotifySubscribers;
}
private void UnsubscribeList(IObservableList list)
{
list.ListChanged -= NotifySubscribers;
}
private void SubscribeDict(IObservableDictionary dict)
{
dict.DictionaryChanged += NotifySubscribers;
}
private void UnsubscribeDict(IObservableDictionary dict)
{
dict.DictionaryChanged -= NotifySubscribers;
}
private void ValidateObservableProperties(Type type)
{
var properties = type.GetProperties();
foreach (var property in properties)
{
var isObservable = Attribute.IsDefined(property, typeof(ObservableAttribute));
if (isObservable && !(property?.GetMethod?.IsVirtual ?? false))
{
var error = $"The property '{property?.Name}' in type '{type.FullName}' is marked as [Observable] but is not virtual. [Observable] properties must be virtual.";
throw new InvalidOperationException(error);
}
}
}
}