WebXR-emulator-extension icon indicating copy to clipboard operation
WebXR-emulator-extension copied to clipboard

Asynchronous WebXR API polyfill load on demand

Open takahirox opened this issue 5 years ago • 4 comments

Currently the extension includes WebXR API polyfill. So WebXR API is ready when the page is loaded. Two problems. Even on non-WebXR application page it consumes memory for the polyfill and the page boot up can be a bit slower.

Asynchronously loading WebXR polyfill on demand maybe can resolve these problems. Probably navigator.xr.isSessionSupported() and/or navigator.xr.requestSession() are good points to trigger the polyfill load.

navigator.xr = {
  isSessionSupported: function(mode) {
    return new Promise((resolve, reject) => {
      delete navigator.xr;
      loadWebXRPolyfill().then(() => {
        navigator.xr.isSessionSupported(mode).then(resolve).catch(reject);      
      });
    });
  },
  requestSession: function(mode, options) {
    return new Promise((resolve, reject) => {
      delete navigator.xr;
      loadWebXRPolyfill().then(() => {
        navigator.xr.requestSession(mode, options).then(resolve).catch(reject);      
      });
    });
  }
};

// user code
navigator.xr.isSessionSupported('immersive-vr').then(supported => { // webxr polyfill is loaded inside
  if (!supported) return;
  navigator.xr.requestSession('immersive-vr').then(session => {
    ...
  });
});

Cons is XR* classes aren't declared until the polyfill is loaded. So for example the following code ends up judging the browser doesn't support WebXR API and stopping the application. I guess this type of accessing XR* before calling navigator.xr.isSessionSupported/requestSession() would be rare so it'd be acceptable limitation?

(There might be other cons I'm not aware of yet.)

const supportsWebXR = (window.XR !== undefined);
if (supportsWebXR) {
  navigator.xr.isSessionSupported('immersive-vr').then(supported => {
    if (!supported) return;
    navigator.xr.requestSession('immersive-vr').then(session => {
      ...
    });
  });
} else {
  console.error('Your browser doesn\'t seem to support WebXR');
}

Inspired by https://github.com/MozillaReality/webxr-ios-js/pull/34

takahirox avatar Oct 25 '19 18:10 takahirox

Do you think this should be for version 0.2.0 or 0.3.0?

joshmarinacci avatar Oct 29 '19 17:10 joshmarinacci

Hard to say now. Please give me time to experiment more. There are Pros and Cons. And there may be a chance that loading the external polyfill file on demand may fail on some platforms or pages due to security stuff.

At least I want to prioritize full WebXR API support and UI polish rather than on demand polyfill load for v0.2.0 or v0.3.0.

takahirox avatar Oct 29 '19 17:10 takahirox

k. moved.

joshmarinacci avatar Oct 29 '19 17:10 joshmarinacci

Let me move this to v4 because I want to focus on AR in v3. And I'm still not sure yet if this approach is good.

takahirox avatar Feb 04 '20 22:02 takahirox