-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathExpressionNode.cs
168 lines (139 loc) · 4.74 KB
/
ExpressionNode.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
namespace Avalonia.Data.Core
{
internal abstract class ExpressionNode
{
protected static readonly WeakReference<object?> UnsetReference =
new WeakReference<object?>(AvaloniaProperty.UnsetValue);
protected static readonly WeakReference<object?> NullReference =
new WeakReference<object?>(null);
private WeakReference<object?> _target = UnsetReference;
private Action<object?>? _subscriber;
private bool _listening;
protected WeakReference<object?>? LastValue { get; private set; }
public abstract string? Description { get; }
public ExpressionNode? Next { get; set; }
public WeakReference<object?> Target
{
get { return _target; }
set
{
_ = value ?? throw new ArgumentNullException(nameof(value));
_target.TryGetTarget(out var oldTarget);
value.TryGetTarget(out var newTarget);
if (!ReferenceEquals(oldTarget, newTarget))
{
if (_listening)
{
StopListening();
}
_target = value;
if (_subscriber != null)
{
StartListening();
}
}
}
}
public void Subscribe(Action<object?> subscriber)
{
if (_subscriber != null)
{
throw new AvaloniaInternalException("ExpressionNode can only be subscribed once.");
}
_subscriber = subscriber;
Next?.Subscribe(NextValueChanged);
StartListening();
}
public void Unsubscribe()
{
Next?.Unsubscribe();
if (_listening)
{
StopListening();
}
LastValue = null;
_subscriber = null;
}
protected virtual void StartListeningCore(WeakReference<object?> reference)
{
reference.TryGetTarget(out var target);
ValueChanged(target);
}
protected virtual void StopListeningCore()
{
}
protected virtual void NextValueChanged(object? value)
{
if (_subscriber is null)
return;
var bindingBroken = BindingNotification.ExtractError(value) as MarkupBindingChainException;
bindingBroken?.AddNode(Description ?? "{empty}");
_subscriber(value);
}
protected void ValueChanged(object? value) => ValueChanged(value, true);
private void ValueChanged(object? value, bool notify)
{
if (_subscriber is { } subscriber)
{
var notification = value as BindingNotification;
var next = Next;
if (notification == null)
{
LastValue = value != null ? new WeakReference<object?>(value) : NullReference;
if (next != null)
{
next.Target = LastValue;
}
else if (notify)
{
subscriber(value);
}
}
else
{
LastValue = notification.Value != null ? new WeakReference<object?>(notification.Value) : NullReference;
if (next != null)
{
next.Target = LastValue;
}
if (next == null || notification.Error != null)
{
subscriber(value);
}
}
}
}
private void StartListening()
{
_target.TryGetTarget(out var target);
if (target == null)
{
ValueChanged(TargetNullNotification());
_listening = false;
}
else if (target != AvaloniaProperty.UnsetValue)
{
_listening = true;
StartListeningCore(_target!);
}
else
{
ValueChanged(AvaloniaProperty.UnsetValue, notify:false);
_listening = false;
}
}
private void StopListening()
{
StopListeningCore();
_listening = false;
}
private static BindingNotification TargetNullNotification()
{
return new BindingNotification(
new MarkupBindingChainException("Null value"),
BindingErrorType.Error,
AvaloniaProperty.UnsetValue);
}
}
}