Skip to content

Commit

Permalink
Update JSInProcessObjectReference.cs (#48287)
Browse files Browse the repository at this point in the history
* Update JSInProcessObjectReference.cs

Fix for bug #48280

* Add JSObjectReferences dispose tests

---------

Co-authored-by: Mackinnon Buck <mackinnon.buck@gmail.com>
  • Loading branch information
LostBeard and MackinnonBuck authored May 24, 2023
1 parent 91d702c commit 90599c6
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Components/Web.JS/dist/Release/blazor.server.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Components/Web.JS/dist/Release/blazor.web.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Components/Web.JS/dist/Release/blazor.webview.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/Components/test/E2ETest/Tests/InteropTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public void CanInvokeDotNetMethods()
["invokeAsyncThrowsSerializingCircularStructure"] = "Success",
["invokeAsyncThrowsUndefinedJSObjectReference"] = "Success",
["invokeAsyncThrowsNullJSObjectReference"] = "Success",
["disposeJSObjectReferenceAsync"] = "Success",
};

var expectedSyncValues = new Dictionary<string, string>
Expand Down Expand Up @@ -143,6 +144,7 @@ public void CanInvokeDotNetMethods()
["genericInstanceMethod"] = @"""Updated value 2""",
["requestDotNetStreamReference"] = @"""Success""",
["requestDotNetStreamWrapperReference"] = @"""Success""",
["disposeJSInProcessObjectReference"] = "Success",
};

// Include the sync assertions only when running under WebAssembly
Expand Down
19 changes: 19 additions & 0 deletions src/Components/test/testassets/BasicTestApp/InteropComponent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,16 @@
JSObjectReferenceInvokeNonFunctionException = e;
}

try
{
await jsObjectReference.DisposeAsync();
ReturnValues["disposeJSObjectReferenceAsync"] = "Success";
}
catch (Exception ex)
{
ReturnValues["disposeJSObjectReferenceAsync"] = $"Failure: {ex.Message}";
}

var module = await JSRuntime.InvokeAsync<IJSObjectReference>("import", "./js/testmodule.js");
ReturnValues["jsObjectReferenceModule"] = await module.InvokeAsync<string>("identity", "Returned from module!");

Expand Down Expand Up @@ -408,6 +418,15 @@
NumberField = 41,
}).ToString();

try
{
jsInProcObjectReference.Dispose();
ReturnValues["disposeJSInProcessObjectReference"] = "Success";
}
catch (Exception ex)
{
ReturnValues["disposeJSInProcessObjectReference"] = $"Failure: {ex.Message}";
}

var unmarshalledRuntime = (IJSUnmarshalledRuntime)JSRuntime;
var jsUnmarshalledReference = unmarshalledRuntime.InvokeUnmarshalled<IJSUnmarshalledObjectReference>("returnJSObjectReference");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ export module DotNet {
throw new Error(`JS object instance with ID ${targetInstanceId} does not exist (has it been disposed?).`);
}

function disposeJSObjectReferenceById(id: number) {
export function disposeJSObjectReferenceById(id: number) {
delete cachedJSObjectsById[id];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public void Dispose()
}
}

[JSImport("DotNet.jsCallDispatcher.disposeJSObjectReferenceById", "blazor-internal")]
[JSImport("globalThis.DotNet.disposeJSObjectReferenceById")]
private static partial void DisposeJSObjectReferenceById([JSMarshalAs<JSType.Number>] long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class JSObjectReference : IJSObjectReference
protected internal long Id { get; }

/// <summary>
/// Inititializes a new <see cref="JSObjectReference"/> instance.
/// Initializes a new <see cref="JSObjectReference"/> instance.
/// </summary>
/// <param name="jsRuntime">The <see cref="JSRuntime"/> used for invoking JS interop calls.</param>
/// <param name="id">The unique identifier.</param>
Expand Down Expand Up @@ -55,7 +55,7 @@ public async ValueTask DisposeAsync()
{
Disposed = true;

await _jsRuntime.InvokeVoidAsync("DotNet.jsCallDispatcher.disposeJSObjectReferenceById", Id);
await _jsRuntime.InvokeVoidAsync("DotNet.disposeJSObjectReferenceById", Id);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public sealed class JSStreamReference : JSObjectReference, IJSStreamReference
public long Length { get; }

/// <summary>
/// Inititializes a new <see cref="JSStreamReference"/> instance.
/// Initializes a new <see cref="JSStreamReference"/> instance.
/// </summary>
/// <param name="jsRuntime">The <see cref="JSRuntime"/> used for invoking JS interop calls.</param>
/// <param name="id">The unique identifier.</param>
Expand Down

0 comments on commit 90599c6

Please sign in to comment.