UNPKG

@dailyshot/hooks

Version:

A set of hooks used in Dailyshot packages

69 lines (64 loc) 2.26 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var React = require('react'); var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); const useGeolocation = (options) => { const [state, setState] = React.useState({ loading: true, accuracy: null, altitude: null, altitudeAccuracy: null, heading: null, latitude: null, longitude: null, speed: null, timestamp: Date.now() }); let mounted = true; let watchId; const onEvent = (event) => { if (mounted) { setState({ loading: false, accuracy: event.coords.accuracy, altitude: event.coords.altitude, altitudeAccuracy: event.coords.altitudeAccuracy, heading: event.coords.heading, latitude: event.coords.latitude, longitude: event.coords.longitude, speed: event.coords.speed, timestamp: event.timestamp }); } }; const onEventError = (error) => mounted && setState((oldState) => __spreadProps(__spreadValues({}, oldState), { loading: false, error })); React.useEffect(() => { navigator.geolocation.getCurrentPosition(onEvent, onEventError, options); watchId = navigator.geolocation.watchPosition(onEvent, onEventError, options); return () => { mounted = false; navigator.geolocation.clearWatch(watchId); }; }, []); return state; }; exports.useGeolocation = useGeolocation; //# sourceMappingURL=use-geolocation.js.map