UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

49 lines (46 loc) • 1.14 kB
import { assertLightSensorListener, assertUpdateInterval } from "./validate.mjs"; function lightSensorCtor() { if (typeof window === "undefined") return void 0; const ctor = window.AmbientLightSensor; return typeof ctor === "function" ? ctor : void 0; } function isAvailable() { return lightSensorCtor() !== void 0; } function setUpdateInterval(intervalMs) { assertUpdateInterval(intervalMs); } function addListener(listener) { assertLightSensorListener(listener); const Ctor = lightSensorCtor(); if (!Ctor) return { remove: () => {} }; let sensor; const onReading = () => { if (sensor) listener({ illuminance: sensor.illuminance, timestamp: Date.now() / 1e3 }); }; try { sensor = new Ctor(); sensor.addEventListener("reading", onReading); sensor.start(); } catch { sensor = void 0; return { remove: () => {} }; } return { remove: () => { sensor?.removeEventListener("reading", onReading); try { sensor?.stop(); } catch {} sensor = void 0; } }; } const LightSensor = Object.freeze({ isAvailable, setUpdateInterval, addListener }); export { LightSensor }; //# sourceMappingURL=index.mjs.map