Skip to content

Commit 07f2ee6

Browse files
committed
Replace use of InvokeNew with InvokeConstructor in Blazor JS interop
1 parent 8883b98 commit 07f2ee6

File tree

22 files changed

+124
-124
lines changed

22 files changed

+124
-124
lines changed

src/Components/Endpoints/src/DependencyInjection/UnsupportedJavaScriptRuntime.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ internal sealed class UnsupportedJavaScriptRuntime : IJSRuntime
1717
ValueTask<TValue> IJSRuntime.InvokeAsync<[DynamicallyAccessedMembers(JsonSerialized)] TValue>(string identifier, object?[]? args)
1818
=> throw new InvalidOperationException(Message);
1919

20-
public ValueTask<IJSObjectReference> InvokeNewAsync(string identifier, object?[]? args)
20+
public ValueTask<IJSObjectReference> InvokeConstructorAsync(string identifier, object?[]? args)
2121
=> throw new InvalidOperationException(Message);
2222

23-
public ValueTask<IJSObjectReference> InvokeNewAsync(string identifier, CancellationToken cancellationToken, object?[]? args)
23+
public ValueTask<IJSObjectReference> InvokeConstructorAsync(string identifier, CancellationToken cancellationToken, object?[]? args)
2424
=> throw new InvalidOperationException(Message);
2525

2626
public ValueTask<TValue> GetValueAsync<[DynamicallyAccessedMembers(JsonSerialized)] TValue>(string identifier)

src/Components/Server/test/ProtectedBrowserStorageTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,13 @@ public ValueTask<TValue> InvokeAsync<TValue>(string identifier, CancellationToke
365365
public ValueTask<TValue> InvokeAsync<TValue>(string identifier, object[] args)
366366
=> InvokeAsync<TValue>(identifier, cancellationToken: CancellationToken.None, args: args);
367367

368-
public ValueTask<IJSObjectReference> InvokeNewAsync(string identifier, object[] args)
368+
public ValueTask<IJSObjectReference> InvokeConstructorAsync(string identifier, object[] args)
369369
{
370370
Invocations.Add((identifier, args, JSCallType.NewCall));
371371
return (ValueTask<IJSObjectReference>)NextInvocationResult;
372372
}
373373

374-
public ValueTask<IJSObjectReference> InvokeNewAsync(string identifier, CancellationToken cancellationToken, object[] args)
374+
public ValueTask<IJSObjectReference> InvokeConstructorAsync(string identifier, CancellationToken cancellationToken, object[] args)
375375
{
376376
Invocations.Add((identifier, args, JSCallType.NewCall));
377377
return (ValueTask<IJSObjectReference>)NextInvocationResult;

src/Components/WebAssembly/WebAssembly.Authentication/test/RemoteAuthenticationServiceTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,10 +549,10 @@ public ValueTask<TValue> GetValueAsync<TValue>(string identifier)
549549
public ValueTask<TValue> GetValueAsync<TValue>(string identifier, CancellationToken cancellationToken)
550550
=> throw new NotImplementedException();
551551

552-
public ValueTask<IJSObjectReference> InvokeNewAsync(string identifier, object[] args)
552+
public ValueTask<IJSObjectReference> InvokeConstructorAsync(string identifier, object[] args)
553553
=> throw new NotImplementedException();
554554

555-
public ValueTask<IJSObjectReference> InvokeNewAsync(string identifier, CancellationToken cancellationToken, object[] args)
555+
public ValueTask<IJSObjectReference> InvokeConstructorAsync(string identifier, CancellationToken cancellationToken, object[] args)
556556
=> throw new NotImplementedException();
557557

558558
public ValueTask SetValueAsync<TValue>(string identifier, TValue value)

src/Components/WebAssembly/WebAssembly.Authentication/test/RemoteAuthenticatorCoreTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,10 +790,10 @@ public ValueTask<TValue> InvokeAsync<TValue>(string identifier, CancellationToke
790790
return default;
791791
}
792792

793-
public ValueTask<IJSObjectReference> InvokeNewAsync(string identifier, object[] args)
793+
public ValueTask<IJSObjectReference> InvokeConstructorAsync(string identifier, object[] args)
794794
=> throw new NotImplementedException();
795795

796-
public ValueTask<IJSObjectReference> InvokeNewAsync(string identifier, CancellationToken cancellationToken, object[] args)
796+
public ValueTask<IJSObjectReference> InvokeConstructorAsync(string identifier, CancellationToken cancellationToken, object[] args)
797797
=> throw new NotImplementedException();
798798

799799
public ValueTask<TValue> GetValueAsync<TValue>(string identifier)

src/Components/WebAssembly/WebAssembly/test/PullFromJSDataStreamTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ public virtual ValueTask<TValue> InvokeAsync<TValue>(string identifier, Cancella
133133
public async ValueTask<TValue> InvokeAsync<TValue>(string identifier, object[] args)
134134
=> await InvokeAsync<TValue>(identifier, CancellationToken.None, args);
135135

136-
public ValueTask<IJSObjectReference> InvokeNewAsync(string identifier, object[] args)
136+
public ValueTask<IJSObjectReference> InvokeConstructorAsync(string identifier, object[] args)
137137
=> throw new NotImplementedException();
138138

139-
public ValueTask<IJSObjectReference> InvokeNewAsync(string identifier, CancellationToken cancellationToken, object[] args)
139+
public ValueTask<IJSObjectReference> InvokeConstructorAsync(string identifier, CancellationToken cancellationToken, object[] args)
140140
=> throw new NotImplementedException();
141141

142142
public ValueTask<TValue> GetValueAsync<TValue>(string identifier)

src/Components/test/E2ETest/Tests/InteropTest.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ public void CanInvokeInteropMethods()
102102
["setValueToSetterAsync"] = "40",
103103
["setValueToUndefinedPropertyAsync"] = "50",
104104
["setValueToGetterAsync"] = "Success",
105-
// InvokeNew tests
106-
["invokeNewWithClassConstructorAsync"] = "Success",
107-
["invokeNewWithClassConstructorAsync.dataProperty"] = "abraka",
108-
["invokeNewWithClassConstructorAsync.function"] = "6",
109-
["invokeNewWithNonConstructorAsync"] = "Success",
105+
// InvokeConstructor tests
106+
["InvokeConstructorWithClassConstructorAsync"] = "Success",
107+
["InvokeConstructorWithClassConstructorAsync.dataProperty"] = "abraka",
108+
["InvokeConstructorWithClassConstructorAsync.function"] = "6",
109+
["InvokeConstructorWithNonConstructorAsync"] = "Success",
110110
// Function reference tests
111111
["changeFunctionViaObjectReferenceAsync"] = "42"
112112
};
@@ -168,11 +168,11 @@ public void CanInvokeInteropMethods()
168168
["setValueToSetter"] = "40",
169169
["setValueToUndefinedProperty"] = "50",
170170
["setValueToGetter"] = "Success",
171-
// InvokeNew tests
172-
["invokeNewWithClassConstructor"] = "Success",
173-
["invokeNewWithClassConstructor.dataProperty"] = "abraka",
174-
["invokeNewWithClassConstructor.function"] = "6",
175-
["invokeNewWithNonConstructor"] = "Success",
171+
// InvokeConstructor tests
172+
["InvokeConstructorWithClassConstructor"] = "Success",
173+
["InvokeConstructorWithClassConstructor.dataProperty"] = "abraka",
174+
["InvokeConstructorWithClassConstructor.function"] = "6",
175+
["InvokeConstructorWithNonConstructor"] = "Success",
176176
// Function reference tests
177177
["changeFunctionViaObjectReference"] = "42"
178178
};

src/Components/test/testassets/BasicTestApp/DotNetToJSInterop.razor

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@
9090
</div>
9191

9292
<div style="margin-top: 2em">
93-
<button @onclick="CreateInstanceByConstructorFunction">Call constructor function InvokeNewAsync</button>
94-
<button @onclick="CreateInstanceByClassConstructor">Call class constructor with InvokeNewAsync</button>
93+
<button @onclick="CreateInstanceByConstructorFunction">Call constructor function InvokeConstructorAsync</button>
94+
<button @onclick="CreateInstanceByClassConstructor">Call class constructor with InvokeConstructorAsync</button>
9595
<button @onclick="ChangeInstanceMethodWithFunctionReference">Change instance method with function reference</button>
9696
<span>@InstanceMessage</span>
9797
</div>
@@ -147,13 +147,13 @@
147147

148148
private async Task CreateInstanceByConstructorFunction()
149149
{
150-
var dogRef = await JSRuntime.InvokeNewAsync("Dog", "A dog");
150+
var dogRef = await JSRuntime.InvokeConstructorAsync("Dog", "A dog");
151151
InstanceMessage = await dogRef.InvokeAsync<string>("bark");
152152
}
153153

154154
private async Task CreateInstanceByClassConstructor()
155155
{
156-
var catRef = await JSRuntime.InvokeNewAsync("Cat", "A cat");
156+
var catRef = await JSRuntime.InvokeConstructorAsync("Cat", "A cat");
157157
InstanceMessage = await catRef.InvokeAsync<string>("meow");
158158
}
159159

@@ -169,9 +169,9 @@
169169

170170
private async Task ChangeInstanceMethodWithFunctionReference()
171171
{
172-
var dogRef = await JSRuntime.InvokeNewAsync("Dog", "A dog");
172+
var dogRef = await JSRuntime.InvokeConstructorAsync("Dog", "A dog");
173173
var dogFuncRef = await dogRef.GetValueAsync<IJSObjectReference>("bark");
174-
var catRef = await JSRuntime.InvokeNewAsync("Cat", "A cat");
174+
var catRef = await JSRuntime.InvokeConstructorAsync("Cat", "A cat");
175175
await catRef.SetValueAsync("meow", dogFuncRef);
176176
InstanceMessage = await catRef.InvokeAsync<string>("meow");
177177
}

src/Components/test/testassets/BasicTestApp/InteropComponent.razor

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,11 @@
294294
SetValueTests();
295295
}
296296

297-
await InvokeNewAsyncTests();
297+
await InvokeConstructorAsyncTests();
298298

299299
if (shouldSupportSyncInterop)
300300
{
301-
InvokeNewTests();
301+
InvokeConstructorTests();
302302
}
303303

304304
await FunctionReferenceAsyncTests();
@@ -560,58 +560,58 @@
560560
}
561561
}
562562

563-
private async Task InvokeNewAsyncTests()
563+
private async Task InvokeConstructorAsyncTests()
564564
{
565-
var testClassRef = await JSRuntime.InvokeNewAsync("jsInteropTests.TestClass", "abraka");
565+
var testClassRef = await JSRuntime.InvokeConstructorAsync("jsInteropTests.TestClass", "abraka");
566566

567-
ReturnValues["invokeNewWithClassConstructorAsync"] = testClassRef is IJSObjectReference ? "Success" : "Failure";
568-
ReturnValues["invokeNewWithClassConstructorAsync.dataProperty"] = await testClassRef.GetValueAsync<string>("text");
569-
ReturnValues["invokeNewWithClassConstructorAsync.function"] = (await testClassRef.InvokeAsync<int>("getTextLength")).ToString();
567+
ReturnValues["InvokeConstructorWithClassConstructorAsync"] = testClassRef is IJSObjectReference ? "Success" : "Failure";
568+
ReturnValues["InvokeConstructorWithClassConstructorAsync.dataProperty"] = await testClassRef.GetValueAsync<string>("text");
569+
ReturnValues["InvokeConstructorWithClassConstructorAsync.function"] = (await testClassRef.InvokeAsync<int>("getTextLength")).ToString();
570570

571571
try
572572
{
573-
var nonConstructorRef = await JSRuntime.InvokeNewAsync("jsInteropTests.nonConstructorFunction");
574-
ReturnValues["invokeNewWithNonConstructorAsync"] = nonConstructorRef is null ? "Failure: null" : "Failure: not null";
573+
var nonConstructorRef = await JSRuntime.InvokeConstructorAsync("jsInteropTests.nonConstructorFunction");
574+
ReturnValues["InvokeConstructorWithNonConstructorAsync"] = nonConstructorRef is null ? "Failure: null" : "Failure: not null";
575575
}
576576
catch (JSException)
577577
{
578-
ReturnValues["invokeNewWithNonConstructorAsync"] = "Success";
578+
ReturnValues["InvokeConstructorWithNonConstructorAsync"] = "Success";
579579
}
580580
catch (Exception ex)
581581
{
582-
ReturnValues["invokeNewWithNonConstructorAsync"] = $"Failure: {ex.Message}";
582+
ReturnValues["InvokeConstructorWithNonConstructorAsync"] = $"Failure: {ex.Message}";
583583
}
584584
}
585585

586-
private void InvokeNewTests()
586+
private void InvokeConstructorTests()
587587
{
588588
var inProcRuntime = ((IJSInProcessRuntime)JSRuntime);
589589

590-
var testClassRef = inProcRuntime.InvokeNew("jsInteropTests.TestClass", "abraka");
590+
var testClassRef = inProcRuntime.InvokeConstructor("jsInteropTests.TestClass", "abraka");
591591

592-
ReturnValues["invokeNewWithClassConstructor"] = testClassRef is IJSInProcessObjectReference ? "Success" : "Failure";
593-
ReturnValues["invokeNewWithClassConstructor.dataProperty"] = testClassRef.GetValue<string>("text");
594-
ReturnValues["invokeNewWithClassConstructor.function"] = testClassRef.Invoke<int>("getTextLength").ToString();
592+
ReturnValues["InvokeConstructorWithClassConstructor"] = testClassRef is IJSInProcessObjectReference ? "Success" : "Failure";
593+
ReturnValues["InvokeConstructorWithClassConstructor.dataProperty"] = testClassRef.GetValue<string>("text");
594+
ReturnValues["InvokeConstructorWithClassConstructor.function"] = testClassRef.Invoke<int>("getTextLength").ToString();
595595

596596
try
597597
{
598-
var nonConstructorRef = inProcRuntime.InvokeNew("jsInteropTests.nonConstructorFunction");
599-
ReturnValues["invokeNewWithNonConstructor"] = nonConstructorRef is null ? "Failure: null" : "Failure: not null";
598+
var nonConstructorRef = inProcRuntime.InvokeConstructor("jsInteropTests.nonConstructorFunction");
599+
ReturnValues["InvokeConstructorWithNonConstructor"] = nonConstructorRef is null ? "Failure: null" : "Failure: not null";
600600
}
601601
catch (JSException)
602602
{
603-
ReturnValues["invokeNewWithNonConstructor"] = "Success";
603+
ReturnValues["InvokeConstructorWithNonConstructor"] = "Success";
604604
}
605605
catch (Exception ex)
606606
{
607-
ReturnValues["invokeNewWithNonConstructor"] = $"Failure: {ex.Message}";
607+
ReturnValues["InvokeConstructorWithNonConstructor"] = $"Failure: {ex.Message}";
608608
}
609609
}
610610

611611
private async Task FunctionReferenceAsyncTests()
612612
{
613613
var funcRef = await JSRuntime.GetValueAsync<IJSObjectReference>("jsInteropTests.nonConstructorFunction");
614-
var testClassRef = await JSRuntime.InvokeNewAsync("jsInteropTests.TestClass", "abraka");
614+
var testClassRef = await JSRuntime.InvokeConstructorAsync("jsInteropTests.TestClass", "abraka");
615615
await testClassRef.SetValueAsync("getTextLength", funcRef);
616616
ReturnValues["changeFunctionViaObjectReferenceAsync"] = (await testClassRef.InvokeAsync<int>("getTextLength")).ToString();
617617
}
@@ -621,7 +621,7 @@
621621
var inProcRuntime = ((IJSInProcessRuntime)JSRuntime);
622622

623623
var funcRef = inProcRuntime.GetValue<IJSObjectReference>("jsInteropTests.nonConstructorFunction");
624-
var testClassRef = inProcRuntime.InvokeNew("jsInteropTests.TestClass", "abraka");
624+
var testClassRef = inProcRuntime.InvokeConstructor("jsInteropTests.TestClass", "abraka");
625625
testClassRef.SetValue("getTextLength", funcRef);
626626
ReturnValues["changeFunctionViaObjectReference"] = testClassRef.Invoke<int>("getTextLength").ToString();
627627
}

src/JSInterop/Microsoft.JSInterop.JS/src/src/Microsoft.JSInterop.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export module DotNet {
6666
*/
6767
export enum JSCallType {
6868
FunctionCall = 1,
69-
NewCall = 2,
69+
ConstructorCall = 2,
7070
GetValue = 3,
7171
SetValue = 4
7272
}
@@ -573,7 +573,7 @@ export module DotNet {
573573
}
574574

575575
/** Traverses the object hierarchy to find an object member specified by the identifier.
576-
*
576+
*
577577
* @param obj Root object to search in.
578578
* @param identifier Complete identifier of the member to find, e.g. "document.location.href".
579579
* @returns A tuple containing the immediate parent of the member and the member name.
@@ -586,19 +586,19 @@ export module DotNet {
586586
// Error handling in case of undefined last key depends on the type of operation.
587587
for (let i = 0; i < keys.length - 1; i++) {
588588
const key = keys[i];
589-
589+
590590
if (current && typeof current === 'object' && key in current) {
591591
current = current[key];
592592
} else {
593593
throw new Error(`Could not find '${identifier}' ('${key}' was undefined).`);
594594
}
595595
}
596-
596+
597597
return [current, keys[keys.length - 1]];
598598
}
599599

600600
/** Takes an object member and a call type and returns a function that performs the operation specified by the call type on the member.
601-
*
601+
*
602602
* @param parent Immediate parent of the accessed object member.
603603
* @param memberName Name (key) of the accessed member.
604604
* @param callType The type of the operation to perform on the member.
@@ -614,7 +614,7 @@ export module DotNet {
614614
} else {
615615
throw new Error(`The value '${identifier}' is not a function.`);
616616
}
617-
case JSCallType.NewCall:
617+
case JSCallType.ConstructorCall:
618618
const ctor = parent[memberName];
619619
if (ctor instanceof Function) {
620620
const bound = ctor.bind(parent);
@@ -640,50 +640,50 @@ export module DotNet {
640640
if (!(propName in obj)) {
641641
return false;
642642
}
643-
643+
644644
// If the property is present we examine its descriptor, potentially needing to walk up the prototype chain.
645645
while (obj !== undefined) {
646646
const descriptor = Object.getOwnPropertyDescriptor(obj, propName);
647-
647+
648648
if (descriptor) {
649649
// Return true for data property
650650
if (descriptor.hasOwnProperty('value')) {
651651
return true
652652
}
653-
653+
654654
// Return true for accessor property with defined getter.
655655
return descriptor.hasOwnProperty('get') && typeof descriptor.get === 'function';
656656
}
657-
657+
658658
obj = Object.getPrototypeOf(obj);
659659
}
660-
660+
661661
return false;
662662
}
663-
663+
664664
function isWritableProperty(obj: any, propName: string) {
665665
// Return true for missing property if the property can be added.
666666
if (!(propName in obj)) {
667667
return Object.isExtensible(obj);
668668
}
669-
669+
670670
// If the property is present we examine its descriptor, potentially needing to walk up the prototype chain.
671671
while (obj !== undefined) {
672672
const descriptor = Object.getOwnPropertyDescriptor(obj, propName);
673-
673+
674674
if (descriptor) {
675675
// Return true for writable data property.
676676
if (descriptor.hasOwnProperty('value') && descriptor.writable) {
677677
return true;
678678
}
679-
679+
680680
// Return true for accessor property with defined setter.
681681
return descriptor.hasOwnProperty('set') && typeof descriptor.set === 'function';
682682
}
683-
683+
684684
obj = Object.getPrototypeOf(obj);
685685
}
686-
686+
687687
return false;
688688
}
689689

0 commit comments

Comments
 (0)