Skip to content

Commit

Permalink
Improve SSE supports.
Browse files Browse the repository at this point in the history
  • Loading branch information
kingcean committed Dec 26, 2024
1 parent 8bbe2de commit 4e056c0
Show file tree
Hide file tree
Showing 7 changed files with 543 additions and 49 deletions.
5 changes: 5 additions & 0 deletions Common/Data/PagingJsonLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ public BaseJsonPagingLoader(bool disableAutoRaise)
/// </summary>
public int PageIndex { get; private set; } = -1;

/// <summary>
/// Gets or sets the additional data.
/// </summary>
public object Tag { get; set; }

/// <summary>
/// Gets data.
/// </summary>
Expand Down
74 changes: 74 additions & 0 deletions Common/UI/Converters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,80 @@ protected virtual object CreateValue(object parameter, string language)
=> DependencyProperty.UnsetValue;
}

/// <summary>
/// The visibility converter about the value is not null or empty.
/// </summary>
public class EmptyToCollapseConverter : IValueConverter
{
/// <summary>
/// Converts a source to target.
/// </summary>
/// <param name="value">The input value to convert.</param>
/// <param name="targetType">The type of target.</param>
/// <param name="parameter">The converter parameter.</param>
/// <param name="language">The language code.</param>
/// <returns>The result converted.</returns>
public object Convert(object value, Type targetType, object parameter, string language)
=> VisualUtility.ConvertFromBoolean(value is string s ? !string.IsNullOrEmpty(s) : value is not null, targetType, true);

/// <summary>
/// Converts the source back.
/// </summary>
/// <param name="value">The input value to convert back.</param>
/// <param name="targetType">The type of target.</param>
/// <param name="parameter">The converter parameter.</param>
/// <param name="language">The language code.</param>
/// <returns>The result converted back.</returns>
public object ConvertBack(object value, Type targetType, object parameter, string language)
=> VisualUtility.ConvertToBoolean(value) != true ? null : CreateValue(parameter, language);

/// <summary>
/// Creates the value.
/// </summary>
/// <param name="parameter">The converter parameter.</param>
/// <param name="language">The language code.</param>
/// <returns>The result created to convert back.</returns>
protected virtual object CreateValue(object parameter, string language)
=> DependencyProperty.UnsetValue;
}

/// <summary>
/// The visibility converter about the value is null or empty.
/// </summary>
public class EmptyToVisibleConverter : IValueConverter
{
/// <summary>
/// Converts a source to target.
/// </summary>
/// <param name="value">The input value to convert.</param>
/// <param name="targetType">The type of target.</param>
/// <param name="parameter">The converter parameter.</param>
/// <param name="language">The language code.</param>
/// <returns>The result converted.</returns>
public object Convert(object value, Type targetType, object parameter, string language)
=> VisualUtility.ConvertFromBoolean(value is string s ? string.IsNullOrEmpty(s) : value is null, targetType, true);

/// <summary>
/// Converts the source back.
/// </summary>
/// <param name="value">The input value to convert back.</param>
/// <param name="targetType">The type of target.</param>
/// <param name="parameter">The converter parameter.</param>
/// <param name="language">The language code.</param>
/// <returns>The result converted back.</returns>
public object ConvertBack(object value, Type targetType, object parameter, string language)
=> VisualUtility.ConvertToBoolean(value) != false ? null : CreateValue(parameter, language);

/// <summary>
/// Creates the value.
/// </summary>
/// <param name="parameter">The converter parameter.</param>
/// <param name="language">The language code.</param>
/// <returns>The result created to convert back.</returns>
protected virtual object CreateValue(object parameter, string language)
=> DependencyProperty.UnsetValue;
}

/// <summary>
/// The two-way converter of visibility and boolean.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Console/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,5 @@ DefaultConsole.WriteLine(new LinearGradientConsoleStyle(
Color.FromArgb(85, 168, 255)) // To color
{
Bold = true // Additional font style
},"Trivial Sample");
}, "Trivial Sample");
```
14 changes: 13 additions & 1 deletion Site/Controllers/TestController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using System.Text.Json.Nodes;
using Trivial.Collection;
using Trivial.Data;
using Trivial.Net;
using Trivial.Security;
using Trivial.Text;
Expand Down Expand Up @@ -50,7 +51,18 @@ public ActionResult GetData()
[HttpGet("Stream")]
public IActionResult Stream()
{
var sse = DemoServer.Instance.StreamData(10);
var sse = DemoServer.Instance.StreamDataAsync(10);
return sse.ToActionResult();
}

[HttpGet("Jsonl")]
public IActionResult ListData()
{
var sse = DemoServer.Instance.ListDataAsync(10);
return sse.ToActionResult();
}

[HttpGet("Items")]
public IActionResult PushData()
=> ControllerExtensions.ToActionResult<JsonObjectNode>(DemoServer.Instance.AppendToAsync);
}
52 changes: 49 additions & 3 deletions Site/DemoServer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.CookiePolicy;
using System.Text.Json.Serialization;
using Trivial.Data;
using Trivial.Net;
using Trivial.Reflection;
using Trivial.Security;
Expand Down Expand Up @@ -52,18 +54,62 @@ protected override async Task<SelectionRelationship<UserModel, TokenInfo>> SignI
});
}

public async IAsyncEnumerable<ServerSentEventInfo> StreamData(int count)
public async IAsyncEnumerable<JsonObjectNode> ListDataAsync(int count)
{
for (var i = 0; i < count; i++)
{
await Task.Delay(1000);
yield return new ServerSentEventInfo("test-data", "message", new JsonObjectNode()
yield return new JsonObjectNode()
{
{ "action", "add" },
{ "value", Guid.NewGuid() },
{ "index", i }
};
}
}

public async IAsyncEnumerable<ServerSentEventInfo> StreamDataAsync(int count)
{
var col = ListDataAsync(count);
await foreach (var item in col)
{
yield return new ServerSentEventInfo("test-data", "message", item);
}
}

public async Task AppendToAsync(CollectionResultBuilder<JsonObjectNode> builder)
{
if (builder == null) return;
var col = ListDataAsync(10);
builder.SetTrackingId(Guid.NewGuid().ToString());
builder.PatchAdditionalInfo(new()
{
{ "name", "test data" },
{ "status", "streaming" },
{ "push", 0 },
});
builder.SetOffset(0, 10, "The item is streaming one by one.");
await foreach (var item in col)
{
builder.Add(item);
builder.PatchAdditionalInfo(new()
{
{ "push", builder.Result.CurrentCount }
});
}
builder.SetMessage("Stream all items successfully.");
builder.PatchAdditionalInfo(new()
{
{ "status", "done" }
});
builder.End();

// Following - nothing happened.
builder.PatchAdditionalInfo(new()
{
{ "status", "no more" }
});
builder.End();
}

public ISignatureProvider GetSignatureProvider<T>(T payload, string id)
Expand Down
Loading

0 comments on commit 4e056c0

Please sign in to comment.