Skip to content

Commit fb395c3

Browse files
authored
Merge pull request #14 from rescript-lang/extract-event-target
Extract reusable functions in inheritance
2 parents a0d5168 + 55e32d6 commit fb395c3

File tree

268 files changed

+2186
-52509
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

268 files changed

+2186
-52509
lines changed

docs/content/docs/contributing/api-modelling.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: API Modelling
33
description: Learn more about the API modelling process of @rescript/webapi.
4-
slug: "04-api-modelling"
4+
slug: "05-api-modelling"
55
---
66

77
import { Aside, Code, Icon } from "@astrojs/starlight/components";

docs/content/docs/contributing/module-structure.mdx renamed to docs/content/docs/contributing/api-module-structure.mdx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
title: Module Structure
3-
description: Learn more about the module structure of @rescript/webapi.
4-
slug: "02-module-structure"
2+
title: API Module Structure
3+
description: Learn more about the API module structure of @rescript/webapi.
4+
slug: "02-api-module-structure"
55
---
66

77
import { Aside } from "@astrojs/starlight/components";

docs/content/docs/contributing/code-generation.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Code Generation
33
description: Learn more about the code generation process for @rescript/webapi.
4-
slug: "03-code-generation"
4+
slug: "04-code-generation"
55
---
66

77
The original bindings were generated using a modified version of [TypeScript-DOM-lib-generator](https://github.com/microsoft/TypeScript-DOM-lib-generator).

docs/content/docs/contributing/documentation.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Documentation"
33
description: Learn more about the relevance of adding documentation to @rescript/webapi.
4-
slug: "06-documentation"
4+
slug: "07-documentation"
55
---
66

77
After the bindings are generated, all you got was a link to the MDN documentation.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
title: Module Type Structure
3+
description: Learn more about the module structure of @rescript/webapi.
4+
slug: "03-module-type-structure"
5+
---
6+
7+
import { Aside, FileTree, Code } from "@astrojs/starlight/components";
8+
9+
Every interface in a Web API module can potentially contain methods. These methods are modeled in a separate module named after the interface.
10+
11+
The primary reason for this separation is to handle method overloads.
12+
As explained in the [Design Philosophy](../design-philosophy) section, ReScript does not permit records to define the same properties more than once.
13+
Therefore, methods with overloads cannot be modeled within the same record type.
14+
15+
## Bindings
16+
17+
Another advantage of having a separate file is that these bindings can utilize all types defined in the API module.
18+
Under normal circumstances, the type module only contains `@send` bindings where the type is the first parameter.
19+
20+
<FileTree>
21+
22+
- DOMAPI
23+
- HTMLButtonElement.res
24+
25+
</FileTree>
26+
27+
```ReScript
28+
/**
29+
Returns whether a form will validate when it is submitted, without having to submit it.
30+
[Read more on MDN](
31+
https://developer.mozilla.org/docs/Web/API/HTMLButtonElement/checkValidity)
32+
*/
33+
@send
34+
external checkValidity: htmlButtonElement => bool = "checkValidity"
35+
```
36+
37+
## Inheritance
38+
39+
When an interface inherits from another interface, the base interface methods can be [included](https://rescript-lang.org/syntax-lookup#include) into the inheriting interface.
40+
All methods from [HTMLElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement#instance_methods) should also be available on [HTMLButtonElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLButtonElement#instance_methods).
41+
42+
export const htmlElementModule = `
43+
open DOMAPI
44+
45+
// A concrete type for \`T.t\` is passed later using the \`include\` keyword.
46+
module Impl = (T: { type t }) => {
47+
48+
/**
49+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/HTMLElement/focus)
50+
*/
51+
@send
52+
external focus: (T.t, ~options: focusOptions=?) => unit = "focus"
53+
54+
}
55+
56+
include Impl({ type t = htmlElement })
57+
`;
58+
59+
<Code
60+
code={htmlElementModule}
61+
title="DOMAPI/HTMLElement.res"
62+
lang="ReScript"
63+
></Code>
64+
65+
export const buttonModule = `
66+
open DOMAPI
67+
68+
// Include all the methods from HTMLElement
69+
include HTMLElement.Impl({ type t = htmlButtonElement })
70+
71+
// Add additional methods specific to HTMLButtonElement:
72+
73+
/**
74+
Returns whether a form will validate when it is submitted, without having to submit it.
75+
[Read more on MDN](
76+
https://developer.mozilla.org/docs/Web/API/HTMLButtonElement/checkValidity)
77+
*/
78+
@send
79+
external checkValidity: htmlButtonElement => bool = "checkValidity"
80+
`;
81+
82+
<Code
83+
code={buttonModule}
84+
title="DOMAPI/HTMLButtonElement.res"
85+
lang="ReScript"
86+
></Code>

docs/content/docs/contributing/testing.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Testing
33
description: Learn more about testing the bindings for @rescript/webapi.
4-
slug: "05-testing"
4+
slug: "06-testing"
55
---
66

77
import { Aside, FileTree } from "@astrojs/starlight/components";

src/CSSFontLoadingAPI/FontFaceSet.js

+6-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CSSFontLoadingAPI/FontFaceSet.res

+3-79
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,9 @@
11
open EventAPI
22
open CSSFontLoadingAPI
33

4-
external asEventTarget: fontFaceSet => eventTarget = "%identity"
5-
/**
6-
Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
7-
8-
The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.
9-
10-
When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.
11-
12-
When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.
13-
14-
When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.
15-
16-
If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.
17-
18-
The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.
19-
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener)
20-
*/
21-
@send
22-
external addEventListener: (
23-
fontFaceSet,
24-
~type_: eventType,
25-
~callback: eventListener<'event>,
26-
~options: addEventListenerOptions=?,
27-
) => unit = "addEventListener"
28-
29-
/**
30-
Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
31-
32-
The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.
33-
34-
When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.
35-
36-
When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.
37-
38-
When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.
39-
40-
If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.
41-
42-
The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.
43-
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener)
44-
*/
45-
@send
46-
external addEventListener2: (
47-
fontFaceSet,
48-
~type_: eventType,
49-
~callback: eventListener<'event>,
50-
~options: bool=?,
51-
) => unit = "addEventListener"
52-
53-
/**
54-
Removes the event listener in target's event listener list with the same type, callback, and options.
55-
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/EventTarget/removeEventListener)
56-
*/
57-
@send
58-
external removeEventListener: (
59-
fontFaceSet,
60-
~type_: eventType,
61-
~callback: eventListener<'event>,
62-
~options: eventListenerOptions=?,
63-
) => unit = "removeEventListener"
64-
65-
/**
66-
Removes the event listener in target's event listener list with the same type, callback, and options.
67-
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/EventTarget/removeEventListener)
68-
*/
69-
@send
70-
external removeEventListener2: (
71-
fontFaceSet,
72-
~type_: eventType,
73-
~callback: eventListener<'event>,
74-
~options: bool=?,
75-
) => unit = "removeEventListener"
76-
77-
/**
78-
Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.
79-
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/EventTarget/dispatchEvent)
80-
*/
81-
@send
82-
external dispatchEvent: (fontFaceSet, event) => bool = "dispatchEvent"
4+
include EventTarget.Impl({
5+
type t = fontFaceSet
6+
})
837

848
/**
859
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/FontFaceSet/add)

src/CanvasAPI/OffscreenCanvas.js

+6-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CanvasAPI/OffscreenCanvas.res

+3-79
Original file line numberDiff line numberDiff line change
@@ -9,85 +9,9 @@ open FileAPI
99
@new
1010
external make: (~width: int, ~height: int) => offscreenCanvas = "OffscreenCanvas"
1111

12-
external asEventTarget: offscreenCanvas => eventTarget = "%identity"
13-
/**
14-
Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
15-
16-
The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.
17-
18-
When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.
19-
20-
When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.
21-
22-
When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.
23-
24-
If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.
25-
26-
The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.
27-
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener)
28-
*/
29-
@send
30-
external addEventListener: (
31-
offscreenCanvas,
32-
~type_: eventType,
33-
~callback: eventListener<'event>,
34-
~options: addEventListenerOptions=?,
35-
) => unit = "addEventListener"
36-
37-
/**
38-
Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
39-
40-
The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.
41-
42-
When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.
43-
44-
When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.
45-
46-
When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.
47-
48-
If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.
49-
50-
The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.
51-
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener)
52-
*/
53-
@send
54-
external addEventListener2: (
55-
offscreenCanvas,
56-
~type_: eventType,
57-
~callback: eventListener<'event>,
58-
~options: bool=?,
59-
) => unit = "addEventListener"
60-
61-
/**
62-
Removes the event listener in target's event listener list with the same type, callback, and options.
63-
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/EventTarget/removeEventListener)
64-
*/
65-
@send
66-
external removeEventListener: (
67-
offscreenCanvas,
68-
~type_: eventType,
69-
~callback: eventListener<'event>,
70-
~options: eventListenerOptions=?,
71-
) => unit = "removeEventListener"
72-
73-
/**
74-
Removes the event listener in target's event listener list with the same type, callback, and options.
75-
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/EventTarget/removeEventListener)
76-
*/
77-
@send
78-
external removeEventListener2: (
79-
offscreenCanvas,
80-
~type_: eventType,
81-
~callback: eventListener<'event>,
82-
~options: bool=?,
83-
) => unit = "removeEventListener"
84-
85-
/**
86-
Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.
87-
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/EventTarget/dispatchEvent)
88-
*/
89-
@send
90-
external dispatchEvent: (offscreenCanvas, event) => bool = "dispatchEvent"
12+
include EventTarget.Impl({
13+
type t = offscreenCanvas
14+
})
9115

9216
/**
9317
Returns an object that exposes an API for drawing on the OffscreenCanvas object. contextId specifies the desired API: "2d", "bitmaprenderer", "webgl", or "webgl2". options is handled by that API.

src/ChannelMessagingAPI/MessagePort.js

+6-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)