Skip to content

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.toOption
let 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")
}

AudioDestinationNode__.test

let ctx = AudioContext.make()
let destinationNode = ctx.destination->AudioDestinationNode.asAudioNode
let 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.start

BindingAbsence__test

let requestWithNullBody: Request.requestInit =