react-native-nitro-geolocation 0.4.0 → 0.5.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nitro-geolocation",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "⚡🚀Blazing-fast geolocation for React Native powered by Nitro Modules",
5
5
  "main": "src/index",
6
6
  "source": "src/index",
@@ -1,5 +1,7 @@
1
1
  import type { LocationRequestOptions } from "../NitroGeolocation.nitro";
2
2
  import { NitroGeolocationHybridObject } from "../NitroGeolocationModule";
3
+ import { isDevtoolsEnabled } from "../devtools";
4
+ import { getDevtoolsCurrentPosition } from "../devtools/getCurrentPosition";
3
5
  import type { GeolocationResponse } from "../types";
4
6
 
5
7
  /**
@@ -31,5 +33,11 @@ import type { GeolocationResponse } from "../types";
31
33
  export function getCurrentPosition(
32
34
  options?: LocationRequestOptions
33
35
  ): Promise<GeolocationResponse> {
36
+ if (isDevtoolsEnabled()) {
37
+ const devtoolsResult = getDevtoolsCurrentPosition();
38
+ if (devtoolsResult) {
39
+ return devtoolsResult;
40
+ }
41
+ }
34
42
  return NitroGeolocationHybridObject.getCurrentPosition(options);
35
43
  }
@@ -1,4 +1,5 @@
1
1
  import { NitroGeolocationHybridObject } from "../NitroGeolocationModule";
2
+ import { devtoolsUnwatch } from "../devtools/watchPosition";
2
3
 
3
4
  /**
4
5
  * Stop a specific watch subscription.
@@ -14,5 +15,8 @@ import { NitroGeolocationHybridObject } from "../NitroGeolocationModule";
14
15
  * ```
15
16
  */
16
17
  export function unwatch(token: string): void {
18
+ if (devtoolsUnwatch(token)) {
19
+ return;
20
+ }
17
21
  NitroGeolocationHybridObject.unwatch(token);
18
22
  }
@@ -3,6 +3,8 @@ import type {
3
3
  LocationRequestOptions
4
4
  } from "../NitroGeolocation.nitro";
5
5
  import { NitroGeolocationHybridObject } from "../NitroGeolocationModule";
6
+ import { isDevtoolsEnabled } from "../devtools";
7
+ import { devtoolsWatchPosition } from "../devtools/watchPosition";
6
8
  import type { GeolocationResponse } from "../types";
7
9
 
8
10
  /**
@@ -34,5 +36,8 @@ export function watchPosition(
34
36
  error?: (error: LocationError) => void,
35
37
  options?: LocationRequestOptions
36
38
  ): string {
39
+ if (isDevtoolsEnabled()) {
40
+ return devtoolsWatchPosition(success, error);
41
+ }
37
42
  return NitroGeolocationHybridObject.watchPosition(success, error, options);
38
43
  }
@@ -0,0 +1,15 @@
1
+ import type { GeolocationResponse } from "../types";
2
+ import { getDevtoolsState } from "./index";
3
+
4
+ export function getDevtoolsCurrentPosition(): Promise<GeolocationResponse> | null {
5
+ const devtools = getDevtoolsState();
6
+ if (devtools.position) {
7
+ return Promise.resolve(devtools.position);
8
+ }
9
+ // Devtools not connected - throw error
10
+ return Promise.reject(
11
+ new Error(
12
+ "Geolocation devtools not connected. Press 'j' in Metro to open devtools and enable the geolocation plugin."
13
+ )
14
+ );
15
+ }
@@ -0,0 +1,23 @@
1
+ import type { GeolocationResponse } from "../types";
2
+
3
+ declare global {
4
+ var __geolocationDevToolsEnabled: boolean | undefined;
5
+ var __geolocationDevtools: DevtoolsState | undefined;
6
+ }
7
+
8
+ interface DevtoolsState {
9
+ position: GeolocationResponse | null;
10
+ }
11
+
12
+ export function getDevtoolsState(): DevtoolsState {
13
+ if (!globalThis.__geolocationDevtools) {
14
+ globalThis.__geolocationDevtools = {
15
+ position: null
16
+ };
17
+ }
18
+ return globalThis.__geolocationDevtools;
19
+ }
20
+
21
+ export function isDevtoolsEnabled(): boolean {
22
+ return globalThis.__geolocationDevToolsEnabled === true;
23
+ }
@@ -0,0 +1,66 @@
1
+ import type { GeolocationError, GeolocationResponse } from "../types";
2
+ import { getDevtoolsState } from "./index";
3
+
4
+ export function devtoolsWatchPosition(
5
+ success: (position: GeolocationResponse) => void,
6
+ error?: (error: GeolocationError) => void
7
+ ): string {
8
+ const devtools = getDevtoolsState();
9
+
10
+ // Check if devtools has position at all
11
+ if (!devtools.position) {
12
+ // Call error callback immediately if provided
13
+ if (error) {
14
+ error({
15
+ code: 2, // POSITION_UNAVAILABLE
16
+ message:
17
+ "Geolocation devtools not connected. Press 'j' in Metro to open devtools and enable the geolocation plugin.",
18
+ PERMISSION_DENIED: 1,
19
+ POSITION_UNAVAILABLE: 2,
20
+ TIMEOUT: 3
21
+ });
22
+ }
23
+ // Return a dummy token that does nothing
24
+ return `devtools-error-${Date.now()}`;
25
+ }
26
+
27
+ let previousPosition = devtools.position;
28
+
29
+ // Send initial position immediately if available
30
+ success(devtools.position);
31
+
32
+ const interval = setInterval(() => {
33
+ if (devtools.position && devtools.position !== previousPosition) {
34
+ previousPosition = devtools.position;
35
+ success(devtools.position);
36
+ }
37
+ }, 100);
38
+
39
+ // Return a cleanup token
40
+ const token = `devtools-${Date.now()}`;
41
+ (globalThis as any).__devtoolsWatchers =
42
+ (globalThis as any).__devtoolsWatchers || {};
43
+ (globalThis as any).__devtoolsWatchers[token] = interval;
44
+
45
+ return token;
46
+ }
47
+
48
+ export function devtoolsUnwatch(token: string): boolean {
49
+ if (!token.startsWith("devtools-")) {
50
+ return false;
51
+ }
52
+
53
+ // Handle error tokens (no cleanup needed)
54
+ if (token.startsWith("devtools-error-")) {
55
+ return true;
56
+ }
57
+
58
+ const watchers = (globalThis as any).__devtoolsWatchers;
59
+ if (watchers?.[token]) {
60
+ clearInterval(watchers[token]);
61
+ delete watchers[token];
62
+ return true;
63
+ }
64
+
65
+ return false;
66
+ }
@@ -3,7 +3,7 @@ import type {
3
3
  LocationError,
4
4
  LocationRequestOptions
5
5
  } from "../NitroGeolocation.nitro";
6
- import { NitroGeolocationHybridObject } from "../NitroGeolocationModule";
6
+ import { unwatch, watchPosition } from "../api";
7
7
  import type { GeolocationResponse } from "../types";
8
8
 
9
9
  /**
@@ -76,7 +76,7 @@ export function useWatchPosition(options?: UseWatchPositionOptions) {
76
76
  if (!enabled) {
77
77
  // Not enabled, ensure cleanup
78
78
  if (tokenRef.current) {
79
- NitroGeolocationHybridObject.unwatch(tokenRef.current);
79
+ unwatch(tokenRef.current);
80
80
  tokenRef.current = null;
81
81
  }
82
82
  setIsWatching(false);
@@ -87,7 +87,7 @@ export function useWatchPosition(options?: UseWatchPositionOptions) {
87
87
  setIsWatching(true);
88
88
  setError(null);
89
89
 
90
- const token = NitroGeolocationHybridObject.watchPosition(
90
+ const token = watchPosition(
91
91
  (result: GeolocationResponse) => {
92
92
  // Success callback
93
93
  if (!isMountedRef.current) return;
@@ -107,7 +107,7 @@ export function useWatchPosition(options?: UseWatchPositionOptions) {
107
107
  // Cleanup function
108
108
  return () => {
109
109
  if (token) {
110
- NitroGeolocationHybridObject.unwatch(token);
110
+ unwatch(token);
111
111
  }
112
112
  };
113
113
  }, [enabled]); // Only re-subscribe when enabled changes