-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from canhorn/dev
dev merge : Add support to CachedEntity finalization +semver: minor
- Loading branch information
Showing
10 changed files
with
213 additions
and
25 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 |
---|---|---|
|
@@ -6,4 +6,5 @@ appsettings.*.json | |
.vs | ||
*.csproj.user | ||
published | ||
nuget-packages | ||
nuget-packages | ||
.idea |
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
...lazor.Interop.Sample/Pages/Testing/InteropTesting/Scenarios/InteropFinalizationTest.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 @@ | ||
<div> | ||
<h3>Finalization Validation</h3> | ||
<div class="--lighter"></div> | ||
<div> | ||
Status: | ||
@if (TestStatus == "Passed") | ||
{ | ||
<span class="green-badge">@TestStatus</span> | ||
} | ||
else if (TestStatus == "Failed") | ||
{ | ||
<span class="red-badge">@TestStatus</span> | ||
} | ||
else | ||
{ | ||
<span>@TestStatus</span> | ||
} | ||
</div> | ||
<button class="run-btn" @onclick="HandleRunTest">Run</button> | ||
</div> | ||
|
||
|
||
@code { | ||
public string TestStatus = "Pending"; | ||
|
||
private int result; | ||
|
||
private async Task HandleRunTest() | ||
{ | ||
result = 0; | ||
|
||
await RunTest(); | ||
ValidateTest(); | ||
} | ||
|
||
public async Task RunTest() | ||
{ | ||
var list = CreateEntities(); | ||
|
||
GC.Collect(); | ||
|
||
// GC won't collect until we release the thread, so we offload | ||
// the rest of it to be completed async. | ||
await Task.Delay(10); | ||
|
||
foreach (var guid in list) | ||
{ | ||
var value = EventHorizonBlazorInterop.Get<string>(guid, "X"); | ||
if (value is not null) | ||
result++; | ||
} | ||
} | ||
|
||
static List<string> CreateEntities() | ||
{ | ||
var list = new List<string>(); | ||
|
||
for (var i = 0; i < 100; i++) | ||
{ | ||
var entity = EventHorizonBlazorInterop.FuncClass( | ||
e => new Vector3CachedEntity(e), | ||
new object[] | ||
{ | ||
new[] { "funcClass", "func" }, | ||
} | ||
); | ||
|
||
list.Add(entity.___guid); | ||
} | ||
|
||
return list; | ||
} | ||
|
||
public void ValidateTest() | ||
{ | ||
if (result == 0) | ||
{ | ||
TestStatus = "Passed"; | ||
} | ||
else | ||
{ | ||
TestStatus = "Failed"; | ||
} | ||
} | ||
|
||
} |
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 |
---|---|---|
|
@@ -4,4 +4,5 @@ | |
|
||
<div class="testing-content"> | ||
<InteropNewArgumentAsNullTest /> | ||
</div> | ||
<InteropFinalizationTest/> | ||
</div> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace EventHorizon.Blazor.Interop | ||
{ | ||
/// <summary> | ||
/// Represents a root reference to JS object. The JS object will live as long as this reference exists, | ||
/// and once it is collected, the JS object will also be released for collection by the JS runtime. | ||
/// </summary> | ||
[JsonConverter(typeof(CachedEntityRefConverter))] | ||
public sealed class CachedEntityRef : IEquatable<CachedEntityRef> | ||
{ | ||
readonly string _guid; | ||
|
||
internal CachedEntityRef(string guid) | ||
{ | ||
_guid = guid; | ||
} | ||
|
||
/// <inheritdoc /> | ||
~CachedEntityRef() | ||
{ | ||
EventHorizonBlazorInterop.RemoveEntity(_guid); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override string ToString() | ||
{ | ||
return _guid; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override int GetHashCode() | ||
{ | ||
return (_guid != null ? _guid.GetHashCode() : 0); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public bool Equals(CachedEntityRef other) | ||
{ | ||
return _guid == other?._guid; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override bool Equals(object obj) | ||
{ | ||
if (ReferenceEquals(null, obj)) return false; | ||
if (ReferenceEquals(this, obj)) return true; | ||
return obj.GetType() == GetType() && Equals((CachedEntityRef) obj); | ||
} | ||
|
||
/// <summary> | ||
/// Converts an instance of this object to string. | ||
/// </summary> | ||
/// <param name="obj">A cached entity reference.</param> | ||
/// <returns>The internal UUID of the cached entity.</returns> | ||
public static implicit operator string(CachedEntityRef obj) => obj.ToString(); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// This helps with the payload of passing a CacheEntity between the C# and client. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
internal class CachedEntityRefConverter : JsonConverter<CachedEntityRef> | ||
{ | ||
/// <inheritdoc /> | ||
public override bool CanConvert(Type typeToConvert) => | ||
typeof(CachedEntityRef).IsAssignableFrom(typeToConvert); | ||
|
||
/// <inheritdoc /> | ||
public override CachedEntityRef Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
return reader.Read() ? new CachedEntityRef(reader.GetString()) : null; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override void Write(Utf8JsonWriter writer, CachedEntityRef value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStringValue(value.ToString()); | ||
} | ||
} | ||
} |
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