-
-
Notifications
You must be signed in to change notification settings - Fork 107
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
22 changed files
with
398 additions
and
153 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,35 @@ | ||
# R3 | ||
|
||
Third Generation of Reactive Extensions. | ||
|
||
```csharp | ||
public abstract class Event<TMessage> | ||
{ | ||
public IDisposable Subscribe(Subscriber<TMessage> subscriber); | ||
} | ||
|
||
public abstract class Subscriber<TMessage> : IDisposable | ||
{ | ||
public void OnNext(TMessage message); | ||
public void OnErrorResume(Exception error); | ||
} | ||
|
||
// Completable | ||
public abstract class CompletableEvent<TMessage, TComplete> | ||
{ | ||
public IDisposable Subscribe(Subscriber<TMessage, TComplete> subscriber) | ||
} | ||
|
||
public abstract class Subscriber<TMessage, TComplete> : IDisposable | ||
{ | ||
public void OnNext(TMessage message); | ||
public void OnErrorResume(Exception error); | ||
public void OnCompleted(TComplete complete); | ||
} | ||
``` | ||
|
||
```csharp | ||
// similar as IObserver<T> | ||
CompletableEvent<TMessage, Result<TComplete>> | ||
``` | ||
|
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,15 @@ | ||
namespace R3; | ||
|
||
public sealed class CancellationDisposable(CancellationTokenSource cancellationTokenSource) : IDisposable | ||
{ | ||
public CancellationDisposable() | ||
: this(new CancellationTokenSource()) | ||
{ | ||
} | ||
|
||
public CancellationToken Token => cancellationTokenSource.Token; | ||
|
||
public bool IsDisposed => cancellationTokenSource.IsCancellationRequested; | ||
|
||
public void Dispose() => cancellationTokenSource.Cancel(); | ||
} |
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,40 @@ | ||
namespace R3 | ||
{ | ||
public static partial class EventFactory | ||
{ | ||
public static CompletableEvent<TMessage, Result<Unit>> ToCompletableEvent<TMessage>(this Task<TMessage> task) | ||
{ | ||
return new R3.Factories.ToCompletableEvent<TMessage>(task); | ||
} | ||
} | ||
} | ||
|
||
namespace R3.Factories | ||
{ | ||
internal sealed class ToCompletableEvent<TMessage>(Task<TMessage> task) : CompletableEvent<TMessage, Result<Unit>> | ||
{ | ||
protected override IDisposable SubscribeCore(Subscriber<TMessage, Result<Unit>> subscriber) | ||
{ | ||
var subscription = new CancellationDisposable(); | ||
SubscribeTask(subscriber, subscription.Token); | ||
return subscription; | ||
} | ||
|
||
async void SubscribeTask(Subscriber<TMessage, Result<Unit>> subscriber, CancellationToken cancellationToken) | ||
{ | ||
TMessage? result; | ||
try | ||
{ | ||
result = await task.WaitAsync(cancellationToken); | ||
} | ||
catch (Exception ex) | ||
{ | ||
subscriber.OnCompleted(Result.Failure<Unit>(ex)); | ||
return; | ||
} | ||
|
||
subscriber.OnNext(result); | ||
subscriber.OnCompleted(Result.Success<Unit>(default)); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.