Skip to content

Commit

Permalink
Bugfixes and update to blazor sample
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Oct 4, 2024
1 parent 20f61d3 commit f45f5f4
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 31 deletions.
2 changes: 1 addition & 1 deletion samples/Sample.Blazor/Contracts/DoThing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

namespace Sample.Blazor.Contracts;

public record DoThing(string Text) : IRequest;
public record DoThing(string Text) : IRequest<int>;
2 changes: 1 addition & 1 deletion samples/Sample.Blazor/Contracts/TheThing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

namespace Sample.Blazor.Contracts;

public class TheThing(string Text) : IEvent;
public record TheThing(string Text) : IEvent;
13 changes: 10 additions & 3 deletions samples/Sample.Blazor/Handlers/DoThingRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@
namespace Sample.Blazor.Handlers;

[SingletonHandler]
public class DoThingRequestHandler(IMediator mediator) : IRequestHandler<DoThing>
public class DoThingRequestHandler(IMediator mediator) : IRequestHandler<DoThing, int>
{
public Task Handle(DoThing request, CancellationToken cancellationToken)
=> mediator.Publish(new TheThing(request.Text), cancellationToken);
[OfflineAvailable]
public async Task<int> Handle(DoThing request, CancellationToken cancellationToken)
{
var num = new Random().Next(1, 1000000);
var value = $"{request.Text} - number: {num}";
await mediator.Publish(new TheThing(value), cancellationToken);

return num;
}
}
40 changes: 34 additions & 6 deletions samples/Sample.Blazor/Pages/Home.razor
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,54 @@ Welcome to your new app.

<input type="text" @bind-value="this.text" />
<button @onclick="TestSend">Test Send</button>

@if (this.text != null)
@if (this.resultText != null)
{
<br />
<text>Text from Mediator: @this.text</text>
<text>Result from Mediator: @this.resultText</text>
}
@if (this.eventText != null)
{
<br />
<text>Event from Mediator: @this.eventText</text>
}
@if (this.offlineText != null)
{
<br />
<text>Value is from Offline: @this.offlineText</text>
}


@code {
string text;

string? text;
string? resultText;
string? eventText;
string? offlineText;

async Task TestSend()
{
await mediator.Send(new DoThing(this.text));
this.eventText = null;
this.offlineText = null;
this.resultText = null;

try
{
var context = await mediator.RequestWithContext(new DoThing(this.text ?? "No Text Sent"));
this.resultText = context.Result.ToString();
var off = context.Context.Offline();
if (off != null)
this.offlineText = $"Data From: {off.Timestamp.ToLocalTime():h:mm:ss tt} - Key: {off.RequestKey}";
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
this.text = null;
}

public Task Handle(TheThing @event, CancellationToken cancellationToken)
{
Console.WriteLine("Received TheThing");
this.eventText = @event.Text;
return Task.CompletedTask;
}
}
1 change: 1 addition & 0 deletions samples/Sample.Blazor/Sample.Blazor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Shiny.Mediator.Caching\Shiny.Mediator.Caching.csproj" />
<ProjectReference Include="..\..\src\Shiny.Mediator\Shiny.Mediator.csproj"/>
<ProjectReference Include="..\..\src\Shiny.Mediator.AppSupport\Shiny.Mediator.AppSupport.csproj"/>
<ProjectReference Include="..\..\src\Shiny.Mediator.Blazor\Shiny.Mediator.Blazor.csproj"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace Shiny.Mediator.Infrastructure;
public interface IStorageService
{
Task Set<T>(string key, T value);
Task<T> Get<T>(string key);
Task<T?> Get<T>(string key);
Task Remove(string key);
}
8 changes: 4 additions & 4 deletions src/Shiny.Mediator.Blazor/Infrastructure/InternetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ namespace Shiny.Mediator.Blazor.Infrastructure;

public class InternetService(IJSRuntime jsruntime) : IInternetService, IDisposable
{
public bool IsAvailable => ((IJSInProcessRuntime)jsruntime).Invoke<bool>("navigator.onLine");
public bool IsAvailable => ((IJSInProcessRuntime)jsruntime).Invoke<bool>("MediatorServices.isOnline");


[JSInvokable("InternetService.OnStatusChanged")]
[JSInvokable("MediatorServices.OnStatusChanged")]
public void OnStatusChanged(bool isOnline)
{
if (isOnline)
Expand All @@ -24,12 +24,12 @@ public async Task WaitForAvailable(CancellationToken cancelToken = default)
return;

var objRef = DotNetObjectReference.Create(this);
((IJSInProcessRuntime)jsruntime).InvokeVoid("InternetService.subscribe", objRef);
((IJSInProcessRuntime)jsruntime).InvokeVoid("MediatorServices.subscribe", objRef);

this.waitSource = new();
await this.waitSource.Task.ConfigureAwait(false);
this.waitSource = null;
}

public void Dispose() => ((IJSInProcessRuntime)jsruntime).InvokeVoid("InternetService.unsubscribe");
public void Dispose() => ((IJSInProcessRuntime)jsruntime).InvokeVoid("MediatorServices.unsubscribe");
}
19 changes: 9 additions & 10 deletions src/Shiny.Mediator.Blazor/Infrastructure/StorageService.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
using System.Text.Json;
using Microsoft.JSInterop;
using Shiny.Mediator.Infrastructure;

namespace Shiny.Mediator.Blazor.Infrastructure;


public class StorageService(IJSRuntime jsruntime) : IStorageService
public class StorageService(IJSRuntime jsruntime, ISerializerService serializer) : IStorageService
{
public Task Set<T>(string key, T value)
{
var json = JsonSerializer.Serialize(value);
((IJSInProcessRuntime)jsruntime).Invoke<string?>("localStorage.setItem", key, json);
var json = serializer.Serialize(value);
((IJSInProcessRuntime)jsruntime).Invoke<string?>("MediatorServices.setStore", key, json);

return Task.FromResult(key);
}


public Task<T> Get<T>(string key)
public Task<T?> Get<T>(string key)
{
var stringValue = ((IJSInProcessRuntime)jsruntime).Invoke<string?>("localStorage.getItem", key);
var stringValue = ((IJSInProcessRuntime)jsruntime).Invoke<string?>("MediatorServices.getStore", key);
if (String.IsNullOrWhiteSpace(stringValue))
return null!;
return Task.FromResult<T?>(default);

var final = JsonSerializer.Deserialize<T>(stringValue);
return Task.FromResult(final);
var final = serializer.Deserialize<T>(stringValue);
return Task.FromResult<T?>(final);
}


public Task Remove(string key)
{
var inproc = (IJSInProcessRuntime)jsruntime;
inproc.InvokeVoid("localStorage.removeItem", key);
inproc.InvokeVoid("MediatorServices.removeStore", key);
return Task.CompletedTask;
}
}
20 changes: 18 additions & 2 deletions src/Shiny.Mediator.Blazor/wwwroot/Mediator.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
let handler;

window.InternetService = {
window.MediatorServices = {
isOnline: function() {
return navigator.onLine;
},

subscribe: function(interop) {

handler = function() {
interop.invokeMethodAsync("InternetService.OnStatusChanged", navigator.onLine);
interop.invokeMethodAsync("MediatorServices.OnStatusChanged", navigator.onLine);
}

window.addEventListener("online", handler);
Expand All @@ -17,5 +21,17 @@ window.InternetService = {

window.removeEventListener("online", handler);
window.removeEventListener("offline", handler);
},

setStore: function(key, value) {
localStorage.setItem(key, value);
},

getStore: function(key) {
return localStorage.getItem(key);
},

removeStore: function(key) {
localStorage.removeItem(key);
}
};
11 changes: 8 additions & 3 deletions tests/Shiny.Mediator.Tests/ConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ public void AddConfiguration()
{
// TODO: ORDER: config, attribute contract, attribute handler
// TODO: config order: full type, exact namespace, sub namespace, *
var dict = new Dictionary<string, object>();

}


ConfigurationManager SetupConfig(params (string Key, bool Enabled)[] keys)
{
var config = new ConfigurationManager();
// config.AddConfiguration(new MemoryConfigurationProvider(new MemoryConfigurationSource().InitialData));
// config.Get("key1").Should().Be("value1");
config.AddInMemoryCollection(keys.ToDictionary(x => x.Key, x => x.Enabled.ToString())!);
return config;
}
}

0 comments on commit f45f5f4

Please sign in to comment.