From 2c4fdcd8d2546ee2032810275380b43c962f8e30 Mon Sep 17 00:00:00 2001 From: Egor Startsev <78896684+benax-se@users.noreply.github.com> Date: Wed, 9 Nov 2022 02:40:07 +0300 Subject: [PATCH] feat(useGeolocation): pause and resume controls (#2376) Co-authored-by: egstartsev Co-authored-by: BenaXskope <78896684+BenaXskope@users.noreply.github.com> closes https://github.com/vueuse/vueuse/issues/2372 --- packages/core/useGeolocation/demo.vue | 12 +++++++-- packages/core/useGeolocation/index.md | 4 ++- packages/core/useGeolocation/index.ts | 36 ++++++++++++++++++--------- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/packages/core/useGeolocation/demo.vue b/packages/core/useGeolocation/demo.vue index edb7c04ff73..556b132910b 100644 --- a/packages/core/useGeolocation/demo.vue +++ b/packages/core/useGeolocation/demo.vue @@ -1,11 +1,12 @@ diff --git a/packages/core/useGeolocation/index.md b/packages/core/useGeolocation/index.md index 91273c0f767..cf1867c527b 100644 --- a/packages/core/useGeolocation/index.md +++ b/packages/core/useGeolocation/index.md @@ -11,7 +11,7 @@ Reactive [Geolocation API](https://developer.mozilla.org/en-US/docs/Web/API/Geol ```js import { useGeolocation } from '@vueuse/core' -const { coords, locatedAt, error } = useGeolocation() +const { coords, locatedAt, error, resume, pause } = useGeolocation() ``` | State | Type | Description | @@ -19,6 +19,8 @@ const { coords, locatedAt, error } = useGeolocation() | coords | [`Coordinates`](https://developer.mozilla.org/en-US/docs/Web/API/Coordinates) | information about the position retrieved like the latitude and longitude | | locatedAt | `Date` | The time of the last geolocation call | | error | `string` | An error message in case geolocation API fails. | +| resume | `function` | Control function to resume updating geolocation | +| pause | `function` | Control function to pause updating geolocation | ## Config diff --git a/packages/core/useGeolocation/index.ts b/packages/core/useGeolocation/index.ts index b747ea4c1ed..bd8696344f1 100644 --- a/packages/core/useGeolocation/index.ts +++ b/packages/core/useGeolocation/index.ts @@ -7,7 +7,9 @@ import type { ConfigurableNavigator } from '../_configurable' import { defaultNavigator } from '../_configurable' import { useSupported } from '../useSupported' -export interface UseGeolocationOptions extends Partial, ConfigurableNavigator {} +export interface UseGeolocationOptions extends Partial, ConfigurableNavigator { + immediate?: boolean +} /** * Reactive Geolocation API. @@ -45,21 +47,29 @@ export function useGeolocation(options: UseGeolocationOptions = {}) { let watcher: number - if (isSupported.value) { - watcher = navigator!.geolocation.watchPosition( - updatePosition, - err => error.value = err, - { - enableHighAccuracy, - maximumAge, - timeout, - }, - ) + function resume() { + if (isSupported.value) { + watcher = navigator!.geolocation.watchPosition( + updatePosition, + err => error.value = err, + { + enableHighAccuracy, + maximumAge, + timeout, + }, + ) + } } - tryOnScopeDispose(() => { + resume() + + function pause() { if (watcher && navigator) navigator.geolocation.clearWatch(watcher) + } + + tryOnScopeDispose(() => { + pause() }) return { @@ -67,6 +77,8 @@ export function useGeolocation(options: UseGeolocationOptions = {}) { coords, locatedAt, error, + resume, + pause, } }