Skip to content

Include SharedWorker in WebWorkerAPI #129

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

Merged
merged 12 commits into from
Jul 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
.astro
dist/
tmp/
docs/public/llm.txt
docs/public/llm.txt
*~
29 changes: 29 additions & 0 deletions src/WebWorkersAPI.res
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
open EventAPI
open FetchAPI

/**
Provides a storage mechanism for Request / Response object pairs that are cached, for example as part of the ServiceWorker life cycle. Note that the Cache interface is exposed to windowed scopes as well as workers. You don't have to use it in conjunction with service workers, even though it is defined in the service worker spec.
Expand All @@ -25,6 +26,8 @@ type multiCacheQueryOptions = {
mutable cacheName?: string,
}

type sharedWorker

/**
The WorkerGlobalScope interface of the Web Workers API is an interface representing the scope of any worker.
Workers have no browsing context; this scope contains the information usually conveyed by Window objects —
Expand All @@ -44,3 +47,29 @@ type workerGlobalScope = {
*/
crossOriginIsolated: bool,
}

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


/** An object containing option properties that can set when creating the
object instance. */
type workerOptions = {
@as("type") mutable type_?: workerType,
mutable credentials?: requestCredentials,
mutable name?: string,
}

/**
The `SharedWorkerGlobalScope` object (the `SharedWorker` global scope) is
accessible through the self keyword. Some additional global functions,
namespaces objects, and constructors, not typically associated with the worker
global scope, but available on it, are listed in the JavaScript Reference. See
the complete list of functions available to workers.
*/
@editor.completeFrom(SharedWorkerGlobalScope)
type sharedWorkerGlobalScope = {
...workerGlobalScope,
name: option<string>
}
7 changes: 7 additions & 0 deletions src/WebWorkersAPI/SharedWorker.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions src/WebWorkersAPI/SharedWorker.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
open ChannelMessagingAPI
open WebWorkersAPI

include EventTarget.Impl({
type t = sharedWorker
})

/**
`make(string)`

The SharedWorker() constructor creates a SharedWorker object that executes the
script at the specified URL. This script must obey the same-origin policy.

```res
let shared: sharedWorker = SharedWorker.make("sharedworker.js")
```

[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker/)
*/
@new
external make: string => sharedWorker = "SharedWorker"

/**
`makeWithName(string, string)`

The SharedWorker() constructor creates a SharedWorker object that executes the
script at the specified URL. This script must obey the same-origin policy.

```res
let shared: sharedWorker = SharedWorker.make("sharedworker.js", "name")
```

[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker/)
*/
@new
external makeWithName: (string, string) => sharedWorker = "SharedWorker"

/**
`makeWithOptions(string, workerOptions)`

The SharedWorker() constructor creates a SharedWorker object that executes the
script at the specified URL. This script must obey the same-origin policy.

```res
let shared: sharedWorker = SharedWorker.makeWithOptions("sharedworker.js", {
name: "workerName",
type_: Module
})
```

[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker/)
*/
@new
external makeWithOptions: (string, workerOptions) => sharedWorker = "SharedWorker"

/**
`port(sharedWorker)`

The port property of the SharedWorker interface returns a MessagePort object
used to communicate and control the shared worker.

```res
let port: WebAPI.ChannelMessagingAPI.messagePort = SharedWorker.port(myWorker)
```

[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker/port)
*/
@get
external port: sharedWorker => messagePort = "port"
15 changes: 15 additions & 0 deletions src/WebWorkersAPI/SharedWorkerGlobalScope.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions src/WebWorkersAPI/SharedWorkerGlobalScope.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
open WebWorkersAPI

module Impl = (
T: {
type t
},
) => {
include WorkerGlobalScope.Impl({
type t = T.t
})

/**
`close(sharedWorkerGlobalScope)`

The close() method of the SharedWorkerGlobalScope interface discards any tasks
queued in the SharedWorkerGlobalScope's event loop, effectively closing this
particular scope.

```res
self -> SharedWorkerGlobalScope.close
```

[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/SharedWorkerGlobalScope/close)
*/
@send
external close: T.t => unit = "close"
}

include Impl({
type t = sharedWorkerGlobalScope
})

11 changes: 11 additions & 0 deletions tests/WebWorkersAPI/SharedWorkerGlobalScope__test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/WebWorkersAPI/SharedWorkerGlobalScope__test.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
open WebAPI.WebWorkersAPI

external getSelf: () => sharedWorkerGlobalScope = "self"

let self = getSelf()

self -> SharedWorkerGlobalScope.close
26 changes: 26 additions & 0 deletions tests/WebWorkersAPI/SharedWorker__test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tests/WebWorkersAPI/SharedWorker__test.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
open WebAPI.WebWorkersAPI

let shared1: sharedWorker = SharedWorker.make("sharedworker.js")

let shared2: sharedWorker = SharedWorker.makeWithName("sharedworker.js", "name")

let shared3: sharedWorker = SharedWorker.makeWithOptions("sharedworker.js", {
name: "workerName",
type_: Module
})

let port: WebAPI.ChannelMessagingAPI.messagePort = SharedWorker.port(shared1)

external getSelf: () => sharedWorkerGlobalScope = "self"

let self = getSelf()

self -> SharedWorkerGlobalScope.close