Skip to content

Commit 898cfc0

Browse files
committed
Feedback
1 parent c2aba59 commit 898cfc0

File tree

2 files changed

+19
-41
lines changed

2 files changed

+19
-41
lines changed

src/Components/Web.JS/src/Rendering/ElementReferenceCapture.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@ function getCaptureIdAttributeName(referenceCaptureId: string) {
1616
return `_bl_${referenceCaptureId}`;
1717
}
1818

19-
function getCaptureIdFromElement(element: Element): string | null {
20-
for (let i = 0; i < element.attributes.length; i++) {
21-
const attr = element.attributes[i];
22-
if (attr.name.startsWith('_bl_')) {
23-
return attr.name.substring(4);
24-
}
25-
}
26-
return null;
27-
}
28-
2919
// Support receiving ElementRef instances as args in interop calls
3020
const elementRefKey = '__internalId'; // Keep in sync with ElementRef.cs
3121
DotNet.attachReviver((key, value) => {
@@ -34,15 +24,4 @@ DotNet.attachReviver((key, value) => {
3424
} else {
3525
return value;
3626
}
37-
});
38-
39-
// Support return of the ElementRef from JS to .NET
40-
DotNet.attachReplacer((key, value) => {
41-
if (value instanceof Element) {
42-
const captureId = getCaptureIdFromElement(value);
43-
if (captureId) {
44-
return { [elementRefKey]: captureId };
45-
}
46-
}
47-
return value;
4827
});

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

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
export module DotNet {
77
export type JsonReviver = ((key: any, value: any) => any);
88
const jsonRevivers: JsonReviver[] = [];
9-
export type JsonReplacer = ((key: any, value: any) => any);
10-
const jsonReplacers: JsonReplacer[] = [];
119

1210
const jsObjectIdKey = "__jsObjectId";
1311
const dotNetObjectRefKey = "__dotNetObject";
12+
const dotNetElementRefKey = "__internalId";
1413
const byteArrayRefKey = "__byte[]";
1514
const dotNetStreamRefKey = "__dotNetStream";
1615
const jsStreamReferenceLengthKey = "__jsStreamReferenceLength";
@@ -120,14 +119,6 @@ export module DotNet {
120119
jsonRevivers.push(reviver);
121120
}
122121

123-
/**
124-
* Adds a JSON replacer callback that will be used when serializing arguments sent to .NET.
125-
* @param replacer The replacer to add.
126-
*/
127-
export function attachReplacer(replacer: JsonReplacer) {
128-
jsonReplacers.push(replacer);
129-
}
130-
131122
/**
132123
* Invokes the specified .NET public method synchronously. Not all hosting scenarios support
133124
* synchronous invocation, so if possible use invokeMethodAsync instead.
@@ -817,22 +808,30 @@ export module DotNet {
817808
return result;
818809
}
819810

811+
function getCaptureIdFromElement(element: Element): string | null {
812+
for (let i = 0; i < element.attributes.length; i++) {
813+
const attr = element.attributes[i];
814+
if (attr.name.startsWith('_bl_')) {
815+
return attr.name.substring(4);
816+
}
817+
}
818+
return null;
819+
}
820+
820821
function argReplacer(key: string, value: any) {
821-
const processedValue = jsonReplacers.reduce(
822-
(currentValue, replacer) => replacer(key, currentValue),
823-
value
824-
);
825-
826-
if (processedValue instanceof DotNetObject) {
827-
return processedValue.serializeAsArg();
828-
} else if (processedValue instanceof Uint8Array) {
822+
if (value instanceof Element) {
823+
return { [dotNetElementRefKey]: getCaptureIdFromElement(value) };
824+
}
825+
if (value instanceof DotNetObject) {
826+
return value.serializeAsArg();
827+
} else if (value instanceof Uint8Array) {
829828
const dotNetCallDispatcher = currentCallDispatcher!.getDotNetCallDispatcher();
830-
dotNetCallDispatcher!.sendByteArray(nextByteArrayIndex, processedValue);
829+
dotNetCallDispatcher!.sendByteArray(nextByteArrayIndex, value);
831830
const jsonValue = { [byteArrayRefKey]: nextByteArrayIndex };
832831
nextByteArrayIndex++;
833832
return jsonValue;
834833
}
835834

836-
return processedValue;
835+
return value;
837836
}
838837
}

0 commit comments

Comments
 (0)