Examples
The best example of usage of these bindings are the tests.
These exercise the public bindings directly. Reusable Web API bindings and conversion helpers belong in the library rather than in individual examples.
AddEventListener__test
let button = DomGlobal.document->Document.querySelector("button")->Null.toOptionlet h2 = DomGlobal.document->Document.querySelector("h2")->Null.toOption
switch (button, h2) {| (Some(button), Some(h2)) => button->Element.addEventListener(EventType.Click, (e: UiEventsTypes.mouseEvent) => { Console.log(`Button clicked, ${Int.toString(e.button)}`) switch h2.textContent { | Null => h2.textContent = Value("1") | Value(text) => switch Int.fromString(text) { | None => h2.textContent = Value("1") | Some(number) => h2.textContent = Value(Int.toString(number + 1)) } } })| _ => Console.log("Stuff not found")}import * as Stdlib_Int from "@rescript/runtime/lib/es6/Stdlib_Int.mjs";import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.mjs";
let button = document.querySelector("button");
let h2 = document.querySelector("h2");
if (button !== null && h2 !== null) { button.addEventListener("click", e => { console.log(`Button clicked, ${e.button.toString()}`); let text = h2.textContent; if (text === null) { h2.textContent = "1"; return; } let number = Stdlib_Int.fromString(text); if (number !== undefined) { h2.textContent = (number + 1 | 0).toString(); } else { h2.textContent = "1"; } });} else { console.log("Stuff not found");}
let button$1 = button === null ? undefined : Primitive_option.some(button);
let h2$1 = h2 === null ? undefined : Primitive_option.some(h2);
export { button$1 as button, h2$1 as h2,}/* button Not a pure module */AudioDestinationNode__.test
let ctx = AudioContext.make()
let destinationNode = ctx.destination->AudioDestinationNode.asAudioNodelet context = AudioContext.asBaseAudioContext(ctx)
let osc = OscillatorNode.make( ~context, ~options={ type_: WebAudioTypes.Sine, frequency: 440.0, },)
let gain = GainNode.make( ~context, ~options={ gain: 0.3, },)
let _ = gain->GainNode.connect(~destinationNode)
let _ = osc ->OscillatorNode.asAudioScheduledSourceNode ->AudioScheduledSourceNode.connect(~destinationNode=gain->GainNode.asAudioNode)
osc->OscillatorNode.startlet ctx = new AudioContext();
let destinationNode = ctx.destination;
let osc = new OscillatorNode(ctx, { type: "sine", frequency: 440.0});
let gain = new GainNode(ctx, { gain: 0.3});
gain.connect(destinationNode);
osc.connect(gain);
osc.start();
let context = ctx;
export { ctx, destinationNode, context, osc, gain,}/* ctx Not a pure module */