Skip to content

Commit ea3435c

Browse files
Merge pull request #1 from switch-to-gitlab/feature/shared-worker
Feature/shared worker
2 parents beb6b0c + 5d002a7 commit ea3435c

13 files changed

+235
-13
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
.astro
55
dist/
66
tmp/
7-
docs/public/llm.txt
7+
docs/public/llm.txt
8+
*~

src/WebWorkersAPI.res

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
open EventAPI
2+
open FetchAPI
23

34
/**
45
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.
@@ -25,6 +26,8 @@ type multiCacheQueryOptions = {
2526
mutable cacheName?: string,
2627
}
2728

29+
type sharedWorker
30+
2831
/**
2932
The WorkerGlobalScope interface of the Web Workers API is an interface representing the scope of any worker.
3033
Workers have no browsing context; this scope contains the information usually conveyed by Window objects —
@@ -44,3 +47,29 @@ type workerGlobalScope = {
4447
*/
4548
crossOriginIsolated: bool,
4649
}
50+
51+
type workerType =
52+
| @as("classic") Classic
53+
| @as("module") Module
54+
55+
56+
/** An object containing option properties that can set when creating the
57+
object instance. */
58+
type workerOptions = {
59+
@as("type") mutable type_?: workerType,
60+
mutable credentials?: requestCredentials,
61+
mutable name?: string,
62+
}
63+
64+
/**
65+
The `SharedWorkerGlobalScope` object (the `SharedWorker` global scope) is
66+
accessible through the self keyword. Some additional global functions,
67+
namespaces objects, and constructors, not typically associated with the worker
68+
global scope, but available on it, are listed in the JavaScript Reference. See
69+
the complete list of functions available to workers.
70+
*/
71+
@editor.completeFrom(SharedWorkerGlobalScope)
72+
type sharedWorkerGlobalScope = {
73+
...workerGlobalScope,
74+
name: option<string>
75+
}

src/WebWorkersAPI/SharedWorker.js

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/WebWorkersAPI/SharedWorker.res

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
open ChannelMessagingAPI
2+
open WebWorkersAPI
3+
4+
include EventTarget.Impl({
5+
type t = sharedWorker
6+
})
7+
8+
/**
9+
`make(string)`
10+
11+
The SharedWorker() constructor creates a SharedWorker object that executes the
12+
script at the specified URL. This script must obey the same-origin policy.
13+
14+
```res
15+
let shared: sharedWorker = SharedWorker.make("sharedworker.js")
16+
```
17+
18+
[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker/)
19+
*/
20+
@new
21+
external make: string => sharedWorker = "SharedWorker"
22+
23+
/**
24+
`make_withName(string, string)`
25+
26+
The SharedWorker() constructor creates a SharedWorker object that executes the
27+
script at the specified URL. This script must obey the same-origin policy.
28+
29+
```res
30+
let shared: sharedWorker = SharedWorker.make("sharedworker.js", "name")
31+
```
32+
33+
[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker/)
34+
*/
35+
@new
36+
external make_withName: (string, string) => sharedWorker = "SharedWorker"
37+
38+
/**
39+
`make_withOptions(string, workerOptions)`
40+
41+
The SharedWorker() constructor creates a SharedWorker object that executes the
42+
script at the specified URL. This script must obey the same-origin policy.
43+
44+
```res
45+
let shared3: sharedWorker = SharedWorker.make_withOptions("sharedworker.js", {
46+
name: "workerName",
47+
type_: Module
48+
})
49+
```
50+
51+
[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker/)
52+
*/
53+
@new
54+
external make_withOptions: (string, workerOptions) => sharedWorker = "SharedWorker"
55+
56+
/**
57+
`port(sharedWorker)`
58+
59+
The port property of the SharedWorker interface returns a MessagePort object
60+
used to communicate and control the shared worker.
61+
62+
```res
63+
let port: WebAPI.ChannelMessagingAPI.messagePort = SharedWorker.port(myWorker)
64+
```
65+
66+
[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker/port)
67+
*/
68+
@get
69+
external port: sharedWorker => messagePort = "port"

src/WebWorkersAPI/SharedWorkerGlobalScope.js

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
open WebWorkersAPI
2+
3+
module Impl = (
4+
T: {
5+
type t
6+
},
7+
) => {
8+
include WorkerGlobalScope.Impl({
9+
type t = T.t
10+
})
11+
12+
/**
13+
`close(sharedWorkerGlobalScope)`
14+
15+
The close() method of the SharedWorkerGlobalScope interface discards any tasks
16+
queued in the SharedWorkerGlobalScope's event loop, effectively closing this
17+
particular scope.
18+
19+
```res
20+
self -> SharedWorkerGlobalScope.close
21+
```
22+
23+
[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/SharedWorkerGlobalScope/close)
24+
*/
25+
@send
26+
external close: T.t => unit = "close"
27+
}
28+
29+
include Impl({
30+
type t = sharedWorkerGlobalScope
31+
})
32+

src/WebWorkersAPI/WorkerGlobalScope.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ let response = await self->WorkerGlobalScope.fetch(myRequest)
3636
[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/fetch)
3737
*/
3838
external fetch_withRequest: (T.t, request, ~init: requestInit=?) => promise<response> = "fetch"
39+
external self: T.t = "self"
3940
}
4041

4142
include Impl({type t = workerGlobalScope})

tests/WebWorkersAPI/SharedWorkerGlobalScope__test.js

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
open WebAPI.WebWorkersAPI
2+
3+
external getSelf: () => sharedWorkerGlobalScope = "self"
4+
5+
let self = getSelf()
6+
7+
self -> SharedWorkerGlobalScope.close

tests/WebWorkersAPI/SharedWorker__test.js

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)