Skip to content
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

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

Open
kekekeks opened this issue May 8, 2024 · 1 comment
Assignees
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation arch-wasm WebAssembly architecture area-System.Runtime.InteropServices.JavaScript feature-request os-browser Browser variant of arch-wasm
Milestone

Comments

@kekekeks
Copy link

kekekeks commented May 8, 2024

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

@kekekeks kekekeks added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label May 8, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label May 8, 2024
@lewing lewing added this to the Future milestone May 10, 2024
@lewing lewing removed the untriaged New issue has not been triaged by the area owner label May 10, 2024
@pavelsavara pavelsavara added arch-wasm WebAssembly architecture feature-request os-browser Browser variant of arch-wasm labels May 13, 2024
@pavelsavara
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation arch-wasm WebAssembly architecture area-System.Runtime.InteropServices.JavaScript feature-request os-browser Browser variant of arch-wasm
Projects
None yet
Development

No branches or pull requests

3 participants