diff --git a/.gitignore b/.gitignore index 0bb699c..f4fca42 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ .astro dist/ tmp/ -docs/public/llm.txt \ No newline at end of file +docs/public/llm.txt +*~ diff --git a/src/WebWorkersAPI.res b/src/WebWorkersAPI.res index e7fb89c..6677015 100644 --- a/src/WebWorkersAPI.res +++ b/src/WebWorkersAPI.res @@ -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. @@ -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 — @@ -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 +} diff --git a/src/WebWorkersAPI/SharedWorker.js b/src/WebWorkersAPI/SharedWorker.js new file mode 100644 index 0000000..dbf8d92 --- /dev/null +++ b/src/WebWorkersAPI/SharedWorker.js @@ -0,0 +1,7 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as EventTarget$WebAPI from "../EventAPI/EventTarget.js"; + +EventTarget$WebAPI.Impl({}); + +/* Not a pure module */ diff --git a/src/WebWorkersAPI/SharedWorker.res b/src/WebWorkersAPI/SharedWorker.res new file mode 100644 index 0000000..105e44a --- /dev/null +++ b/src/WebWorkersAPI/SharedWorker.res @@ -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" diff --git a/src/WebWorkersAPI/SharedWorkerGlobalScope.js b/src/WebWorkersAPI/SharedWorkerGlobalScope.js new file mode 100644 index 0000000..e33fbad --- /dev/null +++ b/src/WebWorkersAPI/SharedWorkerGlobalScope.js @@ -0,0 +1,15 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as WorkerGlobalScope$WebAPI from "./WorkerGlobalScope.js"; + +function Impl(T) { + WorkerGlobalScope$WebAPI.Impl({}); + return {}; +} + +WorkerGlobalScope$WebAPI.Impl({}); + +export { + Impl, +} +/* Not a pure module */ diff --git a/src/WebWorkersAPI/SharedWorkerGlobalScope.res b/src/WebWorkersAPI/SharedWorkerGlobalScope.res new file mode 100644 index 0000000..bd603c1 --- /dev/null +++ b/src/WebWorkersAPI/SharedWorkerGlobalScope.res @@ -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 +}) + diff --git a/tests/WebWorkersAPI/SharedWorkerGlobalScope__test.js b/tests/WebWorkersAPI/SharedWorkerGlobalScope__test.js new file mode 100644 index 0000000..12dd312 --- /dev/null +++ b/tests/WebWorkersAPI/SharedWorkerGlobalScope__test.js @@ -0,0 +1,11 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + + +let self = self(); + +self.close(); + +export { + self, +} +/* self Not a pure module */ diff --git a/tests/WebWorkersAPI/SharedWorkerGlobalScope__test.res b/tests/WebWorkersAPI/SharedWorkerGlobalScope__test.res new file mode 100644 index 0000000..34c3bbf --- /dev/null +++ b/tests/WebWorkersAPI/SharedWorkerGlobalScope__test.res @@ -0,0 +1,7 @@ +open WebAPI.WebWorkersAPI + +external getSelf: () => sharedWorkerGlobalScope = "self" + +let self = getSelf() + +self -> SharedWorkerGlobalScope.close diff --git a/tests/WebWorkersAPI/SharedWorker__test.js b/tests/WebWorkersAPI/SharedWorker__test.js new file mode 100644 index 0000000..44fee58 --- /dev/null +++ b/tests/WebWorkersAPI/SharedWorker__test.js @@ -0,0 +1,26 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + + +let shared1 = new SharedWorker("sharedworker.js"); + +let shared2 = new SharedWorker("sharedworker.js", "name"); + +let shared3 = new SharedWorker("sharedworker.js", { + type: "module", + name: "workerName" +}); + +let port = shared1.port; + +let self = self(); + +self.close(); + +export { + shared1, + shared2, + shared3, + port, + self, +} +/* shared1 Not a pure module */ diff --git a/tests/WebWorkersAPI/SharedWorker__test.res b/tests/WebWorkersAPI/SharedWorker__test.res new file mode 100644 index 0000000..e8bd388 --- /dev/null +++ b/tests/WebWorkersAPI/SharedWorker__test.res @@ -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