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

feat: add worker api #19

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
15 changes: 15 additions & 0 deletions src/Webapi/Dom/Webapi__Dom__DedicatedWorkerGlobalScope.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type t = Dom.dedicatedWorkerGlobalScope;

include Webapi__Dom__WorkerGlobalScope.Impl({
type nonrec t = t;
});

[@mel.get] external name: t => string = "name";

[@mel.send.pipe: t] external close: unit = "name";

[@mel.send.pipe: t] external postMessage: 'a => unit = "postMessage";

[@mel.send.pipe: t]
external postMessageWithTransfers: ('a, array(Webapi__Dom__Window.transferable)) => unit =
"postMessage";
5 changes: 5 additions & 0 deletions src/Webapi/Dom/Webapi__Dom__MessageChannel.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type t = Dom.messageChannel;

[@mel.new] external make: unit => t = "MessageChannel";
[@mel.get] external port1: t => Webapi__Dom__MessagePort.t = "port1";
[@mel.get] external port2: t => Webapi__Dom__MessagePort.t = "port2";
23 changes: 23 additions & 0 deletions src/Webapi/Dom/Webapi__Dom__MessageEvent.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
type t = Dom.messageEvent;

include Webapi__Dom__Event.Impl({
type nonrec t = t;
});

[@mel.new] external make: string => t = "MessageEvent";
[@mel.new]
external makeWithOptions: (string, Js.t({..})) => t = "MessageEvent";

[@mel.get] external data: t => 'a = "data";
[@mel.get] external origin: t => string = "origin";
[@mel.get] external lastEventId: t => string = "lastEventId";
[@mel.get]
external source:
t =>
[@mel.unwrap] [
| `Window(Dom.window)
| `MessagePort(Dom.messagePort)
| `ServiceWorker(Dom.serviceWorker)
] =
"source";
[@mel.get] external ports: t => array(Dom.messagePort) = "ports";
27 changes: 27 additions & 0 deletions src/Webapi/Dom/Webapi__Dom__MessagePort.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
type t = Dom.messagePort;

include Webapi__Dom__EventTarget.Impl({
type nonrec t = t;
});

[@mel.send.pipe: t] external start: unit => unit = "start";
[@mel.send.pipe: t] external close: unit => unit = "close";

[@mel.send.pipe: t] external postMessage: 'a => unit = "postMessage";
[@mel.send.pipe: t] external postMessageWithTransfers: ('a, array(Webapi__Dom__Window.transferable)) => unit = "postMessage";

[@mel.send.pipe: t]
external addMessageEventListener:
([@mel.as "message"] _, Dom.messageEvent => unit) => unit =
"addEventListener";

[@mel.send.pipe: t]
external removeMessageEventListener:
([@mel.as "message"] _, Dom.messageEvent => unit) => unit =
"removeEventListener";

[@mel.set]
external setOnMessage: (t, Dom.messageEvent => unit) => unit = "onmessage";
[@mel.set]
external setOnMessageError: (t, Dom.messageEvent => unit) => unit =
"onmessageerror";
38 changes: 38 additions & 0 deletions src/Webapi/Dom/Webapi__Dom__Worker.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
type t = Dom.worker;

include Webapi__Dom__EventTarget.Impl({
type nonrec t = t;
});

[@mel.new] external make: string => t = "Worker";
[@mel.new] external makeWithUrl: Webapi__Url.t => t = "Worker";
[@mel.send.pipe: t] external postMessage: 'a => unit = "postMessage";
[@mel.send.pipe: t] external terminate: unit = "terminate";

[@mel.send.pipe: t]
external addMessageEventListener:
([@mel.as "message"] _, Dom.messageEvent => unit) => unit =
"addEventListener";

[@mel.send.pipe: t]
external removeMessageEventListener:
([@mel.as "message"] _, Dom.messageEvent => unit) => unit =
"removeEventListener";

[@mel.send.pipe: t]
external addMessageErrorEventListener:
([@mel.as "message"] _, Dom.messageEvent => unit) => unit =
"addEventListener";

[@mel.send.pipe: t]
external removeMessageErrorEventListener:
([@mel.as "message"] _, Dom.messageEvent => unit) => unit =
"removeEventListener";

[@mel.set]
external setOnError: (t, Dom.errorEvent => unit) => unit = "onerror";
[@mel.set]
external setOnMessage: (t, Dom.messageEvent => unit) => unit = "onmessage";
[@mel.set]
external setOnMessageError: (t, Dom.messageEvent => unit) => unit =
"onmessageerror";
41 changes: 41 additions & 0 deletions src/Webapi/Dom/Webapi__Dom__WorkerGlobalScope.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
type t = Dom.workerGlobalScope;

module Impl = (T: {
type t;
}) => {
[@mel.get] external self: T.t => T.t = "self";

[@mel.send.pipe: T.t]
external addErrorEventListener:
([@mel.as "error"] _, Dom.messageEvent => unit) => unit =
"addEventListener";

[@mel.send.pipe: T.t]
external removeErrorEventListener:
([@mel.as "error"] _, Dom.messageEvent => unit) => unit =
"removeEventListener";

[@mel.send.pipe: T.t]
external addMessageEventListener:
([@mel.as "message"] _, Dom.messageEvent => unit) => unit =
"addEventListener";

[@mel.send.pipe: T.t]
external removeMessageEventListener:
([@mel.as "message"] _, Dom.messageEvent => unit) => unit =
"removeEventListener";

[@mel.send.pipe: T.t]
external addMessageErrorEventListener:
([@mel.as "messageerror"] _, Dom.messageEvent => unit) => unit =
"addEventListener";

[@mel.send.pipe: T.t]
external removeMessageErrorEventListener:
([@mel.as "messageerror"] _, Dom.messageEvent => unit) => unit =
"removeEventListener";
};

include Webapi__Dom__EventTarget.Impl({
type nonrec t = t;
});
7 changes: 7 additions & 0 deletions src/Webapi/Webapi__Dom.re
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ module Image = Webapi__Dom__Image;
module InputEvent = Webapi__Dom__InputEvent;
module KeyboardEvent = Webapi__Dom__KeyboardEvent;
module Location = Webapi__Dom__Location;
module MessageEvent = Webapi__Dom__MessageEvent;
module MessageChannel = Webapi__Dom__MessageChannel;
module MessagePort = Webapi__Dom__MessagePort;
module MouseEvent = Webapi__Dom__MouseEvent;
module MutationObserver = Webapi__Dom__MutationObserver;
module MutationRecord = Webapi__Dom__MutationRecord;
Expand Down Expand Up @@ -64,10 +67,14 @@ module ValidityState = Webapi__Dom__ValidityState;
module WebGlContextEvent = Webapi__Dom__WebGlContextEvent;
module WheelEvent = Webapi__Dom__WheelEvent;
module Window = Webapi__Dom__Window;
module Worker = Webapi__Dom__Worker;
module WorkerGlobalScope = Webapi__Dom__WorkerGlobalScope;
module DedicatedWorkerGlobalScope = Webapi__Dom__DedicatedWorkerGlobalScope;

include Webapi__Dom__Types;

external window: Dom.window = "window";
external dedicatedWorkerGlobalScope: Dom.dedicatedWorkerGlobalScope = "self";
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davesnx Is it better to have a globalThis here? Like

external globalThis: 'a = "globalThis"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we should add globalThis

external document: Dom.document = "document";
[@mel.scope "window"] external history: Dom.history = "history";
[@mel.scope "window"] external location: Dom.location = "location";
Expand Down
Loading