-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stream request middleware is ready for testing
- Loading branch information
Showing
14 changed files
with
159 additions
and
15 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,14 @@ | ||
namespace Sample.Handlers; | ||
|
||
public class MyStreamRequestMiddleware<TRequest, TResult> : IStreamRequestMiddleware<TRequest, TResult> where TRequest : IStreamRequest<TResult> | ||
{ | ||
public IAsyncEnumerator<TResult> Process( | ||
TRequest request, | ||
StreamRequestDelegate<TResult> next, | ||
IStreamRequestHandler<TRequest, TResult> requestHandler, | ||
CancellationToken cancellationToken | ||
) | ||
{ | ||
return next(); | ||
} | ||
} |
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Text.Json; | ||
|
||
namespace Shiny.Mediator.Middleware; | ||
|
||
/// <summary> | ||
/// Replays the last result before requesting a new one | ||
/// </summary> | ||
/// <typeparam name="TRequest"></typeparam> | ||
/// <typeparam name="TResult"></typeparam> | ||
public class ReplayStreamMiddleware<TRequest, TResult> : IStreamRequestMiddleware<TRequest, TResult> | ||
where TRequest : IStreamRequest<TResult> | ||
{ | ||
public IAsyncEnumerator<TResult> Process( | ||
TRequest request, | ||
StreamRequestDelegate<TResult> next, | ||
IStreamRequestHandler<TRequest, TResult> requestHandler, | ||
CancellationToken cancellationToken | ||
) | ||
{ | ||
var attribute = requestHandler.GetHandlerHandleMethodAttribute<TRequest, ReplayAttribute>(); | ||
if (attribute == null) | ||
return next(); | ||
|
||
return this.Iterate(request, next, cancellationToken); | ||
} | ||
|
||
|
||
protected virtual async IAsyncEnumerator<TResult> Iterate(TRequest request, StreamRequestDelegate<TResult> next, CancellationToken ct) | ||
{ | ||
var path = this.GetCacheFilePath(request); | ||
if (File.Exists(path)) | ||
{ | ||
var json = File.ReadAllText(path); | ||
var obj = JsonSerializer.Deserialize<TResult>(json); | ||
yield return obj; | ||
} | ||
|
||
var nxt = next(); | ||
while (await nxt.MoveNextAsync() && !ct.IsCancellationRequested) | ||
{ | ||
var json = JsonSerializer.Serialize(nxt.Current); | ||
File.WriteAllText(path, json); | ||
yield return nxt.Current; | ||
} | ||
} | ||
|
||
protected virtual string GetCacheFilePath(TRequest request) | ||
{ | ||
if (request is IReplayKey<TResult> key) | ||
return key.Key; | ||
|
||
var t = request.GetType(); | ||
return $"{t.Namespace}_{t.Name}.replay"; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Shiny.Mediator/Middleware/TimerRefreshStreamRequestMiddleware.cs
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,32 @@ | ||
namespace Shiny.Mediator.Middleware; | ||
|
||
public class TimerRefreshStreamRequestMiddleware<TRequest, TResult> : IStreamRequestMiddleware<TRequest, TResult> | ||
where TRequest : IStreamRequest<TResult> | ||
{ | ||
public IAsyncEnumerator<TResult> Process( | ||
TRequest request, | ||
StreamRequestDelegate<TResult> next, | ||
IStreamRequestHandler<TRequest, TResult> requestHandler, | ||
CancellationToken cancellationToken | ||
) | ||
{ | ||
var attribute = requestHandler.GetHandlerHandleMethodAttribute<TRequest, TimerRefreshAttribute>(); | ||
if (attribute == null) | ||
return next(); | ||
|
||
return Iterate(attribute, next, cancellationToken); | ||
} | ||
|
||
|
||
async IAsyncEnumerator<TResult> Iterate(TimerRefreshAttribute attribute, StreamRequestDelegate<TResult> next, CancellationToken ct) | ||
{ | ||
while (!ct.IsCancellationRequested) | ||
{ | ||
await Task.Delay(attribute.RefreshSeconds, ct); | ||
|
||
var nxt = next(); | ||
while (await nxt.MoveNextAsync() && !ct.IsCancellationRequested) | ||
yield return nxt.Current; | ||
} | ||
} | ||
} |
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,17 @@ | ||
namespace Shiny.Mediator; | ||
|
||
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)] | ||
public class TimerRefreshAttribute(int refreshSeconds, bool ignoreErrors = true) : Attribute | ||
{ | ||
public int RefreshSeconds => refreshSeconds; | ||
public bool IgnoreErrors => ignoreErrors; | ||
} | ||
|
||
|
||
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)] | ||
public class ReplayAttribute : Attribute {} | ||
|
||
public interface IReplayKey<TResult> : IStreamRequest<TResult> | ||
{ | ||
string Key { get; } | ||
} |
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