dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.48k stars 4.76k forks source link

[browser][MT][API Proposal]: Provide an API to transfer JS objects between main browser thread and JSWebWorker contexts #102011

Open kekekeks opened 7 months ago

kekekeks commented 7 months ago

Background and motivation

Certain javascript objects are intended to be transferred to web worker scope rather than be just copied. Notable examples are OffscreenCanvas, streams, video frames, webrtc, etc.

API Proposal

namespace System.Runtime.InteropServices.JavaScript;

public class JSHost
{
    /// <summary>
    /// Transfers the object from its context into the current scope via postMessage API.
    ///  Passed JSObject instance immediately becomes disposed and is no longer usable after this call.
    ///  Users should await the task and use the transferred object instead.
    /// </summary>
    /// <param name="object">The object to be transferred into the current context.</param>
    /// <returns>A task that completes with a reference to the new object in the current context.</returns>
    public Task<JSObject> TransferToCurrentContextAsync(JSObject object);
}

API Usage

JSObject offscreenCanvas = JsImportSafeNativeMethods.TransferControlToOffscreen(canvas);
JSWebWorker.RunAsync(() =>
{
     var canvas = await JSHost.TransferToCurrentContextAsync(offscreenCanvas);
     // use canvas
});

Alternative Designs

Provide a way to add handlers to custom messages for message event and postMessage API of Worker /DedicatedWorkerGlobalScope objects

@pavelsavara

pavelsavara commented 6 months ago

It would be good to be able to postMessage between UI thread and any JSWebWorker in both directions. Also to subscribe to event handler. For that I think we would be better off if we modeled the API in the same way as postMessage and onMessage. For that we need to somehow pass the identity of the JSWebWorker to the main thread. At the moment the draft of the JSWebWorker API doesn't offer such identity or channel.