This repository has been archived by the owner on Nov 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use JsInterop instead of PersistentComponentState
- Loading branch information
1 parent
5916dad
commit 2d9e797
Showing
11 changed files
with
185 additions
and
36 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[*.cs] | ||
|
||
|
||
# CS1591: Missing XML comment for publicly visible type or member | ||
dotnet_diagnostic.CS1591.severity = none |
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
7 changes: 7 additions & 0 deletions
7
sample/BitzArt.Blazor.MVVM.SampleApp/BitzArt.Blazor.MVVM.SampleApp.Client/wwwroot/app.js
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,7 @@ | ||
function getInnerText(id) { | ||
let element = document.getElementById(id); | ||
if (element) { | ||
return element.innerText; | ||
} | ||
return null; | ||
} |
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
42 changes: 42 additions & 0 deletions
42
src/BitzArt.Blazor.MVVM/Extensions/AddRenderingEnvironmentExtension.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,42 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.JSInterop; | ||
|
||
namespace BitzArt.Blazor.MVVM; | ||
|
||
public static class AddRenderingEnvironmentExtension | ||
{ | ||
public static IServiceCollection AddRenderingEnvironment(this IServiceCollection services) | ||
{ | ||
services.AddTransient(x => x.GetRenderingEnvironment()); | ||
|
||
return services; | ||
} | ||
|
||
private static RenderingEnvironment GetRenderingEnvironment(this IServiceProvider serviceProvider) | ||
{ | ||
var isBrowser = OperatingSystem.IsBrowser(); | ||
var isServer = !isBrowser; | ||
|
||
var isPrerender = isServer && serviceProvider.GetIsPrerender(); | ||
|
||
return new() | ||
{ | ||
IsServer = !isBrowser, | ||
IsClient = isBrowser, | ||
IsPrerender = isPrerender | ||
}; | ||
} | ||
|
||
private static bool GetIsPrerender(this IServiceProvider serviceProvider) | ||
{ | ||
var jsRuntime = serviceProvider.GetRequiredService<IJSRuntime>(); | ||
|
||
var JSRuntimeType = jsRuntime.GetType(); | ||
if (JSRuntimeType.Name != "RemoteJSRuntime") return false; | ||
|
||
var IsInitializedProperty = jsRuntime.GetType().GetProperty("IsInitialized"); | ||
var isInitialized = IsInitializedProperty?.GetValue(jsRuntime); | ||
|
||
return isInitialized is not true; | ||
} | ||
} |
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,59 @@ | ||
using Microsoft.AspNetCore.Components.Rendering; | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.JSInterop; | ||
using System.Text.Json; | ||
using System.Text; | ||
|
||
namespace BitzArt.Blazor.MVVM; | ||
|
||
public abstract class PersistentComponentBase<TState> : ComponentBase | ||
where TState : new() | ||
{ | ||
[Inject] | ||
private IJSRuntime Js { get; set; } = default!; | ||
protected TState State { get; set; } = new(); | ||
private const string StateKey = "state"; | ||
|
||
protected override void BuildRenderTree(RenderTreeBuilder builder) => | ||
builder.AddMarkupContent(1, Serialize()); | ||
|
||
protected virtual Task InitializeStateAsync() => Task.CompletedTask; | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
if (!OperatingSystem.IsBrowser()) | ||
{ | ||
await InitializeStateAsync(); | ||
return; | ||
} | ||
|
||
var stateJson = await Js.InvokeAsync<string?>($"document.getElementById({StateKey}).innerText"); | ||
|
||
if (string.IsNullOrWhiteSpace(stateJson)) | ||
{ | ||
await InitializeStateAsync(); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
var buffer = Convert.FromBase64String(stateJson); | ||
var json = Encoding.UTF8.GetString(buffer); | ||
State = JsonSerializer.Deserialize<TState>(json)!; | ||
} | ||
catch | ||
{ | ||
await InitializeStateAsync(); | ||
} | ||
} | ||
|
||
private string Serialize() | ||
{ | ||
if (State is null || OperatingSystem.IsBrowser()) | ||
return ""; | ||
|
||
var json = JsonSerializer.SerializeToUtf8Bytes(State); | ||
var base64 = Convert.ToBase64String(json); | ||
return $"<script id=\"{StateKey}\" type=\"text/template\">{base64}</script>"; | ||
} | ||
} |
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,8 @@ | ||
namespace BitzArt.Blazor.MVVM; | ||
|
||
public class RenderingEnvironment | ||
{ | ||
public required bool IsPrerender { get; init; } | ||
public required bool IsServer { get; init; } | ||
public required bool IsClient { get; init; } | ||
} |