-
-
Notifications
You must be signed in to change notification settings - Fork 110
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
8 changed files
with
149 additions
and
20 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,31 @@ | ||
namespace R3; | ||
|
||
public static partial class Observable | ||
{ | ||
public static Observable<T> Create<T>(Func<Observer<T>, IDisposable> subscribe) | ||
{ | ||
return new AnonymousObservable<T>(subscribe); | ||
} | ||
|
||
public static Observable<T> Create<T, TState>(TState state, Func<Observer<T>, TState, IDisposable> subscribe) | ||
{ | ||
return new AnonymousObservable<T, TState>(state, subscribe); | ||
} | ||
} | ||
|
||
internal sealed class AnonymousObservable<T>(Func<Observer<T>, IDisposable> subscribe) : Observable<T> | ||
{ | ||
protected override IDisposable SubscribeCore(Observer<T> observer) | ||
{ | ||
return subscribe(observer); | ||
} | ||
} | ||
|
||
internal sealed class AnonymousObservable<T, TState>(TState state, Func<Observer<T>, TState, IDisposable> subscribe) : Observable<T> | ||
{ | ||
protected override IDisposable SubscribeCore(Observer<T> observer) | ||
{ | ||
return subscribe(observer, state); | ||
} | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
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,60 @@ | ||
| ||
namespace R3.Tests.FactoryTests; | ||
|
||
public class CreateTest | ||
{ | ||
[Fact] | ||
public void Create() | ||
{ | ||
var source = Observable.Create<int>(observer => | ||
{ | ||
observer.OnNext(1); | ||
observer.OnNext(10); | ||
observer.OnNext(100); | ||
observer.OnCompleted(); | ||
return Disposable.Empty; | ||
}); | ||
|
||
source.ToLiveList().AssertEqual([1, 10, 100]); | ||
} | ||
|
||
[Fact] | ||
public void CreateS() | ||
{ | ||
using var publisher = new Subject<int>(); | ||
var source = Observable.Create<int, Subject<int>>(publisher, (observer, state) => | ||
{ | ||
return state.Subscribe(new Wrap<int>(observer)); | ||
}); | ||
|
||
using var list = source.ToLiveList(); | ||
|
||
publisher.OnNext(1); | ||
list.AssertEqual([1]); | ||
publisher.OnNext(10); | ||
list.AssertEqual([1, 10]); | ||
publisher.OnNext(100); | ||
list.AssertEqual([1, 10, 100]); | ||
|
||
publisher.OnCompleted(); | ||
list.AssertIsCompleted(); | ||
} | ||
} | ||
|
||
file class Wrap<T>(Observer<T> observer) : Observer<T> | ||
{ | ||
protected override void OnCompletedCore(Result result) | ||
{ | ||
observer.OnCompleted(result); | ||
} | ||
|
||
protected override void OnErrorResumeCore(Exception error) | ||
{ | ||
observer.OnErrorResume(error); | ||
} | ||
|
||
protected override void OnNextCore(T value) | ||
{ | ||
observer.OnNext(value); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -21,3 +21,5 @@ public void Test() | |
list.AssertEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); | ||
} | ||
} | ||
|
||
|