Migre le projet vers Blazor WebAssembly en .NET 10
This commit is contained in:
30
ChessCubing.App/Services/BrowserBridge.cs
Normal file
30
ChessCubing.App/Services/BrowserBridge.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using Microsoft.JSInterop;
|
||||
|
||||
namespace ChessCubing.App.Services;
|
||||
|
||||
public sealed class BrowserBridge(IJSRuntime jsRuntime)
|
||||
{
|
||||
public ValueTask StartViewportAsync()
|
||||
=> jsRuntime.InvokeVoidAsync("chesscubingViewport.start");
|
||||
|
||||
public ValueTask SetBodyStateAsync(string? page, string? bodyClass)
|
||||
=> jsRuntime.InvokeVoidAsync("chesscubingPage.setBodyState", page, bodyClass ?? string.Empty);
|
||||
|
||||
public ValueTask<string?> ReadMatchJsonAsync()
|
||||
=> jsRuntime.InvokeAsync<string?>("chesscubingStorage.getMatchState", MatchStore.StorageKey, MatchStore.WindowNameKey);
|
||||
|
||||
public ValueTask WriteMatchJsonAsync(string json)
|
||||
=> jsRuntime.InvokeVoidAsync("chesscubingStorage.setMatchState", MatchStore.StorageKey, MatchStore.WindowNameKey, json);
|
||||
|
||||
public ValueTask ClearMatchAsync()
|
||||
=> jsRuntime.InvokeVoidAsync("chesscubingStorage.clearMatchState", MatchStore.StorageKey);
|
||||
|
||||
public ValueTask<bool> PlayCubePhaseAlertAsync()
|
||||
=> jsRuntime.InvokeAsync<bool>("chesscubingAudio.playCubePhaseAlert");
|
||||
|
||||
public ValueTask PrimeAudioAsync()
|
||||
=> jsRuntime.InvokeVoidAsync("chesscubingAudio.prime");
|
||||
|
||||
public ValueTask ForceRefreshAsync(string path)
|
||||
=> jsRuntime.InvokeVoidAsync("chesscubingBrowser.forceRefresh", path);
|
||||
}
|
||||
1020
ChessCubing.App/Services/MatchEngine.cs
Normal file
1020
ChessCubing.App/Services/MatchEngine.cs
Normal file
File diff suppressed because it is too large
Load Diff
106
ChessCubing.App/Services/MatchStore.cs
Normal file
106
ChessCubing.App/Services/MatchStore.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using ChessCubing.App.Models;
|
||||
|
||||
namespace ChessCubing.App.Services;
|
||||
|
||||
public sealed class MatchStore(BrowserBridge browser)
|
||||
{
|
||||
public const string StorageKey = "chesscubing-arena-state-v2";
|
||||
public const string WindowNameKey = "chesscubing-arena-state-v2:";
|
||||
|
||||
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web)
|
||||
{
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||
};
|
||||
|
||||
private bool _dirty;
|
||||
private long _lastPersistedAt;
|
||||
|
||||
public MatchState? Current { get; private set; }
|
||||
|
||||
public bool IsLoaded { get; private set; }
|
||||
|
||||
public async Task EnsureLoadedAsync()
|
||||
{
|
||||
if (IsLoaded)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var raw = await browser.ReadMatchJsonAsync();
|
||||
if (!string.IsNullOrWhiteSpace(raw))
|
||||
{
|
||||
var parsed = JsonSerializer.Deserialize<MatchState>(raw, JsonOptions);
|
||||
if (parsed is not null && MatchEngine.IsSupportedSchemaVersion(parsed.SchemaVersion))
|
||||
{
|
||||
MatchEngine.NormalizeRecoveredMatch(parsed);
|
||||
Current = parsed;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
Current = null;
|
||||
}
|
||||
|
||||
IsLoaded = true;
|
||||
_lastPersistedAt = MatchEngine.NowUnixMs();
|
||||
}
|
||||
|
||||
public void SetCurrent(MatchState? match)
|
||||
{
|
||||
Current = match;
|
||||
MarkDirty();
|
||||
}
|
||||
|
||||
public void MarkDirty()
|
||||
=> _dirty = true;
|
||||
|
||||
public async Task SaveAsync()
|
||||
{
|
||||
if (!IsLoaded)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Current is null)
|
||||
{
|
||||
await browser.ClearMatchAsync();
|
||||
_dirty = false;
|
||||
_lastPersistedAt = MatchEngine.NowUnixMs();
|
||||
return;
|
||||
}
|
||||
|
||||
var json = JsonSerializer.Serialize(Current, JsonOptions);
|
||||
await browser.WriteMatchJsonAsync(json);
|
||||
_dirty = false;
|
||||
_lastPersistedAt = MatchEngine.NowUnixMs();
|
||||
}
|
||||
|
||||
public async Task FlushIfDueAsync(long minimumIntervalMs = 1_000)
|
||||
{
|
||||
if (!_dirty)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (MatchEngine.NowUnixMs() - _lastPersistedAt < minimumIntervalMs)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await SaveAsync();
|
||||
}
|
||||
|
||||
public async Task ClearAsync()
|
||||
{
|
||||
Current = null;
|
||||
IsLoaded = true;
|
||||
_dirty = false;
|
||||
_lastPersistedAt = MatchEngine.NowUnixMs();
|
||||
await browser.ClearMatchAsync();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user