Skip to content

Commit 8fe3bd4

Browse files
Noemi Rozparamichalchudziak
authored andcommitted
feat: web support
* feat: web support * chore: move bob to dev dependencies * build: rename output to lib * build: include andoid ios and podspec * chore: add lib to flow ignore * feat: change console error to thrown error * fix: flow erors * chore: remove lib from repo * chore: add build to gitignore
1 parent 75a3a67 commit 8fe3bd4

File tree

6 files changed

+372
-177
lines changed

6 files changed

+372
-177
lines changed

.flowconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
; Ignore metro
2020
.*/node_modules/metro/.*
2121

22+
; Ignore the compiled files
23+
lib/.*
24+
2225
[include]
2326

2427
[libs]

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,6 @@ local.properties
4343
buck-out/
4444
\.buckd/
4545
*.keystore
46-
46+
47+
# Build
48+
lib/

js/index.js

Lines changed: 28 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,56 @@
1-
/**
2-
* Copyright (c) Facebook, Inc. and its affiliates.
3-
*
4-
* This source code is licensed under the MIT license found in the
5-
* LICENSE file in the root directory of this source tree.
6-
*
1+
/*
72
* @format
83
* @flow
94
*/
105

11-
import {RNCGeolocation, GeolocationEventEmitter} from './nativeInterface';
12-
13-
import invariant from 'invariant';
14-
import {logError, warning} from './utils';
15-
16-
let subscriptions = [];
17-
let updatesEnabled = false;
18-
19-
type GeoConfiguration = {
20-
skipPermissionRequests: boolean,
21-
authorizationLevel: 'always' | 'whenInUse' | 'auto',
22-
};
23-
246
type GeoOptions = {
25-
timeout?: number,
26-
maximumAge?: number,
27-
enableHighAccuracy?: boolean,
28-
distanceFilter?: number,
29-
useSignificantChanges?: boolean,
7+
timeout: number,
8+
maximumAge: number,
9+
enableHighAccuracy: boolean,
3010
};
3111

32-
/**
33-
* The Geolocation API extends the web spec:
34-
* https://developer.mozilla.org/en-US/docs/Web/API/Geolocation
35-
*
36-
* See https://facebook.github.io/react-native/docs/geolocation.html
37-
*/
3812
const Geolocation = {
39-
/*
40-
* Sets configuration options that will be used in all location requests.
41-
*
42-
* See https://facebook.github.io/react-native/docs/geolocation.html#setrnconfiguration
43-
*
44-
*/
45-
setRNConfiguration: function(config: GeoConfiguration) {
46-
if (RNCGeolocation.setConfiguration) {
47-
RNCGeolocation.setConfiguration(config);
48-
}
13+
setRNConfiguration: function() {
14+
throw new Error('method not supported by the browser');
4915
},
50-
51-
/*
52-
* Requests Location permissions based on the key configured on pList.
53-
*
54-
* See https://facebook.github.io/react-native/docs/geolocation.html#requestauthorization
55-
*/
5616
requestAuthorization: function() {
57-
RNCGeolocation.requestAuthorization();
17+
throw new Error('method not supported by the browser');
5818
},
59-
60-
/*
61-
* Invokes the success callback once with the latest location info.
62-
*
63-
* See https://facebook.github.io/react-native/docs/geolocation.html#getcurrentposition
64-
*/
6519
getCurrentPosition: async function(
66-
geo_success: Function,
67-
geo_error?: Function,
68-
geo_options?: GeoOptions,
20+
success: Function,
21+
error?: Function,
22+
options?: GeoOptions,
6923
) {
70-
invariant(
71-
typeof geo_success === 'function',
72-
'Must provide a valid geo_success callback.',
73-
);
74-
75-
// Permission checks/requests are done on the native side
76-
RNCGeolocation.getCurrentPosition(
77-
geo_options || {},
78-
geo_success,
79-
geo_error || logError,
80-
);
24+
if (typeof success !== 'function') {
25+
throw new Error('success callback must be a function');
26+
} else if (!navigator || !navigator.geolocation) {
27+
console.error('Navigator is undefined');
28+
return;
29+
}
30+
navigator.geolocation.getCurrentPosition(success, error, options);
8131
},
82-
83-
/*
84-
* Invokes the success callback whenever the location changes.
85-
*
86-
* See https://facebook.github.io/react-native/docs/geolocation.html#watchposition
87-
*/
8832
watchPosition: function(
8933
success: Function,
9034
error?: Function,
9135
options?: GeoOptions,
92-
): number {
93-
if (!updatesEnabled) {
94-
RNCGeolocation.startObserving(options || {});
95-
updatesEnabled = true;
36+
) {
37+
if (typeof success !== 'function') {
38+
throw new Error('success callback must be a function');
39+
} else if (!navigator || !navigator.geolocation) {
40+
console.error('Navigator is undefined');
41+
return;
9642
}
97-
const watchID = subscriptions.length;
98-
subscriptions.push([
99-
GeolocationEventEmitter.addListener('geolocationDidChange', success),
100-
error
101-
? GeolocationEventEmitter.addListener('geolocationError', error)
102-
: null,
103-
]);
104-
return watchID;
43+
navigator.geolocation.watchPosition(success, error, options);
10544
},
106-
107-
/*
108-
* Unsubscribes the watcher with the given watchID.
109-
*
110-
* See https://facebook.github.io/react-native/docs/geolocation.html#clearwatch
111-
*/
11245
clearWatch: function(watchID: number) {
113-
const sub = subscriptions[watchID];
114-
if (!sub) {
115-
// Silently exit when the watchID is invalid or already cleared
116-
// This is consistent with timers
46+
if (!navigator || !navigator.geolocation) {
47+
console.error('Navigator is undefined');
11748
return;
11849
}
119-
120-
sub[0].remove();
121-
// array element refinements not yet enabled in Flow
122-
const sub1 = sub[1];
123-
sub1 && sub1.remove();
124-
subscriptions[watchID] = undefined;
125-
let noWatchers = true;
126-
for (let ii = 0; ii < subscriptions.length; ii++) {
127-
if (subscriptions[ii]) {
128-
noWatchers = false; // still valid subscriptions
129-
}
130-
}
131-
if (noWatchers) {
132-
Geolocation.stopObserving();
133-
}
50+
navigator.geolocation.clearWatch(watchID);
13451
},
135-
136-
/*
137-
* Stops observing for device location changes and removes all registered listeners.
138-
*
139-
* See https://facebook.github.io/react-native/docs/geolocation.html#stopobserving
140-
*/
14152
stopObserving: function() {
142-
if (updatesEnabled) {
143-
RNCGeolocation.stopObserving();
144-
updatesEnabled = false;
145-
for (let ii = 0; ii < subscriptions.length; ii++) {
146-
const sub = subscriptions[ii];
147-
if (sub) {
148-
warning(false, 'Called stopObserving with existing subscriptions.');
149-
sub[0].remove();
150-
// array element refinements not yet enabled in Flow
151-
const sub1 = sub[1];
152-
sub1 && sub1.remove();
153-
}
154-
}
155-
subscriptions = [];
156-
}
53+
throw new Error('method not supported by the browser');
15754
},
15855
};
159-
16056
module.exports = Geolocation;

js/index.native.js

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

0 commit comments

Comments
 (0)