Skip to content

Commit

Permalink
Added option to use a property as an observer
Browse files Browse the repository at this point in the history
  • Loading branch information
YohDeadfall committed Sep 6, 2024
1 parent 89123b8 commit 502e081
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/Kinetic/Property.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public T Get() =>
public void Set(T value) =>
Owner.Set(Offset, value);

public IObserver<T> Change =>
Owner.EnsureObservableFor<T>(Offset);

public IObservable<T> Changed =>
Owner.EnsureObservableFor<T>(Offset);

Expand Down
11 changes: 10 additions & 1 deletion src/Kinetic/PropertyObservable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected PropertyObservable(ObservableObject owner, IntPtr offset, PropertyObse

[DebuggerDisplay("Observers = {Subscriptions.GetObserversCountForDebugger()}")]
[DebuggerTypeProxy(typeof(PropertyObservableDebugView<>))]
internal sealed class PropertyObservable<T> : PropertyObservable, IObservableInternal<T>
internal sealed class PropertyObservable<T> : PropertyObservable, IObservableInternal<T>, IObserver<T>
{
internal ObservableSubscriptions<T> Subscriptions;

Expand All @@ -44,4 +44,13 @@ public void Subscribe(ObservableSubscription<T> subscription) =>

public void Unsubscribe(ObservableSubscription<T> subscription) =>
Subscriptions.Unsubscribe(subscription);

public void OnCompleted()
{ }

public void OnError(Exception error)
{ }

public void OnNext(T value) =>
Owner.Set(Offset, value);
}
19 changes: 19 additions & 0 deletions test/Kinetic.Tests/ObservableObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ public void PropertySubscription()
Assert.Equal(new[] { 0, 1, 2, 3 }, numbers);
}

[Fact]
public void PropertyChange()
{
var test = new TestObject();
var numbers = new List<int>();

using (test.Number.Changed.Subscribe(
value => numbers.Add(value)))
{
test.Number.Change.OnNext(1);
test.Number.Change.OnNext(2);
test.Number.Change.OnNext(3);
}

test.Number.Set(4);

Assert.Equal(new[] { 0, 1, 2, 3 }, numbers);
}

[Fact]
public void SuppressNotifications()
{
Expand Down

0 comments on commit 502e081

Please sign in to comment.