-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Added replacers to the JSInterop #63542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
e47b371
bcd1e91
c2aba59
898cfc0
32847a5
df1660f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -16,12 +16,34 @@ function getCaptureIdAttributeName(referenceCaptureId: string) { | |||||||||||
return `_bl_${referenceCaptureId}`; | ||||||||||||
} | ||||||||||||
|
||||||||||||
function getCaptureIdFromElement(element: Element): string | null { | ||||||||||||
for (let i = 0; i < element.attributes.length; i++) { | ||||||||||||
const attr = element.attributes[i]; | ||||||||||||
if (attr.name.startsWith('_bl_')) { | ||||||||||||
return attr.name.substring(4); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
return null; | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Support receiving ElementRef instances as args in interop calls | ||||||||||||
const elementRefKey = '__internalId'; // Keep in sync with ElementRef.cs | ||||||||||||
DotNet.attachReviver((key, value) => { | ||||||||||||
if (value && typeof value === 'object' && Object.prototype.hasOwnProperty.call(value, elementRefKey) && typeof value[elementRefKey] === 'string') { | ||||||||||||
console.log("attachReviver: ", value); | ||||||||||||
return getElementByCaptureId(value[elementRefKey]); | ||||||||||||
} else { | ||||||||||||
return value; | ||||||||||||
} | ||||||||||||
}); | ||||||||||||
|
||||||||||||
// Support return of the ElementRef from JS to .NET | ||||||||||||
DotNet.attachReplacer((key, value) => { | ||||||||||||
if (value instanceof Element) { | ||||||||||||
const captureId = getCaptureIdFromElement(value); | ||||||||||||
if (captureId) { | ||||||||||||
return { [elementRefKey]: captureId }; | ||||||||||||
} | ||||||||||||
} | ||||||||||||
return value; | ||||||||||||
}); | ||||||||||||
|
const jsObjectIdKey = "__jsObjectId"; | |
const dotNetObjectRefKey = "__dotNetObject"; | |
const byteArrayRefKey = "__byte[]"; | |
const dotNetStreamRefKey = "__dotNetStream"; | |
const jsStreamReferenceLengthKey = "__jsStreamReferenceLength"; |
And then on the C# side (if necessary) have a custom converter for ElementReference
, or have it look for the JSObjectIdKey
too if there's already one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is more about adding an extensibility mechanism to processing values sent from JS to .NET that is symmetrical to "JSON revivers" used in the other direction (when handling arguments coming from .NET to JS). I am also not sure if we want to add that but we should evaluate it in these terms.
Alternatively, if we only care about supporting this use case with ElementReference
, we can reuse the JSObjectReference
ID representation and then special-case handling of Element
instances in createJSCallResult
in Microsoft.JSInterop.ts
(or some other related function, I haven't look at it in detail).
Uh oh!
There was an error while loading. Please reload this page.