forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoPersistHelper.cs
More file actions
196 lines (177 loc) · 9.01 KB
/
AutoPersistHelper.cs
File metadata and controls
196 lines (177 loc) · 9.01 KB
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
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Reflection;
using Splat;
namespace ReactiveUI
{
public static class AutoPersistHelper
{
static MemoizingMRUCache<Type, Dictionary<string, bool>> persistablePropertiesCache = new MemoizingMRUCache<Type, Dictionary<string, bool>>((type, _) => {
return type.GetTypeInfo().DeclaredProperties
.Where(x => x.CustomAttributes.Any(y => typeof(DataMemberAttribute).GetTypeInfo().IsAssignableFrom(y.AttributeType.GetTypeInfo())))
.ToDictionary(k => k.Name, v => true);
}, RxApp.SmallCacheLimit);
static MemoizingMRUCache<Type, bool> dataContractCheckCache = new MemoizingMRUCache<Type, bool>((t, _) => {
return t.GetTypeInfo().GetCustomAttributes(typeof(DataContractAttribute), true).Any();
}, RxApp.SmallCacheLimit);
/// <summary>
/// AutoPersist allows you to automatically call a method when an object
/// has changed, throttling on a certain interval. Note that this object
/// must mark its persistable properties via the [DataMember] attribute.
/// Changes to properties not marked with DataMember will not trigger the
/// object to be saved.
/// </summary>
/// <param name="doPersist">The asynchronous method to call to save the
/// object to disk.</param>
/// <param name="interval">The interval to save the object on. Note that
/// if an object is constantly changing, it is possible that it will never
/// be saved.</param>
/// <returns>A Disposable to disable automatic persistence.</returns>
public static IDisposable AutoPersist<T>(this T This, Func<T, IObservable<Unit>> doPersist, TimeSpan? interval = null)
where T : IReactiveObject
{
return This.AutoPersist(doPersist, Observable.Never<Unit>(), interval);
}
/// <summary>
/// AutoPersist allows you to automatically call a method when an object
/// has changed, throttling on a certain interval. Note that this object
/// must mark its persistable properties via the [DataMember] attribute.
/// Changes to properties not marked with DataMember will not trigger the
/// object to be saved.
/// </summary>
/// <param name="doPersist">The asynchronous method to call to save the
/// object to disk.</param>
/// <param name="manualSaveSignal">When invoked, the object will be saved
/// regardless of whether it has changed.</param>
/// <param name="interval">The interval to save the object on. Note that
/// if an object is constantly changing, it is possible that it will never
/// be saved.</param>
/// <returns>A Disposable to disable automatic persistence.</returns>
public static IDisposable AutoPersist<T, TDontCare>(this T This, Func<T, IObservable<Unit>> doPersist, IObservable<TDontCare> manualSaveSignal, TimeSpan? interval = null)
where T : IReactiveObject
{
interval = interval ?? TimeSpan.FromSeconds(3.0);
lock (dataContractCheckCache) {
if (!dataContractCheckCache.Get(This.GetType())) {
throw new ArgumentException("AutoPersist can only be applied to objects with [DataContract]");
}
}
var persistableProperties = default(Dictionary<string, bool>);
lock (persistablePropertiesCache) {
persistableProperties = persistablePropertiesCache.Get(This.GetType());
}
var saveHint = Observable.Merge(
This.getChangedObservable().Where(x => persistableProperties.ContainsKey(x.PropertyName)).Select(_ => Unit.Default),
manualSaveSignal.Select(_ => Unit.Default));
var autoSaver = saveHint
.Throttle(interval.Value, RxApp.TaskpoolScheduler)
.SelectMany(_ => doPersist(This))
.Publish();
// NB: This rigamarole is to prevent the initialization of a class
// from triggering a save
var ret = new SingleAssignmentDisposable();
RxApp.MainThreadScheduler.Schedule(() => {
if (ret.IsDisposed) return;
ret.Disposable = autoSaver.Connect();
});
return ret;
}
/// <summary>
/// Apply AutoPersistence to all objects in a collection. Items that are
/// no longer in the collection won't be persisted anymore.
/// </summary>
/// <param name="doPersist">The asynchronous method to call to save the
/// object to disk.</param>
/// <param name="interval">The interval to save the object on. Note that
/// if an object is constantly changing, it is possible that it will never
/// be saved.</param>
/// <returns>A Disposable to disable automatic persistence.</returns>
public static IDisposable AutoPersistCollection<T>(this ReactiveList<T> This, Func<T, IObservable<Unit>> doPersist, TimeSpan? interval = null)
where T : IReactiveObject
{
return AutoPersistCollection(This, doPersist, Observable.Never<Unit>(), interval);
}
/// <summary>
/// Apply AutoPersistence to all objects in a collection. Items that are
/// no longer in the collection won't be persisted anymore.
/// </summary>
/// <param name="doPersist">The asynchronous method to call to save the
/// object to disk.</param>
/// <param name="manualSaveSignal">When invoked, the object will be saved
/// regardless of whether it has changed.</param>
/// <param name="interval">The interval to save the object on. Note that
/// if an object is constantly changing, it is possible that it will never
/// be saved.</param>
/// <returns>A Disposable to disable automatic persistence.</returns>
public static IDisposable AutoPersistCollection<T, TDontCare>(this ReactiveList<T> This, Func<T, IObservable<Unit>> doPersist, IObservable<TDontCare> manualSaveSignal, TimeSpan? interval = null)
where T : IReactiveObject
{
var disposerList = new Dictionary<T, IDisposable>();
var disp = This.ActOnEveryObject(
x => {
if (disposerList.ContainsKey(x)) return;
disposerList[x] = x.AutoPersist(doPersist, manualSaveSignal, interval);
},
x => {
disposerList[x].Dispose();
disposerList.Remove(x);
});
return Disposable.Create(() => {
disp.Dispose();
disposerList.Values.ForEach(x => x.Dispose());
});
}
/// <summary>
/// Call methods 'onAdd' and 'onRemove' whenever an object is added or
/// removed from a collection. This class correctly handles both when
/// a collection is initialized, as well as when the collection is Reset.
/// </summary>
/// <param name="onAdd">A method to be called when an object is added
/// to the collection.</param>
/// <param name="onRemove">A method to be called when an object is removed
/// from the collection.</param>
/// <returns>A Disposable that deactivates this behavior.</returns>
public static IDisposable ActOnEveryObject<T>(this ReactiveList<T> This, Action<T> onAdd, Action<T> onRemove)
where T : IReactiveObject
{
foreach (var v in This) { onAdd(v); }
var changingDisp = This.Changing
.Where(x => x.Action == NotifyCollectionChangedAction.Reset)
.Subscribe(
_ => This.ForEach(x => onRemove(x)));
var changedDisp = This.Changed.Subscribe(x => {
switch (x.Action) {
case NotifyCollectionChangedAction.Add:
foreach (T v in x.NewItems) { onAdd(v); }
break;
case NotifyCollectionChangedAction.Replace:
foreach (T v in x.OldItems) { onRemove(v); }
foreach (T v in x.NewItems) { onAdd(v); }
break;
case NotifyCollectionChangedAction.Remove:
foreach (T v in x.OldItems) { onRemove(v); }
break;
case NotifyCollectionChangedAction.Reset:
foreach (T v in This) { onAdd(v); }
break;
default:
break;
}
});
return Disposable.Create(() => {
changingDisp.Dispose();
changedDisp.Dispose();
This.ForEach(x => onRemove(x));
});
}
}
}