Skip to content

Commit 65f5398

Browse files
committed
Add SettingsManager.ObservePropertyChanged
1 parent d959d60 commit 65f5398

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

StabilityMatrix.Core/Services/ISettingsManager.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ IDisposable RegisterPropertyChangedHandler<T>(
9191
Action<T> onPropertyChanged
9292
);
9393

94+
/// <summary>
95+
/// Creates an observable sequence that notifies when the specified settings property changes.
96+
/// Emits the initial value upon subscription and subsequent changes.
97+
/// </summary>
98+
/// <typeparam name="T">The type of the property value.</typeparam>
99+
/// <param name="settingsProperty">An expression representing the settings property to observe.</param>
100+
/// <returns>An observable sequence of the property's values.</returns>
101+
IObservable<T> ObservePropertyChanged<T>(Expression<Func<Settings, T>> settingsProperty);
102+
94103
/// <summary>
95104
/// Attempts to locate and set the library path
96105
/// Return true if found, false otherwise

StabilityMatrix.Core/Services/SettingsManager.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,20 @@ Action<T> onPropertyChanged
301301
});
302302
}
303303

304+
/// <inheritdoc />
305+
public IObservable<T> ObservePropertyChanged<T>(Expression<Func<Settings, T>> settingsProperty)
306+
{
307+
var settingsAccessor = CompiledExpression.CreateAccessor(settingsProperty);
308+
309+
return Observable
310+
.FromEventPattern<EventHandler<RelayPropertyChangedEventArgs>, RelayPropertyChangedEventArgs>(
311+
h => SettingsPropertyChanged += h,
312+
h => SettingsPropertyChanged -= h
313+
)
314+
.Where(args => args.EventArgs.PropertyName == settingsAccessor.FullName)
315+
.Select(_ => settingsAccessor.Get(Settings));
316+
}
317+
304318
/// <summary>
305319
/// Attempts to locate and set the library path
306320
/// Return true if found, false otherwise

0 commit comments

Comments
 (0)