-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServices.cs
217 lines (185 loc) · 6.18 KB
/
Services.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System.Reactive.Subjects;
using System.Reactive.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public class WidgetRegistrationService
{
private readonly object idGeneratorLock = new object();
private readonly object idWidgetRegistrationLock = new object();
private readonly object idEventRegistrationLock = new object();
private readonly ReplaySubject<Action> eventsSubject = new ReplaySubject<Action>(10);
private readonly BehaviorSubject<Dictionary<int, Action>> onClickRegistry = new BehaviorSubject<Dictionary<int, Action>>(new Dictionary<int, Action>());
private readonly Dictionary<int, object> widgetRegistry = new Dictionary<int, object>();
private int lastWidgetId = 0;
private int lastComponentId = 0;
public WidgetRegistrationService()
{
eventsSubject
.Throttle(TimeSpan.FromMilliseconds(1))
.Subscribe(fn => fn());
}
public object GetWidgetById(int widgetId)
{
lock (idWidgetRegistrationLock)
{
widgetRegistry.TryGetValue(widgetId, out var widget);
return widget;
}
}
public void RegisterWidget(int widgetId, object widget)
{
lock (idWidgetRegistrationLock)
{
widgetRegistry[widgetId] = widget;
}
}
public int GetNextWidgetId()
{
lock (idGeneratorLock)
{
var widgetId = lastWidgetId;
lastWidgetId++;
return widgetId;
}
}
public int GetNextComponentId()
{
lock (idGeneratorLock)
{
var componentId = lastComponentId;
lastComponentId++;
return componentId;
}
}
public void RegisterOnClick(int widgetId, Action onClick)
{
lock (idEventRegistrationLock)
{
var newRegistry = new Dictionary<int, Action>(onClickRegistry.Value)
{
{ widgetId, onClick }
};
onClickRegistry.OnNext(newRegistry);
}
}
public void DispatchOnClickEvent(int widgetId)
{
if (onClickRegistry.Value.TryGetValue(widgetId, out var onClick))
{
eventsSubject.OnNext(onClick);
}
else
{
Console.WriteLine($"Widget with id {widgetId} has no on_click handler");
}
}
public void CreateWidget(RawChildlessWidgetNodeWithId widget)
{
var filteredProps = widget.props
.Where(kv => kv.Value == null || !typeof(Delegate).IsAssignableFrom(kv.Value.GetType()))
.ToDictionary(kv => kv.Key, kv => kv.Value);
var widgetData = new Dictionary<string, object>
{
{ "id", widget.id },
{ "type", widget.type }
};
foreach (var kv in filteredProps)
{
if (kv.Key == "style" || kv.Key == "activeStyle" || kv.Key == "hoverStyle" || kv.Key == "disabledStyle")
{
if (kv.Value is NodeStyleDef nodeStyleDef)
{
widgetData[kv.Key] = nodeStyleDef.ToDictionary();
}
else if (kv.Value is WidgetStyleDef widgetStyleDef)
{
widgetData[kv.Key] = widgetStyleDef.ToDictionary();
}
} else {
widgetData[kv.Key] = kv.Value;
}
}
var settings = new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
Converters = new List<JsonConverter> { new StringEnumConverter() }
};
var widgetJson = JsonConvert.SerializeObject(widgetData, settings);
SetElement(widgetJson);
}
public void PatchWidget(int widgetId, object widget)
{
var widgetJson = JsonConvert.SerializeObject(widget);
PatchElement(widgetId, widgetJson);
}
public void LinkChildren(int widgetId, List<int> childIds)
{
var childrenJson = JsonConvert.SerializeObject(childIds);
SetChildren(widgetId, childrenJson);
}
// These methods need work
public void SetData(int widgetId, object data)
{
var dataJson = JsonConvert.SerializeObject(data);
ElementInternalOp(widgetId, dataJson);
}
public void AppendData(int widgetId, object data)
{
var dataJson = JsonConvert.SerializeObject(data);
ElementInternalOp(widgetId, dataJson);
}
public void ResetData(int widgetId)
{
var dataJson = JsonConvert.SerializeObject("");
ElementInternalOp(widgetId, dataJson);
}
public void ResetData(int widgetId, object data)
{
var dataJson = JsonConvert.SerializeObject(data);
ElementInternalOp(widgetId, dataJson);
}
public void AppendDataToPlotLine(int widgetId, double x, double y)
{
var plotData = new { x, y };
ElementInternalOp(widgetId, JsonConvert.SerializeObject(plotData));
}
public void SetPlotLineAxesDecimalDigits(int widgetId, double x, double y)
{
var axesData = new { x, y };
ElementInternalOp(widgetId, JsonConvert.SerializeObject(axesData));
}
public void AppendTextToClippedMultiLineTextRenderer(int widgetId, string text)
{
ExternAppendText(widgetId, text);
}
public void SetInputTextValue(int widgetId, string value)
{
var inputTextData = new { value };
ElementInternalOp(widgetId, JsonConvert.SerializeObject(inputTextData));
}
public void SetComboSelectedIndex(int widgetId, int index)
{
var selectedIndexData = new { index };
ElementInternalOp(widgetId, JsonConvert.SerializeObject(selectedIndexData));
}
private void SetElement(string jsonData)
{
XFrames.setElement(jsonData);
}
private void PatchElement(int widgetId, string jsonData)
{
// Implement patch logic
}
private void SetChildren(int widgetId, string jsonData)
{
XFrames.setChildren(widgetId, jsonData);
}
private void ElementInternalOp(int widgetId, string jsonData)
{
// Implement internal operation logic
}
private void ExternAppendText(int widgetId, string text)
{
// Implement text appending logic
}
}