Skip to content

[WebAPI Request] SharedWorker API Request #112

@switch-to-gitlab

Description

@switch-to-gitlab
Contributor

Hello,

I am working with a not-thread-safe library and so I have to use SharedWorkers.

JavaScript Sample of WebAPI You Wish to Use

const myWorker = new SharedWorker("worker.js");

MDN Link to the Involved Function or Interfaces

MDN Documentation Link: https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker

Thank you for maintaining this project. It is important!

Activity

nojaf

nojaf commented on Jun 19, 2025

@nojaf
Member

Hi there, thank you for opening this issue.
Are you interested in contributing these bindings?
If so, I can give you some pointers to get started!

switch-to-gitlab

switch-to-gitlab commented on Jun 19, 2025

@switch-to-gitlab
ContributorAuthor

@nojaf Yeah I could use a hand. This sounds like a silly question, but when I run the code generator... where is tmp folder with the generated files (ref)?

I see a generated folder... is that it?

switch-to-gitlab

switch-to-gitlab commented on Jun 19, 2025

@switch-to-gitlab
ContributorAuthor

@nojaf Update: wait there's a tmp directory in the root... but if that's it I definitely did something wrong--there doesn't appear to be anything in there that looks like the SharedWorker I'm trying to generate.

nojaf

nojaf commented on Jun 20, 2025

@nojaf
Member

Hello, try uncommenting

// emitFlavor(webidl, new Set(knownTypes.Worker), {
// name: "sharedworker",
// global: ["SharedWorker", "Worker"],
// outputFolder,
// useIteratorObject,
// });

Then you need to update const windowInterface = allInterfaces.find((i) => i.name === "WindowOrWorkerGlobalScope");

I would add

  await fs.writeFile(
    path.join(outputFolder, "sharedworker.json"),
    JSON.stringify(webidl, null, 2),
  );

somewhere, so you have an idea what is present.

switch-to-gitlab

switch-to-gitlab commented on Jun 21, 2025

@switch-to-gitlab
ContributorAuthor

Forgive me, but

Then you need to update const windowInterface = allInterfaces.find((i) => i.name === "WindowOrWorkerGlobalScope");

Where does this go?

nojaf

nojaf commented on Jun 21, 2025

@nojaf
Member

No worries, I was referring to

const windowInterface = allInterfaces.find((i) => i.name === "Window");
if (!windowInterface) throw new Error("Window interface not found");

This will help generate the Global bindings. What is globally available will be different in your SharedWorker context.

switch-to-gitlab

switch-to-gitlab commented on Jun 29, 2025

@switch-to-gitlab
ContributorAuthor

@nojaf Alright, I could use a hand. This commit is my attempt so far, which is pretty far off base.

This seems fairly right to me, but it does not produce valid ReScript in tmp/Temp.res. Instead it produces:

@@warning("-30")

open EventAPI
open ChannelMessagingAPI

type workerType =
  | @as("classic") Classic
  | @as("module") Module

type workerOptions = {
    @as("type") mutable  type_?: workerType,
     mutable credentials?: requestCredentials,
     mutable name?: string,
}

turns sharedWorker's MessagePort object which can be used to communicate with the global environment.
    [Read more on MDN](https://developer.mozilla.org/docs/Web/API/SharedWorker/port)
    */
    port: messagePort,
}

type workerOptions = {
    @as("type") mutable  type_?: workerType,
     mutable credentials?: requestCredentials,
     mutable name?: string,
}

So workerOptions is there twice and somehow there's a chunk missing? With the part that begins "turns" that should obviously be /** Returns.

It does also produce a Global.res file that scans as close enough to what I'm looking for for the SharedWorkerGlobalScope, which is encouraging, but I suspect I'll have questions about how to situate that so the inheritance makes sense with the rest of the API.

nojaf

nojaf commented on Jun 30, 2025

@nojaf
Member

Hmm, I've tried what you have so far and I got:

@@warning("-30")

open EventAPI
open ChannelMessagingAPI

type workerType =
  | @as("classic") Classic
  | @as("module") Module

type workerOptions = {
    @as("type") mutable  type_?: workerType,
     mutable credentials?: requestCredentials,
     mutable name?: string,
}

out of it.
Seems, okay-ish.

Changing the open to open FetchAPI, compiled for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @nojaf@switch-to-gitlab

      Issue actions

        [WebAPI Request] SharedWorker API Request · Issue #112 · rescript-lang/experimental-rescript-webapi