UNPKG

@stimulus-library/mixins

Version:

A library of useful controllers for Stimulus

56 lines (55 loc) 1.67 kB
import { reactive } from "@stimulus-library/utilities"; import { useMixin } from "./use_mixin"; export function useGeolocation(controller, options = {}, update, error) { if (update) { update = update.bind(controller); } if (error) { error = error.bind(controller); } const { enableHighAccuracy = true, maximumAge = 30000, timeout = 27000, } = options; const isSupported = navigator && "geolocation" in navigator; const values = reactive({ locatedAt: null, error: null, coords: { accuracy: 0, latitude: Infinity, longitude: Infinity, altitude: null, altitudeAccuracy: null, heading: null, speed: null, }, teardown: () => { if (watcher) { navigator.geolocation.clearWatch(watcher); watcher = null; } }, }); const setup = () => { if (isSupported) { watcher = navigator.geolocation.watchPosition((position) => { values.locatedAt = position.timestamp; values.coords = position.coords; values.error = null; if (update) { update(position); } }, (err) => { values.error = err; if (error) { error(err); } }, { enableHighAccuracy, maximumAge, timeout, }); } }; let watcher = null; useMixin(controller, setup, values.teardown); return values; }