-
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.
- Loading branch information
1 parent
da2fab4
commit d161d57
Showing
8 changed files
with
131 additions
and
150 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
86 changes: 86 additions & 0 deletions
86
samples/KristofferStrube.Blazor.WebIDL.WasmExample/Pages/MapLike.razor
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,86 @@ | ||
@page "/MapLike" | ||
|
||
@inject IJSRuntime JSRuntime | ||
|
||
<PageTitle>WebIDL - MapLike</PageTitle> | ||
|
||
<h1>MapLike</h1> | ||
|
||
<p>This page shows a very simple way to implement a wrapper for a JS <code>Map</code> simply by implementing the <code>IReadonlyMapLike</code> interface.</p> | ||
|
||
|
||
<h3> | ||
The map currently has size: <code>@size</code> | ||
</h3> | ||
@if (size > 0) | ||
{ | ||
<h3> | ||
The values are: <code>[@string.Join(", ", values)]</code> | ||
</h3> | ||
} | ||
|
||
|
||
@code { | ||
Map map = default!; | ||
ulong size; | ||
List<string> values = new(); | ||
string inputText = ""; | ||
double inputNumber = 0; | ||
bool inputBoolean = false; | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
map = await Map.CreateAsync<object>(JSRuntime); | ||
|
||
await UpdateMapInformationAsync(); | ||
} | ||
|
||
async Task UpdateMapInformationAsync() | ||
{ | ||
size = await map.GetSizeAsync(); | ||
values.Clear(); | ||
} | ||
|
||
[IJSWrapperConverter] | ||
public class Map : IReadonlyMapLike<Map, string, string>, IJSCreatable<Map> | ||
{ | ||
/// <inheritdoc/> | ||
public IJSObjectReference JSReference { get; } | ||
/// <inheritdoc/> | ||
public IJSRuntime JSRuntime { get; } | ||
/// <inheritdoc/> | ||
public bool DisposesJSReference { get; } | ||
|
||
public static async Task<Map> CreateAsync<T>(IJSRuntime jSRuntime) | ||
{ | ||
var jSInstance = await jSRuntime.InvokeAsync<IJSObjectReference>("constructMap"); | ||
return new Map(jSRuntime, jSInstance, new() { DisposesJSReference = true }); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public static Task<Map> CreateAsync(IJSRuntime jSRuntime, IJSObjectReference jSReference) | ||
{ | ||
return Task.FromResult(new Map(jSRuntime, jSReference, new())); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public static Task<Map> CreateAsync(IJSRuntime jSRuntime, IJSObjectReference jSReference, CreationOptions options) | ||
{ | ||
return Task.FromResult(new Map(jSRuntime, jSReference, options)); | ||
} | ||
|
||
public Map(IJSRuntime jSRuntime, IJSObjectReference jSReference, CreationOptions options) | ||
{ | ||
JSRuntime = jSRuntime; | ||
JSReference = jSReference; | ||
DisposesJSReference = options.DisposesJSReference; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async ValueTask DisposeAsync() | ||
{ | ||
await IJSWrapper.DisposeJSReference(this); | ||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
} |
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
18 changes: 18 additions & 0 deletions
18
src/KristofferStrube.Blazor.WebIDL/Declerations/IReadonlyMapLike.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,18 @@ | ||
using Microsoft.JSInterop; | ||
|
||
namespace KristofferStrube.Blazor.WebIDL; | ||
|
||
/// <summary> | ||
/// Objects implementing an interface that is declared to be maplike represent an ordered map of key–value pairs, initially empty, known as that object’s map entries. | ||
/// </summary> | ||
/// <remarks><see href="https://webidl.spec.whatwg.org/#idl-maplike">See the API definition here</see>.</remarks> | ||
public interface IReadonlyMapLike<TMap, TKey, TValue> : IJSWrapper where TMap : IReadonlyMapLike<TMap, TKey, TValue> { } | ||
|
||
public static class IReadonlyMapLikeExtensions | ||
{ | ||
public static async Task<ulong> GetSizeAsync<TMap, TKey, TValue>(this IReadonlyMapLike<TMap, TKey, TValue> map) where TMap : IReadonlyMapLike<TMap, TKey, TValue> | ||
Check warning on line 13 in src/KristofferStrube.Blazor.WebIDL/Declerations/IReadonlyMapLike.cs
|
||
{ | ||
IJSObjectReference helper = await map.JSRuntime.GetHelperAsync(); | ||
return await helper.InvokeAsync<ulong>("getAttribute", map.JSReference, "size"); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.