@webority-technologies/mobile-core 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -1
- package/lib/commonjs/api/baseUrl.js +34 -0
- package/lib/commonjs/api/curl.js +109 -0
- package/lib/commonjs/api/publicClient.js +11 -20
- package/lib/commonjs/api/retry.js +75 -0
- package/lib/commonjs/api/userClient.js +46 -22
- package/lib/commonjs/auth/authStore.js +101 -33
- package/lib/commonjs/auth/jwt.js +32 -8
- package/lib/commonjs/config/index.js +2 -1
- package/lib/commonjs/deepLink/index.js +286 -0
- package/lib/commonjs/download/adapters/expoFs.js +100 -0
- package/lib/commonjs/download/adapters/shared.js +13 -0
- package/lib/commonjs/download/index.js +272 -0
- package/lib/commonjs/formatters/date.js +1 -1
- package/lib/commonjs/formatters/index.js +6 -0
- package/lib/commonjs/formatters/phone.js +103 -15
- package/lib/commonjs/geolocation/adapters/expoLocation.js +126 -0
- package/lib/commonjs/geolocation/index.js +339 -0
- package/lib/commonjs/index.js +133 -48
- package/lib/commonjs/initSDK.js +23 -2
- package/lib/commonjs/initUserAuth.js +2 -1
- package/lib/commonjs/logger/index.js +159 -5
- package/lib/commonjs/network/index.js +6 -0
- package/lib/commonjs/network/networkStatus.js +50 -15
- package/lib/commonjs/permissions/index.js +24 -1
- package/lib/commonjs/sqlite/adapters/expo.js +53 -0
- package/lib/commonjs/sqlite/adapters/op.js +43 -0
- package/lib/commonjs/sqlite/adapters/rows.js +33 -0
- package/lib/commonjs/sqlite/adapters/storage.js +50 -0
- package/lib/commonjs/sqlite/adapters/sync.js +45 -0
- package/lib/commonjs/sqlite/index.js +610 -0
- package/lib/commonjs/sqlite/nitro.js +17 -0
- package/lib/commonjs/sqlite/op.js +18 -0
- package/lib/commonjs/sqlite/quick.js +14 -0
- package/lib/commonjs/sqlite/storage.js +14 -0
- package/lib/commonjs/storage/index.js +40 -19
- package/lib/commonjs/types/globals.d.js +6 -0
- package/lib/commonjs/utils/imageCompression.js +51 -14
- package/lib/commonjs/utils/index.js +6 -0
- package/lib/commonjs/versionCheck/index.js +28 -4
- package/lib/module/api/baseUrl.js +30 -0
- package/lib/module/api/curl.js +104 -0
- package/lib/module/api/publicClient.js +11 -20
- package/lib/module/api/retry.js +69 -0
- package/lib/module/api/userClient.js +45 -21
- package/lib/module/auth/authStore.js +99 -32
- package/lib/module/auth/jwt.js +32 -8
- package/lib/module/config/index.js +2 -1
- package/lib/module/deepLink/index.js +280 -0
- package/lib/module/download/adapters/expoFs.js +95 -0
- package/lib/module/download/adapters/shared.js +8 -0
- package/lib/module/download/index.js +264 -0
- package/lib/module/formatters/date.js +1 -1
- package/lib/module/formatters/index.js +1 -1
- package/lib/module/formatters/phone.js +99 -13
- package/lib/module/geolocation/adapters/expoLocation.js +121 -0
- package/lib/module/geolocation/index.js +332 -0
- package/lib/module/index.js +28 -11
- package/lib/module/initSDK.js +23 -2
- package/lib/module/initUserAuth.js +2 -1
- package/lib/module/logger/index.js +156 -4
- package/lib/module/network/index.js +1 -1
- package/lib/module/network/networkStatus.js +47 -14
- package/lib/module/permissions/index.js +22 -0
- package/lib/module/sqlite/adapters/expo.js +48 -0
- package/lib/module/sqlite/adapters/op.js +38 -0
- package/lib/module/sqlite/adapters/rows.js +28 -0
- package/lib/module/sqlite/adapters/storage.js +46 -0
- package/lib/module/sqlite/adapters/sync.js +41 -0
- package/lib/module/sqlite/index.js +600 -0
- package/lib/module/sqlite/nitro.js +12 -0
- package/lib/module/sqlite/op.js +13 -0
- package/lib/module/sqlite/quick.js +9 -0
- package/lib/module/sqlite/storage.js +9 -0
- package/lib/module/storage/index.js +37 -18
- package/lib/module/types/globals.d.js +10 -0
- package/lib/module/utils/imageCompression.js +49 -13
- package/lib/module/utils/index.js +1 -1
- package/lib/module/versionCheck/index.js +27 -4
- package/lib/typescript/commonjs/api/baseUrl.d.ts +20 -0
- package/lib/typescript/commonjs/api/curl.d.ts +18 -0
- package/lib/typescript/commonjs/api/retry.d.ts +27 -0
- package/lib/typescript/commonjs/api/userClient.d.ts +11 -0
- package/lib/typescript/commonjs/auth/authStore.d.ts +29 -6
- package/lib/typescript/commonjs/config/index.d.ts +6 -0
- package/lib/typescript/commonjs/deepLink/index.d.ts +54 -0
- package/lib/typescript/commonjs/download/adapters/expoFs.d.ts +45 -0
- package/lib/typescript/commonjs/download/adapters/shared.d.ts +3 -0
- package/lib/typescript/commonjs/download/index.d.ts +76 -0
- package/lib/typescript/commonjs/formatters/index.d.ts +2 -1
- package/lib/typescript/commonjs/formatters/phone.d.ts +16 -1
- package/lib/typescript/commonjs/geolocation/adapters/expoLocation.d.ts +42 -0
- package/lib/typescript/commonjs/geolocation/index.d.ts +89 -0
- package/lib/typescript/commonjs/index.d.ts +30 -13
- package/lib/typescript/commonjs/initSDK.d.ts +32 -0
- package/lib/typescript/commonjs/logger/index.d.ts +39 -0
- package/lib/typescript/commonjs/network/index.d.ts +2 -2
- package/lib/typescript/commonjs/network/networkStatus.d.ts +16 -0
- package/lib/typescript/commonjs/permissions/index.d.ts +35 -0
- package/lib/typescript/commonjs/sqlite/adapters/expo.d.ts +30 -0
- package/lib/typescript/commonjs/sqlite/adapters/op.d.ts +27 -0
- package/lib/typescript/commonjs/sqlite/adapters/rows.d.ts +7 -0
- package/lib/typescript/commonjs/sqlite/adapters/storage.d.ts +30 -0
- package/lib/typescript/commonjs/sqlite/adapters/sync.d.ts +31 -0
- package/lib/typescript/commonjs/sqlite/index.d.ts +121 -0
- package/lib/typescript/commonjs/sqlite/nitro.d.ts +7 -0
- package/lib/typescript/commonjs/sqlite/op.d.ts +8 -0
- package/lib/typescript/commonjs/sqlite/quick.d.ts +4 -0
- package/lib/typescript/commonjs/sqlite/storage.d.ts +4 -0
- package/lib/typescript/commonjs/storage/index.d.ts +24 -2
- package/lib/typescript/commonjs/utils/imageCompression.d.ts +17 -0
- package/lib/typescript/commonjs/utils/index.d.ts +2 -2
- package/lib/typescript/module/api/baseUrl.d.ts +20 -0
- package/lib/typescript/module/api/curl.d.ts +18 -0
- package/lib/typescript/module/api/retry.d.ts +27 -0
- package/lib/typescript/module/api/userClient.d.ts +11 -0
- package/lib/typescript/module/auth/authStore.d.ts +29 -6
- package/lib/typescript/module/config/index.d.ts +6 -0
- package/lib/typescript/module/deepLink/index.d.ts +54 -0
- package/lib/typescript/module/download/adapters/expoFs.d.ts +45 -0
- package/lib/typescript/module/download/adapters/shared.d.ts +3 -0
- package/lib/typescript/module/download/index.d.ts +76 -0
- package/lib/typescript/module/formatters/index.d.ts +2 -1
- package/lib/typescript/module/formatters/phone.d.ts +16 -1
- package/lib/typescript/module/geolocation/adapters/expoLocation.d.ts +42 -0
- package/lib/typescript/module/geolocation/index.d.ts +89 -0
- package/lib/typescript/module/index.d.ts +30 -13
- package/lib/typescript/module/initSDK.d.ts +32 -0
- package/lib/typescript/module/logger/index.d.ts +39 -0
- package/lib/typescript/module/network/index.d.ts +2 -2
- package/lib/typescript/module/network/networkStatus.d.ts +16 -0
- package/lib/typescript/module/permissions/index.d.ts +35 -0
- package/lib/typescript/module/sqlite/adapters/expo.d.ts +30 -0
- package/lib/typescript/module/sqlite/adapters/op.d.ts +27 -0
- package/lib/typescript/module/sqlite/adapters/rows.d.ts +7 -0
- package/lib/typescript/module/sqlite/adapters/storage.d.ts +30 -0
- package/lib/typescript/module/sqlite/adapters/sync.d.ts +31 -0
- package/lib/typescript/module/sqlite/index.d.ts +121 -0
- package/lib/typescript/module/sqlite/nitro.d.ts +7 -0
- package/lib/typescript/module/sqlite/op.d.ts +8 -0
- package/lib/typescript/module/sqlite/quick.d.ts +4 -0
- package/lib/typescript/module/sqlite/storage.d.ts +4 -0
- package/lib/typescript/module/storage/index.d.ts +24 -2
- package/lib/typescript/module/utils/imageCompression.d.ts +17 -0
- package/lib/typescript/module/utils/index.d.ts +2 -2
- package/package.json +102 -14
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.useCurrentPosition = exports.setGeolocationImplementation = exports.GeolocationError = exports.Geolocation = void 0;
|
|
7
|
+
var ExpoLocation = _interopRequireWildcard(require("expo-location"));
|
|
8
|
+
var _react = require("react");
|
|
9
|
+
var _index = require("../logger/index.js");
|
|
10
|
+
var _index2 = require("../permissions/index.js");
|
|
11
|
+
var _expoLocation2 = require("./adapters/expoLocation.js");
|
|
12
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
13
|
+
const PREFIX = '[@webority-technologies/mobile-core]';
|
|
14
|
+
class GeolocationError extends Error {
|
|
15
|
+
constructor(code, message) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = 'GeolocationError';
|
|
18
|
+
this.code = code;
|
|
19
|
+
// Babel downlevels `class extends Error`, which breaks `instanceof` for
|
|
20
|
+
// subclasses unless the prototype is restored explicitly.
|
|
21
|
+
Object.setPrototypeOf(this, GeolocationError.prototype);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Minimal shape of the native geolocation module we depend on. */
|
|
26
|
+
exports.GeolocationError = GeolocationError;
|
|
27
|
+
const DEFAULT_TIMEOUT_MS = 15000;
|
|
28
|
+
const DEFAULT_RETRIES = 1;
|
|
29
|
+
const DEFAULT_RETRY_DELAY_MS = 800;
|
|
30
|
+
const isGeolocationLike = value => {
|
|
31
|
+
const candidate = value;
|
|
32
|
+
return typeof candidate?.getCurrentPosition === 'function' && typeof candidate?.watchPosition === 'function' && typeof candidate?.clearWatch === 'function';
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** Only what a test or the showcase injected; null means use expo-location. */
|
|
36
|
+
let backend = null;
|
|
37
|
+
let expoBackend;
|
|
38
|
+
const resolveBackend = () => {
|
|
39
|
+
if (backend) {
|
|
40
|
+
return backend;
|
|
41
|
+
}
|
|
42
|
+
if (!expoBackend) {
|
|
43
|
+
expoBackend = (0, _expoLocation2.adaptExpoLocation)(ExpoLocation);
|
|
44
|
+
}
|
|
45
|
+
return expoBackend;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Replace the geolocation provider. Intended for TESTS and for the showcase,
|
|
50
|
+
* which injects a denying provider to demo the permission and services-off
|
|
51
|
+
* errors; an app gets expo-location without wiring anything. Pass `null` to
|
|
52
|
+
* restore it.
|
|
53
|
+
*/
|
|
54
|
+
const setGeolocationImplementation = impl => {
|
|
55
|
+
backend = impl;
|
|
56
|
+
};
|
|
57
|
+
exports.setGeolocationImplementation = setGeolocationImplementation;
|
|
58
|
+
const requireBackend = op => {
|
|
59
|
+
const b = resolveBackend();
|
|
60
|
+
if (!b) {
|
|
61
|
+
throw new GeolocationError('unsupported', `${PREFIX} No geolocation provider is available; cannot ${op}. ` + 'expo-location did not load, which on a real build means the native ' + 'module was not linked.');
|
|
62
|
+
}
|
|
63
|
+
return b;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Android reports "location services are switched off" through the same code 2
|
|
68
|
+
* that also covers a genuine failure to fix a position, and the two need
|
|
69
|
+
* different handling: one is fixed by the user in system settings, the other is
|
|
70
|
+
* worth retrying. The message is the only signal that separates them.
|
|
71
|
+
*/
|
|
72
|
+
const SERVICES_OFF_PATTERN = /(location|gps)[^.]{0,40}(disabled|switched off|turned off|not enabled)|no location provider|settings_not_satisfied|location services (are |is )?off/i;
|
|
73
|
+
const toGeoError = raw => {
|
|
74
|
+
if (raw instanceof GeolocationError) {
|
|
75
|
+
return raw;
|
|
76
|
+
}
|
|
77
|
+
const native = raw ?? {};
|
|
78
|
+
const detail = typeof native.message === 'string' && native.message.length > 0 ? native.message : 'the provider gave no reason';
|
|
79
|
+
switch (native.code) {
|
|
80
|
+
case 1:
|
|
81
|
+
return new GeolocationError('permission-denied', `${PREFIX} Location permission was denied (${detail}). Request the ` + 'location permission before calling, or send the user to settings.');
|
|
82
|
+
case 2:
|
|
83
|
+
return SERVICES_OFF_PATTERN.test(detail) ? new GeolocationError('services-disabled', `${PREFIX} Location services are switched off (${detail}). Ask the user ` + 'to enable location in system settings; retrying will not help.') : new GeolocationError('position-unavailable', `${PREFIX} No position fix (${detail}).`);
|
|
84
|
+
case 3:
|
|
85
|
+
return new GeolocationError('timeout', `${PREFIX} Location request timed out (${detail}).`);
|
|
86
|
+
case 4:
|
|
87
|
+
case 5:
|
|
88
|
+
return new GeolocationError('services-disabled', `${PREFIX} Location services are unavailable on this device (${detail}). ` + 'Ask the user to enable location, or install Google Play services.');
|
|
89
|
+
default:
|
|
90
|
+
return new GeolocationError('position-unavailable', `${PREFIX} Location request failed (${detail}).`);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const RETRYABLE = ['timeout', 'position-unavailable'];
|
|
94
|
+
const normalizeCoords = raw => {
|
|
95
|
+
const coords = raw?.coords;
|
|
96
|
+
if (!coords || !Number.isFinite(coords.latitude) || !Number.isFinite(coords.longitude)) {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
latitude: coords.latitude,
|
|
101
|
+
longitude: coords.longitude,
|
|
102
|
+
// An absent radius is treated as infinitely bad rather than perfect, so a
|
|
103
|
+
// provider that omits it can never satisfy `minimumAccuracyM` by accident.
|
|
104
|
+
accuracy: Number.isFinite(coords.accuracy) ? coords.accuracy : Number.POSITIVE_INFINITY,
|
|
105
|
+
altitude: Number.isFinite(coords.altitude) ? coords.altitude : null,
|
|
106
|
+
heading: Number.isFinite(coords.heading) ? coords.heading : null,
|
|
107
|
+
speed: Number.isFinite(coords.speed) ? coords.speed : null
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
const normalizePosition = raw => {
|
|
111
|
+
const coords = normalizeCoords(raw);
|
|
112
|
+
if (!coords) {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
coords,
|
|
117
|
+
timestamp: Number.isFinite(raw?.timestamp) ? raw?.timestamp : Date.now()
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
const toNativeOptions = options => {
|
|
121
|
+
const native = {
|
|
122
|
+
enableHighAccuracy: (options.accuracy ?? 'fine') === 'fine',
|
|
123
|
+
timeout: options.timeoutMs ?? DEFAULT_TIMEOUT_MS,
|
|
124
|
+
maximumAge: options.maximumAgeMs ?? 0
|
|
125
|
+
};
|
|
126
|
+
if (options.distanceFilterM !== undefined) {
|
|
127
|
+
native.distanceFilter = options.distanceFilterM;
|
|
128
|
+
}
|
|
129
|
+
return native;
|
|
130
|
+
};
|
|
131
|
+
const sleep = ms => new Promise(resolve => {
|
|
132
|
+
setTimeout(resolve, ms);
|
|
133
|
+
});
|
|
134
|
+
const ensureLocationPermission = async () => {
|
|
135
|
+
const status = await _index2.Permissions.ensure('location');
|
|
136
|
+
if (status === 'granted' || status === 'limited') {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (status === 'unavailable') {
|
|
140
|
+
_index.Logger.warn(`${PREFIX} Location permission status is unavailable (react-native-permissions is not ` + 'installed, or this platform has no mapping); attempting the location request anyway.');
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
throw new GeolocationError('permission-denied', `${PREFIX} Location permission is "${status}". Grant it before requesting a position` + (status === 'blocked' ? ', or call Permissions.openSettings() to let the user fix it.' : '.'));
|
|
144
|
+
};
|
|
145
|
+
const positionOnce = (b, options) => {
|
|
146
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
147
|
+
return new Promise((resolve, reject) => {
|
|
148
|
+
let settled = false;
|
|
149
|
+
// The native timeout is passed through as well, but a provider that never
|
|
150
|
+
// calls back at all would otherwise hang the caller forever.
|
|
151
|
+
const timer = setTimeout(() => {
|
|
152
|
+
if (settled) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
settled = true;
|
|
156
|
+
reject(new GeolocationError('timeout', `${PREFIX} Location request timed out after ${timeoutMs}ms. Raise timeoutMs, or use ` + "accuracy: 'coarse' where a network fix is good enough."));
|
|
157
|
+
}, timeoutMs);
|
|
158
|
+
const settle = run => {
|
|
159
|
+
if (settled) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
settled = true;
|
|
163
|
+
clearTimeout(timer);
|
|
164
|
+
run();
|
|
165
|
+
};
|
|
166
|
+
const onSuccess = raw => settle(() => {
|
|
167
|
+
const position = normalizePosition(raw);
|
|
168
|
+
if (!position) {
|
|
169
|
+
reject(new GeolocationError('position-unavailable', `${PREFIX} The provider returned a fix without usable coordinates.`));
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
const floor = options.minimumAccuracyM;
|
|
173
|
+
if (floor !== undefined && position.coords.accuracy > floor) {
|
|
174
|
+
reject(new GeolocationError('position-unavailable', `${PREFIX} Fix accuracy ${position.coords.accuracy}m is worse than the required ` + `${floor}m.`));
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
resolve(position);
|
|
178
|
+
});
|
|
179
|
+
const onError = error => settle(() => reject(toGeoError(error)));
|
|
180
|
+
try {
|
|
181
|
+
b.getCurrentPosition(onSuccess, onError, toNativeOptions(options));
|
|
182
|
+
} catch (error) {
|
|
183
|
+
settle(() => reject(toGeoError(error)));
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
};
|
|
187
|
+
const getCurrentPosition = async (options = {}) => {
|
|
188
|
+
const b = requireBackend('get the current position');
|
|
189
|
+
await ensureLocationPermission();
|
|
190
|
+
const attempts = Math.max(0, Math.trunc(options.retries ?? DEFAULT_RETRIES)) + 1;
|
|
191
|
+
const retryDelayMs = options.retryDelayMs ?? DEFAULT_RETRY_DELAY_MS;
|
|
192
|
+
let lastError = null;
|
|
193
|
+
for (let attempt = 0; attempt < attempts; attempt += 1) {
|
|
194
|
+
try {
|
|
195
|
+
return await positionOnce(b, options);
|
|
196
|
+
} catch (error) {
|
|
197
|
+
const geoError = toGeoError(error);
|
|
198
|
+
if (!RETRYABLE.includes(geoError.code)) {
|
|
199
|
+
throw geoError;
|
|
200
|
+
}
|
|
201
|
+
lastError = geoError;
|
|
202
|
+
if (attempt < attempts - 1 && retryDelayMs > 0) {
|
|
203
|
+
await sleep(retryDelayMs);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
throw lastError ?? new GeolocationError('position-unavailable', `${PREFIX} Location request failed.`);
|
|
208
|
+
};
|
|
209
|
+
const watchPosition = (listener, options = {}) => {
|
|
210
|
+
const b = requireBackend('watch the position');
|
|
211
|
+
let cancelled = false;
|
|
212
|
+
let watchId = null;
|
|
213
|
+
const stop = () => {
|
|
214
|
+
if (cancelled) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
cancelled = true;
|
|
218
|
+
if (watchId !== null) {
|
|
219
|
+
b.clearWatch(watchId);
|
|
220
|
+
watchId = null;
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
const fail = error => {
|
|
224
|
+
const geoError = toGeoError(error);
|
|
225
|
+
_index.Logger.error(geoError.message);
|
|
226
|
+
options.onError?.(geoError);
|
|
227
|
+
};
|
|
228
|
+
void ensureLocationPermission().then(() => {
|
|
229
|
+
// The caller can unsubscribe while the permission gate is still pending;
|
|
230
|
+
// registering afterwards would leak a watch nothing can clear.
|
|
231
|
+
if (cancelled) {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
watchId = b.watchPosition(raw => {
|
|
235
|
+
if (cancelled) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
const position = normalizePosition(raw);
|
|
239
|
+
if (!position) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
const floor = options.minimumAccuracyM;
|
|
243
|
+
if (floor !== undefined && position.coords.accuracy > floor) {
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
listener(position);
|
|
247
|
+
}, error => {
|
|
248
|
+
if (!cancelled) {
|
|
249
|
+
fail(error);
|
|
250
|
+
}
|
|
251
|
+
}, toNativeOptions(options));
|
|
252
|
+
}).catch(fail);
|
|
253
|
+
return stop;
|
|
254
|
+
};
|
|
255
|
+
const isAvailable = () => resolveBackend() !== null;
|
|
256
|
+
const Geolocation = exports.Geolocation = {
|
|
257
|
+
getCurrentPosition,
|
|
258
|
+
watchPosition,
|
|
259
|
+
isAvailable
|
|
260
|
+
};
|
|
261
|
+
const useCurrentPosition = (options = {}) => {
|
|
262
|
+
const {
|
|
263
|
+
enabled = true,
|
|
264
|
+
accuracy,
|
|
265
|
+
timeoutMs,
|
|
266
|
+
maximumAgeMs,
|
|
267
|
+
retries,
|
|
268
|
+
minimumAccuracyM,
|
|
269
|
+
retryDelayMs
|
|
270
|
+
} = options;
|
|
271
|
+
const [state, setState] = (0, _react.useState)({
|
|
272
|
+
position: null,
|
|
273
|
+
error: null,
|
|
274
|
+
loading: enabled
|
|
275
|
+
});
|
|
276
|
+
const mountedRef = (0, _react.useRef)(true);
|
|
277
|
+
// Identifies the in-flight request so a slow earlier fix cannot overwrite the
|
|
278
|
+
// result of a later refresh().
|
|
279
|
+
const requestRef = (0, _react.useRef)(0);
|
|
280
|
+
(0, _react.useEffect)(() => {
|
|
281
|
+
mountedRef.current = true;
|
|
282
|
+
return () => {
|
|
283
|
+
mountedRef.current = false;
|
|
284
|
+
};
|
|
285
|
+
}, []);
|
|
286
|
+
|
|
287
|
+
// Every dependency is a primitive read off `options`, so an inline options
|
|
288
|
+
// object (the normal call shape) does not re-create this on every render.
|
|
289
|
+
const run = (0, _react.useCallback)(() => {
|
|
290
|
+
requestRef.current += 1;
|
|
291
|
+
const token = requestRef.current;
|
|
292
|
+
setState(prev => prev.loading ? prev : {
|
|
293
|
+
...prev,
|
|
294
|
+
loading: true
|
|
295
|
+
});
|
|
296
|
+
const accept = next => {
|
|
297
|
+
if (mountedRef.current && requestRef.current === token) {
|
|
298
|
+
setState(next);
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
getCurrentPosition({
|
|
302
|
+
accuracy,
|
|
303
|
+
timeoutMs,
|
|
304
|
+
maximumAgeMs,
|
|
305
|
+
retries,
|
|
306
|
+
minimumAccuracyM,
|
|
307
|
+
retryDelayMs
|
|
308
|
+
}).then(position => accept({
|
|
309
|
+
position,
|
|
310
|
+
error: null,
|
|
311
|
+
loading: false
|
|
312
|
+
}), error => accept({
|
|
313
|
+
position: null,
|
|
314
|
+
error: toGeoError(error),
|
|
315
|
+
loading: false
|
|
316
|
+
}));
|
|
317
|
+
}, [accuracy, timeoutMs, maximumAgeMs, retries, minimumAccuracyM, retryDelayMs]);
|
|
318
|
+
(0, _react.useEffect)(() => {
|
|
319
|
+
if (!enabled) {
|
|
320
|
+
// Discard any in-flight request so its result cannot land after opting out.
|
|
321
|
+
requestRef.current += 1;
|
|
322
|
+
setState(prev => prev.loading || prev.position !== null || prev.error !== null ? {
|
|
323
|
+
position: null,
|
|
324
|
+
error: null,
|
|
325
|
+
loading: false
|
|
326
|
+
} : prev);
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
run();
|
|
330
|
+
}, [enabled, run]);
|
|
331
|
+
return {
|
|
332
|
+
position: state.position,
|
|
333
|
+
error: state.error,
|
|
334
|
+
loading: state.loading,
|
|
335
|
+
refresh: run
|
|
336
|
+
};
|
|
337
|
+
};
|
|
338
|
+
exports.useCurrentPosition = useCurrentPosition;
|
|
339
|
+
//# sourceMappingURL=index.js.map
|