|
| 1 | +@@warning("-30") |
| 2 | + |
| 3 | +open Prelude |
| 4 | +open Event |
| 5 | + |
| 6 | +type binaryType = |
| 7 | + | @as("arraybuffer") Arraybuffer |
| 8 | + | @as("blob") Blob |
| 9 | + |
| 10 | +type messageEventSource = any |
| 11 | + |
| 12 | +/** |
| 13 | +Provides the API for creating and managing a WebSocket connection to a server, as well as for sending and receiving data on the connection. |
| 14 | +[See WebSocket on MDN](https://developer.mozilla.org/docs/Web/API/WebSocket) |
| 15 | +*/ |
| 16 | +type webSocket = { |
| 17 | + ...eventTarget, |
| 18 | + /** |
| 19 | + Returns the URL that was used to establish the WebSocket connection. |
| 20 | + [Read more on MDN](https://developer.mozilla.org/docs/Web/API/WebSocket/url) |
| 21 | + */ |
| 22 | + url: string, |
| 23 | + /** |
| 24 | + Returns the state of the WebSocket object's connection. It can have the values described below. |
| 25 | + [Read more on MDN](https://developer.mozilla.org/docs/Web/API/WebSocket/readyState) |
| 26 | + */ |
| 27 | + readyState: int, |
| 28 | + /** |
| 29 | + Returns the number of bytes of application data (UTF-8 text and binary data) that have been queued using send() but not yet been transmitted to the network. |
| 30 | +
|
| 31 | +If the WebSocket connection is closed, this attribute's value will only increase with each call to the send() method. (The number does not reset to zero once the connection closes.) |
| 32 | + [Read more on MDN](https://developer.mozilla.org/docs/Web/API/WebSocket/bufferedAmount) |
| 33 | + */ |
| 34 | + bufferedAmount: int, |
| 35 | + /** |
| 36 | + Returns the extensions selected by the server, if any. |
| 37 | + [Read more on MDN](https://developer.mozilla.org/docs/Web/API/WebSocket/extensions) |
| 38 | + */ |
| 39 | + extensions: string, |
| 40 | + /** |
| 41 | + Returns the subprotocol selected by the server, if any. It can be used in conjunction with the array form of the constructor's second argument to perform subprotocol negotiation. |
| 42 | + [Read more on MDN](https://developer.mozilla.org/docs/Web/API/WebSocket/protocol) |
| 43 | + */ |
| 44 | + protocol: string, |
| 45 | + /** |
| 46 | + Returns a string that indicates how binary data from the WebSocket object is exposed to scripts: |
| 47 | +
|
| 48 | +Can be set, to change how binary data is returned. The default is "blob". |
| 49 | + [Read more on MDN](https://developer.mozilla.org/docs/Web/API/WebSocket/binaryType) |
| 50 | + */ |
| 51 | + mutable binaryType: binaryType, |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | +A CloseEvent is sent to clients using WebSockets when the connection is closed. This is delivered to the listener indicated by the WebSocket object's onclose attribute. |
| 56 | +[See CloseEvent on MDN](https://developer.mozilla.org/docs/Web/API/CloseEvent) |
| 57 | +*/ |
| 58 | +type closeEvent = { |
| 59 | + ...event, |
| 60 | + /** |
| 61 | + Returns true if the connection closed cleanly; false otherwise. |
| 62 | + [Read more on MDN](https://developer.mozilla.org/docs/Web/API/CloseEvent/wasClean) |
| 63 | + */ |
| 64 | + wasClean: bool, |
| 65 | + /** |
| 66 | + Returns the WebSocket connection close code provided by the server. |
| 67 | + [Read more on MDN](https://developer.mozilla.org/docs/Web/API/CloseEvent/code) |
| 68 | + */ |
| 69 | + code: int, |
| 70 | + /** |
| 71 | + Returns the WebSocket connection close reason provided by the server. |
| 72 | + [Read more on MDN](https://developer.mozilla.org/docs/Web/API/CloseEvent/reason) |
| 73 | + */ |
| 74 | + reason: string, |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | +This Channel Messaging API interface represents one of the two ports of a MessageChannel, allowing messages to be sent from one port and listening out for them arriving at the other. |
| 79 | +[See MessagePort on MDN](https://developer.mozilla.org/docs/Web/API/MessagePort) |
| 80 | +*/ |
| 81 | +type messagePort = { |
| 82 | + ...eventTarget, |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | +A message received by a target object. |
| 87 | +[See MessageEvent on MDN](https://developer.mozilla.org/docs/Web/API/MessageEvent) |
| 88 | +*/ |
| 89 | +type messageEvent<'t> = { |
| 90 | + ...event, |
| 91 | + /** |
| 92 | + Returns the data of the message. |
| 93 | + [Read more on MDN](https://developer.mozilla.org/docs/Web/API/MessageEvent/data) |
| 94 | + */ |
| 95 | + data: 't, |
| 96 | + /** |
| 97 | + Returns the origin of the message, for server-sent events and cross-document messaging. |
| 98 | + [Read more on MDN](https://developer.mozilla.org/docs/Web/API/MessageEvent/origin) |
| 99 | + */ |
| 100 | + origin: string, |
| 101 | + /** |
| 102 | + Returns the last event ID string, for server-sent events. |
| 103 | + [Read more on MDN](https://developer.mozilla.org/docs/Web/API/MessageEvent/lastEventId) |
| 104 | + */ |
| 105 | + lastEventId: string, |
| 106 | + /** |
| 107 | + Returns the WindowProxy of the source window, for cross-document messaging, and the MessagePort being attached, in the connect event fired at SharedWorkerGlobalScope objects. |
| 108 | + [Read more on MDN](https://developer.mozilla.org/docs/Web/API/MessageEvent/source) |
| 109 | + */ |
| 110 | + source: Null.t<messageEventSource>, |
| 111 | + /** |
| 112 | + Returns the MessagePort array sent with the message, for cross-document messaging and channel messaging. |
| 113 | + [Read more on MDN](https://developer.mozilla.org/docs/Web/API/MessageEvent/ports) |
| 114 | + */ |
| 115 | + ports: array<messagePort>, |
| 116 | +} |
| 117 | + |
| 118 | +type closeEventInit = { |
| 119 | + ...eventInit, |
| 120 | + mutable wasClean: bool, |
| 121 | + mutable code: int, |
| 122 | + mutable reason: string, |
| 123 | +} |
| 124 | + |
| 125 | +type messageEventInit<'t> = { |
| 126 | + ...eventInit, |
| 127 | + mutable data: 't, |
| 128 | + mutable origin: string, |
| 129 | + mutable lastEventId: string, |
| 130 | + mutable source: Null.t<messageEventSource>, |
| 131 | + mutable ports: array<messagePort>, |
| 132 | +} |
| 133 | + |
| 134 | +module WebSocket = { |
| 135 | + /** |
| 136 | + [Read more on MDN](https://developer.mozilla.org/docs/Web/API/WebSocket) |
| 137 | + */ |
| 138 | + @new |
| 139 | + external make: (string, unknown) => webSocket = "WebSocket" |
| 140 | + /** |
| 141 | + Closes the WebSocket connection, optionally using code as the the WebSocket connection close code and reason as the the WebSocket connection close reason. |
| 142 | + [Read more on MDN](https://developer.mozilla.org/docs/Web/API/WebSocket/close) |
| 143 | + */ |
| 144 | + @send |
| 145 | + external close: (webSocket, int, string) => unit = "close" |
| 146 | + |
| 147 | + /** |
| 148 | + Transmits data using the WebSocket connection. data can be a string, a Blob, an ArrayBuffer, or an ArrayBufferView. |
| 149 | + [Read more on MDN](https://developer.mozilla.org/docs/Web/API/WebSocket/send) |
| 150 | + */ |
| 151 | + @send |
| 152 | + external send: (webSocket, unknown) => unit = "send" |
| 153 | +} |
| 154 | + |
| 155 | +module CloseEvent = { |
| 156 | + /** |
| 157 | + [Read more on MDN](https://developer.mozilla.org/docs/Web/API/CloseEvent) |
| 158 | + */ |
| 159 | + @new |
| 160 | + external make: (string, closeEventInit) => closeEvent = "CloseEvent" |
| 161 | +} |
| 162 | + |
| 163 | +module MessagePort = { |
| 164 | + /** |
| 165 | + Posts a message through the channel. Objects listed in transfer are transferred, not just cloned, meaning that they are no longer usable on the sending side. |
| 166 | +
|
| 167 | +Throws a "DataCloneError" DOMException if transfer contains duplicate objects or port, or if message could not be cloned. |
| 168 | + [Read more on MDN](https://developer.mozilla.org/docs/Web/API/MessagePort/postMessage) |
| 169 | + */ |
| 170 | + @send |
| 171 | + external postMessage: (messagePort, any, array<Dict.t<string>>) => unit = "postMessage" |
| 172 | + |
| 173 | + /** |
| 174 | + Begins dispatching messages received on the port. |
| 175 | + [Read more on MDN](https://developer.mozilla.org/docs/Web/API/MessagePort/start) |
| 176 | + */ |
| 177 | + @send |
| 178 | + external start: messagePort => unit = "start" |
| 179 | + |
| 180 | + /** |
| 181 | + Disconnects the port, so that it is no longer active. |
| 182 | + [Read more on MDN](https://developer.mozilla.org/docs/Web/API/MessagePort/close) |
| 183 | + */ |
| 184 | + @send |
| 185 | + external close: messagePort => unit = "close" |
| 186 | +} |
| 187 | + |
| 188 | +module MessageEvent = { |
| 189 | + /** |
| 190 | + [Read more on MDN](https://developer.mozilla.org/docs/Web/API/MessageEvent) |
| 191 | + */ |
| 192 | + @new |
| 193 | + external make: (string, messageEventInit<'t>) => messageEvent<'t> = "MessageEvent" |
| 194 | +} |
0 commit comments