Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 915: Stop/start Location manager based on listeners registered. #999

Merged
merged 16 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
PR review comment addressed
  • Loading branch information
soumyashisPR committed Sep 12, 2020
commit 41f282bdd38a1e4423abb024868521fa47b71616
23 changes: 10 additions & 13 deletions __tests__/components/UserLocation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,12 @@ describe('UserLocation', () => {

test('renders with CircleLayers by default', (done) => {
const {UNSAFE_getAllByType} = render(<UserLocation />);
const shapeSource = UNSAFE_getAllByType(ShapeSource);
const circleLayer = UNSAFE_getAllByType(CircleLayer);

setTimeout(() => {
const shapeSource = UNSAFE_getAllByType(ShapeSource);
const circleLayer = UNSAFE_getAllByType(CircleLayer);

expect(shapeSource.length).toBe(1);
expect(circleLayer.length).toBe(3);
done();
});
expect(shapeSource.length).toBe(1);
expect(circleLayer.length).toBe(3);
done();
});

test('does not render with visible set to false', (done) => {
Expand Down Expand Up @@ -187,7 +184,7 @@ describe('UserLocation', () => {

expect(ul._isLocationManagerRequired).toStrictEqual(true);
expect(locationManager.start).toHaveBeenCalledTimes(1);
expect(locationManager.getLastKnownLocation).toHaveBeenCalledTimes(1);
expect(locationManager.getLastKnownLocation).not.toHaveBeenCalled();
expect(ul.setState).toHaveBeenCalledTimes(1);
expect(ul.setState).toHaveBeenCalledWith({
coordinates: lastKnownLocation,
Expand Down Expand Up @@ -216,29 +213,29 @@ describe('UserLocation', () => {
describe('#isLocationManagerRequired', () => {
test('returns true correctly', () => {
// default props "onUpdate: undefined, visible: true"
expect(ul.isLocationManagerRequired()).toStrictEqual(true);
expect(ul.checkLocationManagerRequired()).toStrictEqual(true);

ul.props = {
onUpdate: () => {},
visible: true,
};

expect(ul.isLocationManagerRequired()).toStrictEqual(true);
expect(ul.checkLocationManagerRequired()).toStrictEqual(true);

ul.props = {
onUpdate: () => {},
visible: false,
};

expect(ul.isLocationManagerRequired()).toStrictEqual(true);
expect(ul.checkLocationManagerRequired()).toStrictEqual(true);
});

test('returns false correctly', () => {
ul.props = {
visible: false,
};

expect(ul.isLocationManagerRequired()).toStrictEqual(false);
expect(ul.checkLocationManagerRequired()).toStrictEqual(false);
});
});

Expand Down
11 changes: 9 additions & 2 deletions __tests__/modules/location/locationManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ describe('LocationManager', () => {
expect(myListener).not.toHaveBeenCalled();
});

test('listener is not called just after being added', () => {
test('calls listener with "lastKnownLocation"', () => {
locationManager._lastKnownLocation = location;

locationManager.addListener(myListener);
expect(locationManager._listeners).toStrictEqual([myListener]);
expect(myListener).not.toHaveBeenCalled();
expect(myListener).toHaveBeenCalledWith(location);
expect(myListener).toHaveBeenCalledTimes(1);
});
});

Expand All @@ -108,16 +109,22 @@ describe('LocationManager', () => {

locationManager.addListener(listenerA);
expect(locationManager._listeners).toStrictEqual([listenerA]);
expect(MapboxGLLocationManager.stop).not.toHaveBeenCalled();

locationManager.addListener(listenerB);
expect(locationManager._listeners).toStrictEqual([
listenerA,
listenerB,
]);
expect(MapboxGLLocationManager.stop).not.toHaveBeenCalled();

locationManager.removeListener(listenerB);
expect(locationManager._listeners).toStrictEqual([listenerA]);
expect(MapboxGLLocationManager.stop).not.toHaveBeenCalled();

locationManager.removeListener(listenerA);
expect(locationManager._listeners).toStrictEqual([]);
expect(MapboxGLLocationManager.stop).toHaveBeenCalledTimes(1);
});
});

Expand Down
12 changes: 5 additions & 7 deletions javascript/components/UserLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class UserLocation extends React.Component {
this._isMounted = true;

await this.setLocationManager({
required: this.isLocationManagerRequired(),
required: this.checkLocationManagerRequired(),
});

if (this.renderMode === UserLocation.RenderMode.Native) {
Expand All @@ -155,7 +155,7 @@ class UserLocation extends React.Component {

async componentDidUpdate(prevProps) {
await this.setLocationManager({
required: this.isLocationManagerRequired(),
required: this.checkLocationManagerRequired(),
});

if (this.props.minDisplacement !== prevProps.minDisplacement) {
Expand Down Expand Up @@ -183,8 +183,6 @@ class UserLocation extends React.Component {
this._isLocationManagerRequired = required;
if (required) {
locationManager.addListener(this._onLocationUpdate);
const location = await locationManager.getLastKnownLocation();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed this the last time around, I think you can remove getLastKnownLocation then from locationManager as it's not being used anymore?

Copy link
Contributor Author

@soumyashisPR soumyashisPR Sep 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, this is actually being used in setLocationManager
if (running) { locationManager.addListener(this._onLocationUpdate); const location = await locationManager.getLastKnownLocation(); this._onLocationUpdate(location); } else { locationManager.removeListener(this._onLocationUpdate); }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean... yeah, now it does after you reverted it again :)

this._onLocationUpdate(location);
} else {
locationManager.removeListener(this._onLocationUpdate);
}
Expand All @@ -193,11 +191,11 @@ class UserLocation extends React.Component {

/**
*
* If locationManager is required.
* Checks if locationManager is required.
*
* @return {boolean}
* @return {boolean} True is required
*/
isLocationManagerRequired() {
checkLocationManagerRequired() {
if (this.props.renderMode === UserLocation.RenderMode.Native) {
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions javascript/modules/location/locationManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class LocationManager {
}
if (!this._listeners.includes(listener)) {
this._listeners.push(listener);

if (this._lastKnownLocation) {
listener(this._lastKnownLocation);
}
}
}

Expand Down