Skip to content

Commit abba799

Browse files
committed
WebSocket and WebStorage
1 parent c88834c commit abba799

File tree

5 files changed

+399
-33
lines changed

5 files changed

+399
-33
lines changed

src/WebSockets.js

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

src/WebSockets.res

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
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+
}

src/WebStorage.js

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

src/WebStorage.res

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
@@warning("-30")
2+
3+
open Event
4+
5+
/**
6+
This Web Storage API interface provides access to a particular domain's session or local storage. It allows, for example, the addition, modification, or deletion of stored data items.
7+
[See Storage on MDN](https://developer.mozilla.org/docs/Web/API/Storage)
8+
*/
9+
type storage = {
10+
/**
11+
Returns the number of key/value pairs.
12+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Storage/length)
13+
*/
14+
length: int,
15+
}
16+
17+
/**
18+
A StorageEvent is sent to a window when a storage area it has access to is changed within the context of another document.
19+
[See StorageEvent on MDN](https://developer.mozilla.org/docs/Web/API/StorageEvent)
20+
*/
21+
type storageEvent = {
22+
...event,
23+
/**
24+
Returns the key of the storage item being changed.
25+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/StorageEvent/key)
26+
*/
27+
key: Null.t<string>,
28+
/**
29+
Returns the old value of the key of the storage item whose value is being changed.
30+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/StorageEvent/oldValue)
31+
*/
32+
oldValue: Null.t<string>,
33+
/**
34+
Returns the new value of the key of the storage item whose value is being changed.
35+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/StorageEvent/newValue)
36+
*/
37+
newValue: Null.t<string>,
38+
/**
39+
Returns the URL of the document whose storage item changed.
40+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/StorageEvent/url)
41+
*/
42+
url: string,
43+
/**
44+
Returns the Storage object that was affected.
45+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/StorageEvent/storageArea)
46+
*/
47+
storageArea: Null.t<storage>,
48+
}
49+
50+
type storageEventInit = {
51+
...eventInit,
52+
mutable key: Null.t<string>,
53+
mutable oldValue: Null.t<string>,
54+
mutable newValue: Null.t<string>,
55+
mutable url: string,
56+
mutable storageArea: Null.t<storage>,
57+
}
58+
59+
module Storage = {
60+
/**
61+
Returns the name of the nth key, or null if n is greater than or equal to the number of key/value pairs.
62+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Storage/key)
63+
*/
64+
@send
65+
external key: (storage, int) => string = "key"
66+
67+
/**
68+
Returns the current value associated with the given key, or null if the given key does not exist.
69+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Storage/getItem)
70+
*/
71+
@send
72+
external getItem: (storage, string) => string = "getItem"
73+
74+
/**
75+
Sets the value of the pair identified by key to value, creating a new key/value pair if none existed for key previously.
76+
77+
Throws a "QuotaExceededError" DOMException exception if the new value couldn't be set. (Setting could fail if, e.g., the user has disabled storage for the site, or if the quota has been exceeded.)
78+
79+
Dispatches a storage event on Window objects holding an equivalent Storage object.
80+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Storage/setItem)
81+
*/
82+
@send
83+
external setItem: (storage, string, string) => unit = "setItem"
84+
85+
/**
86+
Removes the key/value pair with the given key, if a key/value pair with the given key exists.
87+
88+
Dispatches a storage event on Window objects holding an equivalent Storage object.
89+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Storage/removeItem)
90+
*/
91+
@send
92+
external removeItem: (storage, string) => unit = "removeItem"
93+
94+
/**
95+
Removes all key/value pairs, if there are any.
96+
97+
Dispatches a storage event on Window objects holding an equivalent Storage object.
98+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Storage/clear)
99+
*/
100+
@send
101+
external clear: storage => unit = "clear"
102+
}
103+
104+
module StorageEvent = {
105+
/**
106+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/StorageEvent)
107+
*/
108+
@new
109+
external make: (string, storageEventInit) => storageEvent = "StorageEvent"
110+
}

0 commit comments

Comments
 (0)