MicrosoftEdge / MicrosoftEdge/WebView2Feedback
[PostSharedBufferToScript] Ensure Web receives the Buffer
@Lakshmisha-KS is already working on this.
Since Feb 12, 2025.
- Dominant language
- PowerShell
- Stars
- 526
- Forks
- 67
- PR merge metrics
- No merged PRs in 30d
Description
Is your feature request related to a problem? Please describe.
Currently, PostSharedBufferToScript does not ensure that the web has received the buffer. We manually have to post a message from the web once we receive the buffer.
I have faced cases where I posted the buffer but it was not yet received by the web counterpart resulting in a failure.
So, I use the following core as of now to ensure everything works fine:
private readonly Dictionary<long, TaskCompletionSource<object>> _sharedBufferReceivedGuarantor = new();
internal SharedBufferManager(WebView2 webView2)
{
_webView2 = webView2;
_webView2.WebMessageReceived += OnMessageReceivedFromWeb;
}
private void OnMessageReceivedFromWeb(WebView2 sender, CoreWebView2WebMessageReceivedEventArgs args)
{
if (!long.TryParse(args.WebMessageAsJson, out var id) || !_sharedBufferReceivedGuarantor.ContainsKey(id))
{
return;
}
var tcsThatCanBeResolved = _sharedBufferReceivedGuarantor[id];
_sharedBufferReceivedGuarantor.Remove(id);
tcsThatCanBeResolved.SetResult(null);
}
private async Task GuaranteeSharedBufferIsPostedToScript(
CoreWebView2SharedBuffer sharedBuffer, CoreWebView2SharedBufferAccess access, long id)
{
var tcs = new TaskCompletionSource<object>();
_sharedBufferReceivedGuarantor.Add(id, tcs);
_webView2.CoreWebView2.PostSharedBufferToScript(
sharedBuffer, access, $"{{\"id\":{id}}}");
await tcs.Task;
}
const sharedBufferReceivedHandler = e => {
// ...
chrome.webview.postMessage(e.additionalData.id)
}
chrome.webview.addEventListener('sharedbufferreceived', sharedBufferReceivedHandler)
Describe the solution you'd like and alternatives you've considered
It would be good to ensure this internally. The implementation could be similar to what I am using.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.