react-native-nitro-geolocation 0.4.0 → 1.0.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/LICENSE CHANGED
@@ -1,24 +1,24 @@
1
- MIT License
1
+ This is free and unencumbered software released into the public domain.
2
2
 
3
- This project is a Nitro Modules port of @react-native-community/geolocation
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
4
7
 
5
- Original work Copyright (c) 2022-present, React Native Community
6
- Modified work Copyright (c) 2025, jingjing2222
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
7
15
 
8
- Permission is hereby granted, free of charge, to any person obtaining a copy
9
- of this software and associated documentation files (the "Software"), to deal
10
- in the Software without restriction, including without limitation the rights
11
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
- copies of the Software, and to permit persons to whom the Software is
13
- furnished to do so, subject to the following conditions:
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
14
23
 
15
- The above copyright notice and this permission notice shall be included in all
16
- copies or substantial portions of the Software.
17
-
18
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
- SOFTWARE.
24
+ For more information, please refer to <http://unlicense.org/>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nitro-geolocation",
3
- "version": "0.4.0",
3
+ "version": "1.0.0",
4
4
  "description": "⚡🚀Blazing-fast geolocation for React Native powered by Nitro Modules",
5
5
  "main": "src/index",
6
6
  "source": "src/index",
@@ -23,7 +23,7 @@
23
23
  "typecheck": "tsc --noEmit"
24
24
  },
25
25
  "author": "jingjing2222",
26
- "license": "MIT",
26
+ "license": "Unlicense",
27
27
  "homepage": "https://react-native-nitro-geolocation.pages.dev",
28
28
  "repository": "https://github.com/jingjing2222/react-native-nitro-geolocation",
29
29
  "files": [
@@ -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