Added TrackedProperty for commiting and reverting values
This commit is contained in:
@@ -17,6 +17,8 @@ public partial class Observable :
|
||||
IPublisherRequired,
|
||||
IDisposerRequired
|
||||
{
|
||||
private readonly Dictionary<string, object> trackedProperties = [];
|
||||
|
||||
[ObservableProperty]
|
||||
private bool isInitialized;
|
||||
|
||||
@@ -48,8 +50,13 @@ public partial class Observable :
|
||||
|
||||
public IPublisher Publisher { get; }
|
||||
|
||||
public virtual Task OnActivated() =>
|
||||
Task.CompletedTask;
|
||||
public void Commit()
|
||||
{
|
||||
foreach (object trackedProperty in trackedProperties.Values)
|
||||
{
|
||||
((dynamic)trackedProperty).Commit();
|
||||
}
|
||||
}
|
||||
|
||||
public Task Deactivate()
|
||||
{
|
||||
@@ -57,12 +64,6 @@ public partial class Observable :
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public virtual Task OnDeactivated() =>
|
||||
Task.CompletedTask;
|
||||
|
||||
public virtual Task OnDeactivating() =>
|
||||
Task.CompletedTask;
|
||||
|
||||
public virtual void Dispose()
|
||||
{
|
||||
GC.SuppressFinalize(this);
|
||||
@@ -79,6 +80,32 @@ public partial class Observable :
|
||||
IsInitialized = true;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public virtual Task OnActivated() =>
|
||||
Task.CompletedTask;
|
||||
|
||||
public virtual Task OnDeactivated() =>
|
||||
Task.CompletedTask;
|
||||
|
||||
public virtual Task OnDeactivating() =>
|
||||
Task.CompletedTask;
|
||||
|
||||
public void Revert()
|
||||
{
|
||||
foreach (object trackedProperty in trackedProperties.Values)
|
||||
{
|
||||
((dynamic)trackedProperty).Revert();
|
||||
}
|
||||
}
|
||||
|
||||
public void Track<T>(string propertyName, Func<T> getter, Action<T> setter)
|
||||
{
|
||||
if (!trackedProperties.ContainsKey(propertyName))
|
||||
{
|
||||
T initialValue = getter();
|
||||
trackedProperties[propertyName] = new TrackedProperty<T>(initialValue, setter, getter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Observable<TValue>(IServiceProvider provider,
|
||||
@@ -93,7 +120,6 @@ public partial class Observable<TValue>(IServiceProvider provider,
|
||||
private TValue? value = value;
|
||||
}
|
||||
|
||||
|
||||
public partial class Observable<TKey, TValue>(IServiceProvider provider,
|
||||
IServiceFactory factory,
|
||||
IMediator mediator,
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class TrackedProperty<T>(T initial,
|
||||
Action<T> revert,
|
||||
Func<T> commit)
|
||||
{
|
||||
public void Commit() => initial = commit();
|
||||
|
||||
public void Revert() => revert(initial);
|
||||
}
|
||||
Reference in New Issue
Block a user