-
Notifications
You must be signed in to change notification settings - Fork 239
/
Copy pathimplementation.native.ts
153 lines (138 loc) · 4.27 KB
/
implementation.native.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* Copyright (c) React Native Community
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import type { EmitterSubscription } from 'react-native';
import GeolocationNativeInterface from './nativeInterface';
import invariant from 'invariant';
import { logError, warning } from './utils';
import type {
GeolocationOptions,
GeolocationConfiguration,
GeolocationResponse,
GeolocationError,
} from './NativeRNCGeolocation';
const { RNCGeolocation, GeolocationEventEmitter } = GeolocationNativeInterface;
let subscriptions: {
[key: number]: [EmitterSubscription, EmitterSubscription | null];
} = {};
let updatesEnabled = false;
/**
* The Geolocation API extends the web spec:
* https://developer.mozilla.org/en-US/docs/Web/API/Geolocation
*
* See https://facebook.github.io/react-native/docs/geolocation.html
*/
/*
* Sets configuration options that will be used in all location requests.
*
* See https://facebook.github.io/react-native/docs/geolocation.html#setrnconfiguration
*
*/
export function setRNConfiguration(config: GeolocationConfiguration) {
RNCGeolocation.setConfiguration({
...config,
enableBackgroundLocationUpdates:
config?.enableBackgroundLocationUpdates ?? true,
authorizationLevel:
config?.authorizationLevel === 'auto'
? undefined
: config.authorizationLevel,
locationProvider:
config?.locationProvider === 'auto' ? undefined : config.locationProvider,
});
}
/*
* Requests Location permissions based on the key configured on pList.
*
* See https://facebook.github.io/react-native/docs/geolocation.html#requestauthorization
*/
export function requestAuthorization(
success: () => void = () => {},
error: (error: GeolocationError) => void = logError
) {
RNCGeolocation.requestAuthorization(success, error);
}
/*
* Invokes the success callback once with the latest location info.
*
* See https://facebook.github.io/react-native/docs/geolocation.html#getcurrentposition
*/
export async function getCurrentPosition(
success: (position: GeolocationResponse) => void,
error: (error: GeolocationError) => void = logError,
options: GeolocationOptions = {}
) {
invariant(
typeof success === 'function',
'Must provide a valid geo_success callback.'
);
// Permission checks/requests are done on the native side
RNCGeolocation.getCurrentPosition(options, success, error);
}
/*
* Invokes the success callback whenever the location changes.
*
* See https://facebook.github.io/react-native/docs/geolocation.html#watchposition
*/
export function watchPosition(
success: (position: GeolocationResponse) => void,
error: (error: GeolocationError) => void = logError,
options: GeolocationOptions = {}
): number {
if (!updatesEnabled) {
RNCGeolocation.startObserving(options);
updatesEnabled = true;
}
const watchID = Object.keys(subscriptions).length + 1000;
subscriptions[watchID] = [
GeolocationEventEmitter.addListener('geolocationDidChange', success),
error
? GeolocationEventEmitter.addListener('geolocationError', error)
: null,
];
return watchID;
}
/*
* Unsubscribes the watcher with the given watchID.
*
* See https://facebook.github.io/react-native/docs/geolocation.html#clearwatch
*/
export function clearWatch(watchID: number) {
const sub = subscriptions[watchID];
if (!sub) {
// Silently exit when the watchID is invalid or already cleared
// This is consistent with timers
return;
}
sub[0].remove();
// array element refinements not yet enabled in Flow
const sub1 = sub[1];
sub1 && sub1.remove();
delete subscriptions[watchID];
let noWatchers = Object.keys(subscriptions).length === 0;
if (noWatchers) {
stopObserving();
}
}
/*
* Stops observing for device location changes and removes all registered listeners.
*
* See https://facebook.github.io/react-native/docs/geolocation.html#stopobserving
*/
export function stopObserving() {
if (updatesEnabled) {
RNCGeolocation.stopObserving();
updatesEnabled = false;
Object.values(subscriptions).forEach(([sub, sub1]) => {
warning(false, 'Called stopObserving with existing subscriptions.');
sub.remove();
sub1 && sub1.remove();
});
subscriptions = {};
}
}