-
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
163 additions
and
66 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
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
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,54 @@ | ||
namespace R3; | ||
|
||
public static class ReactivePropertyExtensions | ||
{ | ||
public static ReadOnlyReactiveProperty<T> ToReadOnlyReactiveProperty<T>(this Observable<T> source, T initialValue = default!) | ||
{ | ||
return source.ToReadOnlyReactiveProperty(EqualityComparer<T>.Default, initialValue); | ||
} | ||
|
||
public static ReadOnlyReactiveProperty<T> ToReadOnlyReactiveProperty<T>(this Observable<T> source, EqualityComparer<T>? equalityComparer, T initialValue = default!) | ||
{ | ||
if (source is ReadOnlyReactiveProperty<T> rrp) | ||
{ | ||
return rrp; | ||
} | ||
|
||
// for use hack, allow to cast ReactiveProperty<T> | ||
return new ConnectedReactiveProperty<T>(source, initialValue, equalityComparer); | ||
} | ||
} | ||
|
||
internal sealed class ConnectedReactiveProperty<T> : ReactiveProperty<T> | ||
{ | ||
readonly IDisposable sourceSubscription; | ||
|
||
public ConnectedReactiveProperty(Observable<T> source, T initialValue, EqualityComparer<T>? equalityComparer) | ||
: base(initialValue, equalityComparer) | ||
{ | ||
this.sourceSubscription = source.Subscribe(new Observer(this)); | ||
} | ||
|
||
protected override void DisposeCore() | ||
{ | ||
sourceSubscription.Dispose(); | ||
} | ||
|
||
class Observer(ConnectedReactiveProperty<T> parent) : Observer<T> | ||
{ | ||
protected override void OnNextCore(T value) | ||
{ | ||
parent.Value = value; | ||
} | ||
|
||
protected override void OnErrorResumeCore(Exception error) | ||
{ | ||
parent.OnErrorResume(error); | ||
} | ||
|
||
protected override void OnCompletedCore(Result result) | ||
{ | ||
parent.OnCompleted(result); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.