-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from Cysharp/observe-property
Add ObservePropertyChanged, ObservePropertyChanging for `INotifyPropertyChanged`, `INotifyPropertyChanging`
- Loading branch information
Showing
3 changed files
with
304 additions
and
11 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
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,199 @@ | ||
using System.ComponentModel; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace R3; | ||
|
||
public static partial class Observable | ||
{ | ||
/// <summary> | ||
/// Convert INotifyPropertyChanged to Observable. | ||
/// `propertySelector` must be a Func specifying a simple property. For example, it extracts "Foo" from `x => x.Foo`. | ||
/// </summary> | ||
public static Observable<TProperty> ObservePropertyChanged<T, TProperty>(this T value, | ||
Func<T, TProperty> propertySelector, | ||
bool pushCurrentValueOnSubscribe = true, | ||
CancellationToken cancellationToken = default, | ||
[CallerArgumentExpression("propertySelector")] string? expr = null) | ||
where T : INotifyPropertyChanged | ||
{ | ||
if (expr == null) throw new ArgumentNullException(expr); | ||
|
||
var propertyName = expr!.Substring(expr.LastIndexOf('.') + 1); | ||
return new ObservePropertyChanged<T, TProperty>(value, propertySelector, propertyName, pushCurrentValueOnSubscribe, cancellationToken); | ||
} | ||
|
||
/// <summary> | ||
/// Convert INotifyPropertyChanging to Observable. | ||
/// `propertySelector` must be a Func specifying a simple property. For example, it extracts "Foo" from `x => x.Foo`. | ||
/// </summary> | ||
public static Observable<TProperty> ObservePropertyChanging<T, TProperty>(this T value, | ||
Func<T, TProperty> propertySelector, | ||
bool pushCurrentValueOnSubscribe = true, | ||
CancellationToken cancellationToken = default, | ||
[CallerArgumentExpression("propertySelector")] string? expr = null) | ||
where T : INotifyPropertyChanging | ||
{ | ||
if (expr == null) throw new ArgumentNullException(expr); | ||
|
||
var propertyName = expr!.Substring(expr.LastIndexOf('.') + 1); | ||
return new ObservePropertyChanging<T, TProperty>(value, propertySelector, propertyName, pushCurrentValueOnSubscribe, cancellationToken); | ||
} | ||
} | ||
|
||
internal sealed class ObservePropertyChanged<T, TProperty>(T value, Func<T, TProperty> propertySelector, string propertyName, bool pushCurrentValueOnSubscribe, CancellationToken cancellationToken) | ||
: Observable<TProperty> where T : INotifyPropertyChanged | ||
{ | ||
protected override IDisposable SubscribeCore(Observer<TProperty> observer) | ||
{ | ||
if (pushCurrentValueOnSubscribe) | ||
{ | ||
observer.OnNext(propertySelector(value)); | ||
} | ||
|
||
return new _ObservePropertyChanged(observer, value, propertySelector, propertyName, cancellationToken); | ||
} | ||
|
||
sealed class _ObservePropertyChanged : IDisposable | ||
{ | ||
readonly Observer<TProperty> observer; | ||
readonly T value; | ||
readonly Func<T, TProperty> propertySelector; | ||
readonly string propertyName; | ||
PropertyChangedEventHandler? eventHandler; | ||
CancellationTokenRegistration cancellationTokenRegistration; | ||
|
||
public _ObservePropertyChanged(Observer<TProperty> observer, T value, Func<T, TProperty> propertySelector, string propertyName, CancellationToken cancellationToken) | ||
{ | ||
this.observer = observer; | ||
this.value = value; | ||
this.propertySelector = propertySelector; | ||
this.propertyName = propertyName; | ||
this.eventHandler = PublishOnNext; | ||
|
||
value.PropertyChanged += eventHandler; | ||
|
||
if (cancellationToken.CanBeCanceled) | ||
{ | ||
this.cancellationTokenRegistration = cancellationToken.UnsafeRegister(static state => | ||
{ | ||
var s = (_ObservePropertyChanged)state!; | ||
s.CompleteDispose(); | ||
}, this); | ||
} | ||
} | ||
|
||
void PublishOnNext(object? sender, PropertyChangedEventArgs e) | ||
{ | ||
if (e.PropertyName == propertyName) | ||
{ | ||
TProperty prop; | ||
try | ||
{ | ||
prop = propertySelector(value); | ||
} | ||
catch (Exception ex) | ||
{ | ||
observer.OnErrorResume(ex); | ||
return; | ||
} | ||
|
||
observer.OnNext(prop); | ||
} | ||
} | ||
|
||
void CompleteDispose() | ||
{ | ||
observer.OnCompleted(); | ||
Dispose(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
var handler = Interlocked.Exchange(ref eventHandler, null); | ||
if (handler != null) | ||
{ | ||
cancellationTokenRegistration.Dispose(); | ||
value.PropertyChanged -= eventHandler; | ||
} | ||
} | ||
} | ||
} | ||
|
||
internal sealed class ObservePropertyChanging<T, TProperty>(T value, Func<T, TProperty> propertySelector, string propertyName, bool pushCurrentValueOnSubscribe, CancellationToken cancellationToken) | ||
: Observable<TProperty> where T : INotifyPropertyChanging | ||
{ | ||
protected override IDisposable SubscribeCore(Observer<TProperty> observer) | ||
{ | ||
if (pushCurrentValueOnSubscribe) | ||
{ | ||
observer.OnNext(propertySelector(value)); | ||
} | ||
|
||
return new _ObservePropertyChanged(observer, value, propertySelector, propertyName, cancellationToken); | ||
} | ||
|
||
sealed class _ObservePropertyChanged : IDisposable | ||
{ | ||
readonly Observer<TProperty> observer; | ||
readonly T value; | ||
readonly Func<T, TProperty> propertySelector; | ||
readonly string propertyName; | ||
PropertyChangingEventHandler? eventHandler; | ||
CancellationTokenRegistration cancellationTokenRegistration; | ||
|
||
public _ObservePropertyChanged(Observer<TProperty> observer, T value, Func<T, TProperty> propertySelector, string propertyName, CancellationToken cancellationToken) | ||
{ | ||
this.observer = observer; | ||
this.value = value; | ||
this.propertySelector = propertySelector; | ||
this.propertyName = propertyName; | ||
this.eventHandler = PublishOnNext; | ||
|
||
value.PropertyChanging += eventHandler; | ||
|
||
if (cancellationToken.CanBeCanceled) | ||
{ | ||
this.cancellationTokenRegistration = cancellationToken.UnsafeRegister(static state => | ||
{ | ||
var s = (_ObservePropertyChanged)state!; | ||
s.CompleteDispose(); | ||
}, this); | ||
} | ||
} | ||
|
||
void PublishOnNext(object? sender, PropertyChangingEventArgs e) | ||
{ | ||
if (e.PropertyName == propertyName) | ||
{ | ||
TProperty prop; | ||
try | ||
{ | ||
prop = propertySelector(value); | ||
} | ||
catch (Exception ex) | ||
{ | ||
observer.OnErrorResume(ex); | ||
return; | ||
} | ||
|
||
observer.OnNext(prop); | ||
} | ||
} | ||
|
||
void CompleteDispose() | ||
{ | ||
observer.OnCompleted(); | ||
Dispose(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
var handler = Interlocked.Exchange(ref eventHandler, null); | ||
if (handler != null) | ||
{ | ||
cancellationTokenRegistration.Dispose(); | ||
value.PropertyChanging -= eventHandler; | ||
} | ||
} | ||
} | ||
} |
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,77 @@ | ||
using System.ComponentModel; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace R3.Tests.FactoryTests; | ||
|
||
public class ObservePropertyTest | ||
{ | ||
[Fact] | ||
public void PropertyChanged() | ||
{ | ||
ChangesProperty propertyChanger = new(); | ||
|
||
using var liveList = propertyChanger | ||
.ObservePropertyChanged(x => x.Value) | ||
.ToLiveList(); | ||
|
||
liveList.AssertEqual([0]); | ||
|
||
propertyChanger.Value = 1; | ||
|
||
liveList.AssertEqual([0, 1]); | ||
} | ||
|
||
[Fact] | ||
public void PropertyChanging() | ||
{ | ||
ChangesProperty propertyChanger = new(); | ||
|
||
using var liveList = propertyChanger | ||
.ObservePropertyChanging(x => x.Value) | ||
.ToLiveList(); | ||
|
||
liveList.AssertEqual([0]); | ||
|
||
propertyChanger.Value = 1; | ||
|
||
liveList.AssertEqual([0, 0]); | ||
} | ||
|
||
class ChangesProperty : INotifyPropertyChanged, INotifyPropertyChanging | ||
{ | ||
private int _value; | ||
|
||
public event PropertyChangedEventHandler? PropertyChanged; | ||
public event PropertyChangingEventHandler? PropertyChanging; | ||
|
||
public int Value | ||
{ | ||
get => _value; | ||
set => SetField(ref _value, value); | ||
} | ||
|
||
private void OnPropertyChanged([CallerMemberName] string? propertyName = null) | ||
{ | ||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); | ||
} | ||
|
||
private void OnPropertyChanging([CallerMemberName] string? propertyName = null) | ||
{ | ||
PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(propertyName)); | ||
} | ||
|
||
private bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null) | ||
{ | ||
if (EqualityComparer<T>.Default.Equals(field, value)) | ||
{ | ||
return false; | ||
} | ||
|
||
OnPropertyChanging(propertyName); | ||
field = value; | ||
OnPropertyChanged(propertyName); | ||
return true; | ||
} | ||
|
||
} | ||
} |