'use strict'; var solidJs = require('solid-js'); var web = require('solid-js/web'); var store = require('solid-js/store'); // src/index.ts exports.createDevices = () => { if (web.isServer) { return () => []; } const [devices, setDevices] = solidJs.createSignal([]); const enumerate = () => { navigator.mediaDevices.enumerateDevices().then(setDevices); }; enumerate(); navigator.mediaDevices.addEventListener("devicechange", enumerate); solidJs.onCleanup(() => navigator.mediaDevices.removeEventListener("devicechange", enumerate)); return devices; }; var equalDeviceLists = (prev, next) => prev.length === next.length && prev.every((device) => next.includes(device)); exports.createMicrophones = () => { if (web.isServer) { return () => []; } const devices = exports.createDevices(); return solidJs.createMemo(() => devices().filter((device) => device.kind === "audioinput"), [], { name: "microphones", equals: equalDeviceLists }); }; exports.createSpeakers = () => { if (web.isServer) { return () => []; } const devices = exports.createDevices(); return solidJs.createMemo(() => devices().filter((device) => device.kind === "audiooutput"), [], { name: "speakers", equals: equalDeviceLists }); }; exports.createCameras = () => { if (web.isServer) { return () => []; } const devices = exports.createDevices(); return solidJs.createMemo(() => devices().filter((device) => device.kind === "videoinput"), [], { name: "cameras", equals: equalDeviceLists }); }; exports.createAccelerometer = (includeGravity = false, interval = 100) => { if (web.isServer) { return () => ({ x: 0, y: 0, z: 0 }); } const [acceleration, setAcceleration] = solidJs.createSignal(); let throttled = false; const accelerationEvent = (e) => { if (throttled) return; throttled = true; setTimeout(() => { throttled = false; }, interval); const acceleration2 = includeGravity ? e.accelerationIncludingGravity : e.acceleration; setAcceleration(acceleration2 ? acceleration2 : void 0); }; addEventListener("devicemotion", accelerationEvent); solidJs.getOwner() && solidJs.onCleanup(() => removeEventListener("devicemotion", accelerationEvent)); return acceleration; }; exports.createGyroscope = (interval = 100) => { if (web.isServer) { return { alpha: 0, beta: 0, gamma: 0 }; } const [orientation, setOrientation] = store.createStore({ alpha: 0, beta: 0, gamma: 0 }); let throttled = false; const orientationEvent = (e) => { if (throttled) return; throttled = true; setTimeout(() => { throttled = false; }, interval); setOrientation({ alpha: e.alpha ? e.alpha : 0, beta: e.beta ? e.beta : 0, gamma: e.gamma ? e.gamma : 0 }); }; addEventListener("deviceorientation", orientationEvent); solidJs.getOwner() && solidJs.onCleanup(() => removeEventListener("deviceorientation", orientationEvent)); return orientation; };