Binding `navigator.getVRDisplays()`



I’m trying to bind to navigator.getVRDisplays() in Bucklescript. The problem is while navigator is always there, navigator.getVRDisplays is undefined if VR isn’t supported in the environment. It seems like I need to somehow combine bs.optional and bs.send, but the things I’ve tried don’t seem to work, I only get one or the other.

How should I create this binding?


I have not ever used VR stuff before, but maybe this will help you get started:

type vrDisplay;
exception VRDisplayNotAvailable;

[@bs.val] external getVRDisplays_ : 
  unit => Js.Promise.t(array(vrDisplay)) = "navigator.getVRDisplays";

let getVRDisplays = () => {
  let isAvailable: bool = [%bs.raw 
    "typeof(navigator) !== 'undefined' && navigator.getVRDisplays !== undefined"
  isAvailable ? getVRDisplays_() : Js.Promise.reject(VRDisplayNotAvailable);

  |> then_(displays => {
    displays |. Belt.Array.forEach(Js.log);
  |> catch(err => {
    Js.log2("Failure:", err);