UNPKG

5.49 kBSource Map (JSON)View Raw
1{"version":3,"file":"isSensorEnabledAsync.web.js","sourceRoot":"","sources":["../../src/utils/isSensorEnabledAsync.web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAI5C,MAAM,UAAU,oBAAoB;IAClC,IAAI,OAAO,iBAAiB,KAAK,WAAW,IAAI,CAAC,CAAC,iBAAiB,EAAE,iBAAiB,EAAE;QACtF,OAAO,iBAAiB,CAAC,iBAAiB,CAAC;KAC5C;SAAM,IACL,OAAO,sBAAsB,KAAK,WAAW;QAC7C,CAAC,CAAC,sBAAsB,EAAE,iBAAiB,EAC3C;QACA,OAAO,sBAAsB,CAAC,iBAAiB,CAAC;KACjD;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED,MAAM,eAAgB,SAAQ,KAAK;IACjC,YAAY,SAAiB;QAC3B,IAAI,YAAY,GAAG,yBAAyB,SAAS,eAAe,CAAC;QAErE,YAAY,IAAI,kHAAkH,CAAC;QAEnI,IAAI,QAAQ,CAAC,QAAQ,KAAK,QAAQ,EAAE;YAClC,YAAY;gBACV,yGAAyG,CAAC;SAC7G;QACD,kEAAkE;QAClE,IAAI,KAAK,EAAE,IAAI,CAAC,oBAAoB,EAAE,EAAE;YACtC,YAAY;gBACV,oHAAoH,CAAC;SACxH;QAED,KAAK,CAAC,YAAY,CAAC,CAAC;IACtB,CAAC;CACF;AAED,gDAAgD;AAChD,qDAAqD;AACrD,MAAM,CAAC,KAAK,UAAU,6BAA6B,CACjD,SAA0B,EAC1B,OAAgB;IAEhB,IAAI,CAAC,QAAQ,CAAC,cAAc,EAAE;QAC5B,OAAO,KAAK,CAAC;KACd;IAED,IAAI,oBAAoB,EAAE,EAAE;QAC1B,IAAI,MAAM,oBAAoB,CAAC,SAAS,EAAE,OAAO,CAAC,EAAE;YAClD,OAAO,IAAI,CAAC;SACb;QACD,MAAM,IAAI,eAAe,CAAC,SAAS,CAAC,CAAC;KACt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B,CAAC","sourcesContent":["import { Platform } from '@unimodules/core';\n\ntype SensorEventName = 'deviceorientation' | 'devicemotion';\n\nexport function getRequestPermission(): (() => Promise<PermissionState>) | null {\n if (typeof DeviceMotionEvent !== 'undefined' && !!DeviceMotionEvent?.requestPermission) {\n return DeviceMotionEvent.requestPermission;\n } else if (\n typeof DeviceOrientationEvent !== 'undefined' &&\n !!DeviceOrientationEvent?.requestPermission\n ) {\n return DeviceOrientationEvent.requestPermission;\n }\n\n return null;\n}\n\nclass PermissionError extends Error {\n constructor(eventName: string) {\n let errorMessage = `Cannot observe event: ${eventName}. How to fix:`;\n\n errorMessage += `\\n- Ensure you've requested the \\`MOTION\\` permission via expo-permissions (this must be done in a touch event).`;\n\n if (location.protocol !== 'https:') {\n errorMessage +=\n '\\n- Ensure that you are hosting with `https` as DeviceMotion and DeviceOrientation are now secure APIs.';\n }\n // is iOS and doesn't support requesting permissions, must be 12.2\n if (isIOS() && !getRequestPermission()) {\n errorMessage +=\n '\\n- On iOS 12.2, you must manually enable device orientation in `Settings > Safari > Motion & Orientation Access`.';\n }\n\n super(errorMessage);\n }\n}\n\n// iOS 12.2 disables DeviceMotion by default now\n// https://github.com/w3c/deviceorientation/issues/57\nexport async function assertSensorEventEnabledAsync(\n eventName: SensorEventName,\n timeout?: number\n): Promise<boolean> {\n if (!Platform.isDOMAvailable) {\n return false;\n }\n\n if (getRequestPermission()) {\n if (await isSensorEnabledAsync(eventName, timeout)) {\n return true;\n }\n throw new PermissionError(eventName);\n }\n return true;\n}\n\n// throw error if the sensor is disabled.\nexport async function isSensorEnabledAsync(\n eventName: SensorEventName,\n // Initial interval tests found results on a median of\n // devicemotion:\n // - iPhone 7 Plus: 166.6666753590107ms\n // - iPhone X: 166.6666753590107ms\n // deviceorientation:\n // -\n //\n // The initial launch of iOS Safari onto a page calling this API seems to take a little longer than a regular call.\n // devicemotion:\n // - ~35ms\n // deviceorientation:\n // - ~45ms\n //\n timeout: number = 250\n): Promise<boolean> {\n if (!Platform.isDOMAvailable) {\n return false;\n }\n\n // If there is no method to request permission then the device has access to device motion.\n // Older versions of iOS have no method but still disable motion so we should always check on iOS.\n if (!isIOS && !getRequestPermission()) {\n return true;\n }\n\n return new Promise(resolve => {\n const id = setTimeout(() => {\n window.removeEventListener(eventName, listener);\n resolve(false);\n }, timeout);\n\n const listener = (): void => {\n clearTimeout(id);\n window.removeEventListener(eventName, listener);\n resolve(true);\n };\n\n window.addEventListener(eventName, listener);\n });\n}\n\n// https://stackoverflow.com/a/9039885/4047926\nfunction isIOS(): boolean {\n const isIOSUA = /(iPad|iPhone|iPod)/g.test(navigator.userAgent);\n const isIE11 = !!window['MSStream'];\n return isIOSUA && !isIE11;\n}\n"]}
\No newline at end of file