Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add methods to automatically save objects on a timer
- Loading branch information
1 parent
c5bd1f6
commit a90c770
Showing
5 changed files
with
232 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reactive; | ||
using System.Reactive.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.Reactive.Testing; | ||
using ReactiveUI.Testing; | ||
using Xunit; | ||
using System.Reactive.Subjects; | ||
|
||
namespace ReactiveUI.Tests | ||
{ | ||
public class AutoPersistHelperTest | ||
{ | ||
[Fact] | ||
public void AutoPersistDoesntWorkOnNonDataContractClasses() | ||
{ | ||
var fixture = new FakeViewModel(); | ||
|
||
bool shouldDie = true; | ||
try { | ||
fixture.AutoPersist(x => Observable.Return(Unit.Default)); | ||
} catch (Exception ex) { | ||
shouldDie = false; | ||
} | ||
|
||
Assert.False(shouldDie); | ||
} | ||
|
||
[Fact] | ||
public void AutoPersistHelperShouldntTriggerOnNonPersistableProperties() | ||
{ | ||
(new TestScheduler()).With(sched => { | ||
var fixture = new TestFixture(); | ||
var manualSave = new Subject<Unit>(); | ||
int timesSaved = 0; | ||
fixture.AutoPersist(x => { timesSaved++; return Observable.Return(Unit.Default); }, manualSave, TimeSpan.FromMilliseconds(100)); | ||
// No changes = no saving | ||
sched.AdvanceByMs(2 * 100); | ||
Assert.Equal(0, timesSaved); | ||
// Change to not serialized = no saving | ||
fixture.NotSerialized = "Foo"; | ||
sched.AdvanceByMs(2 * 100); | ||
Assert.Equal(0, timesSaved); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void AutoPersistHelperSavesOnInterval() | ||
{ | ||
(new TestScheduler()).With(sched => { | ||
var fixture = new TestFixture(); | ||
var manualSave = new Subject<Unit>(); | ||
int timesSaved = 0; | ||
fixture.AutoPersist(x => { timesSaved++; return Observable.Return(Unit.Default); }, manualSave, TimeSpan.FromMilliseconds(100)); | ||
// No changes = no saving | ||
sched.AdvanceByMs(2 * 100); | ||
Assert.Equal(0, timesSaved); | ||
// Change = one save | ||
fixture.IsNotNullString = "Foo"; | ||
sched.AdvanceByMs(2 * 100); | ||
Assert.Equal(1, timesSaved); | ||
// Two fast changes = one save | ||
fixture.IsNotNullString = "Foo"; | ||
fixture.IsNotNullString = "Bar"; | ||
sched.AdvanceByMs(2 * 100); | ||
Assert.Equal(2, timesSaved); | ||
// Trigger save twice = one save | ||
manualSave.OnNext(Unit.Default); | ||
manualSave.OnNext(Unit.Default); | ||
sched.AdvanceByMs(2 * 100); | ||
Assert.Equal(3, timesSaved); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
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; | ||
|
||
namespace ReactiveUI | ||
{ | ||
public static class AutoPersistHelper | ||
{ | ||
static MemoizingMRUCache<Type, Dictionary<string, bool>> persistablePropertiesCache = new MemoizingMRUCache<Type, Dictionary<string, bool>>((type, _) => { | ||
return type.GetProperties() | ||
.Where(x => x.CustomAttributes.Any(y => typeof(DataMemberAttribute).IsAssignableFrom(y.AttributeType))) | ||
.ToDictionary(k => k.Name, v => true); | ||
}, 32); | ||
|
||
static MemoizingMRUCache<Type, bool> dataContractCheckCache = new MemoizingMRUCache<Type, bool>((t, _) => { | ||
return t.GetCustomAttributes(typeof(DataContractAttribute), true).Any(); | ||
}, 64); | ||
|
||
public static IDisposable AutoPersist<T>(this T This, Func<T, IObservable<Unit>> doPersist, TimeSpan? interval = null) | ||
where T : IReactiveNotifyPropertyChanged | ||
{ | ||
return This.AutoPersist(doPersist, Observable.Never<Unit>()); | ||
} | ||
|
||
public static IDisposable AutoPersist<T, TDontCare>(this T This, Func<T, IObservable<Unit>> doPersist, IObservable<TDontCare> manualSaveSignal, TimeSpan? interval = null) | ||
where T : IReactiveNotifyPropertyChanged | ||
{ | ||
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.Changed.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.DeferredScheduler.Schedule(() => { | ||
if (ret.IsDisposed) return; | ||
ret.Disposable = autoSaver.Connect(); | ||
}); | ||
|
||
return ret; | ||
} | ||
|
||
public static IDisposable AutoPersistCollection<T, TDontCare>(this ReactiveCollection<T> This, Func<T, IObservable<Unit>> doPersist, IObservable<TDontCare> manualSaveSignal) | ||
where T : IReactiveNotifyPropertyChanged | ||
{ | ||
var disposerList = new Dictionary<T, IDisposable>(); | ||
|
||
var disp = This.ActOnEveryObject( | ||
x => { | ||
if (disposerList.ContainsKey(x)) return; | ||
disposerList[x] = x.AutoPersist(doPersist, manualSaveSignal); | ||
}, | ||
x => { | ||
disposerList[x].Dispose(); | ||
disposerList.Remove(x); | ||
}); | ||
|
||
return Disposable.Create(() => { | ||
disp.Dispose(); | ||
disposerList.Values.ForEach(x => x.Dispose()); | ||
}); | ||
} | ||
|
||
public static IDisposable ActOnEveryObject<T>(this ReactiveCollection<T> This, Action<T> onAdd, Action<T> onRemove) | ||
where T : IReactiveNotifyPropertyChanged | ||
{ | ||
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)); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters