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 15 commits
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
5 changes: 3 additions & 2 deletions __tests__/components/UserLocation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ describe('UserLocation', () => {
expect(ul.locationManagerRunning).toStrictEqual(true);
expect(locationManager.start).toHaveBeenCalledTimes(1);
expect(locationManager.getLastKnownLocation).toHaveBeenCalledTimes(1);
expect(ul.setState).toHaveBeenCalledTimes(1);
expect(ul.setState).toHaveBeenCalledTimes(2);
expect(ul.setState).toHaveBeenCalledWith({
coordinates: lastKnownLocation,
heading,
Expand All @@ -207,7 +207,8 @@ describe('UserLocation', () => {
expect(ul.locationManagerRunning).toStrictEqual(false);
// only once from start
expect(locationManager.start).toHaveBeenCalledTimes(1);
expect(locationManager.stop).toHaveBeenCalledTimes(1);
// stop should not be called
expect(locationManager.stop).not.toHaveBeenCalled();
});
});

Expand Down
11 changes: 10 additions & 1 deletion __tests__/modules/location/locationManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe('LocationManager', () => {

describe('#addListener', () => {
const myListener = jest.fn();
MapboxGL.LocationCallbackName = {Update: 'MapboxUserLocationUpdate'};

afterEach(() => {
locationManager._listeners = [];
Expand All @@ -80,7 +81,7 @@ describe('LocationManager', () => {
expect(locationManager._listeners).toStrictEqual([myListener]);
});

test('does not readd same listener', () => {
test('does not read same listener', () => {
Copy link
Contributor

@mfazekas mfazekas Oct 1, 2020

Choose a reason for hiding this comment

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

I think 're-add' make more sense here

Suggested change
test('does not read same listener', () => {
test('does not readd same listener', () => {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

locationManager.addListener(myListener);
expect(locationManager._listeners).toStrictEqual([myListener]);
locationManager.addListener(myListener);
Expand All @@ -99,23 +100,31 @@ describe('LocationManager', () => {
});

describe('#removeListener', () => {
MapboxGLLocationManager.stop = jest.fn();

ferdicus marked this conversation as resolved.
Show resolved Hide resolved
test('removes selected listener', () => {
// just two different functions
const listenerA = jest.fn(() => 'listenerA');
const listenerB = () => 'listenerB';

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
2 changes: 1 addition & 1 deletion docs/UserLocation.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
### methods
#### setLocationManager({running})
ferdicus marked this conversation as resolved.
Show resolved Hide resolved

Whether to start or stop the locationManager<br/><br/>Notice, that locationManager will start automatically when<br/>either `onUpdate` or `visible` are set
Whether to start or stop listening to the locationManager<br/><br/>Notice, that listening will start automatically when<br/>either `onUpdate` or `visible` are set

##### arguments
| Name | Type | Required | Description |
Expand Down
4 changes: 2 additions & 2 deletions docs/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -5164,7 +5164,7 @@
"methods": [
{
"name": "setLocationManager",
"docblock": "Whether to start or stop the locationManager\n\nNotice, that locationManager will start automatically when\neither `onUpdate` or `visible` are set\n\n@async\n@param {Object} running - Object with key `running` and `boolean` value\n@return {Promise<void>}",
"docblock": "Whether to start or stop listening to the locationManager\n\nNotice, that listening will start automatically when\neither `onUpdate` or `visible` are set\n\n@async\n@param {Object} running - Object with key `running` and `boolean` value\n@return {Promise<void>}",
"modifiers": [
"async"
],
Expand All @@ -5184,7 +5184,7 @@
]
}
},
"description": "Whether to start or stop the locationManager\n\nNotice, that locationManager will start automatically when\neither `onUpdate` or `visible` are set",
"description": "Whether to start or stop listening to the locationManager\n\nNotice, that listening will start automatically when\neither `onUpdate` or `visible` are set",
"examples": []
},
{
Expand Down
12 changes: 4 additions & 8 deletions javascript/components/UserLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ class UserLocation extends React.Component {
async componentDidMount() {
this._isMounted = true;

locationManager.addListener(this._onLocationUpdate);

await this.setLocationManager({
running: this.needsLocationManagerRunning(),
});
Expand All @@ -167,14 +165,13 @@ class UserLocation extends React.Component {

async componentWillUnmount() {
this._isMounted = false;
locationManager.removeListener(this._onLocationUpdate);
mfazekas marked this conversation as resolved.
Show resolved Hide resolved
await this.setLocationManager({running: false});
}

/**
* Whether to start or stop the locationManager
* Whether to start or stop listening to the locationManager
*
* Notice, that locationManager will start automatically when
* Notice, that listening will start automatically when
* either `onUpdate` or `visible` are set
*
* @async
Expand All @@ -185,12 +182,11 @@ class UserLocation extends React.Component {
if (this.locationManagerRunning !== running) {
this.locationManagerRunning = running;
if (running) {
locationManager.start();

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.stop();
locationManager.removeListener(this._onLocationUpdate);
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions javascript/modules/location/locationManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class LocationManager {
}

addListener(listener) {
if (!this._isListening) {
this.start();
}
if (!this._listeners.includes(listener)) {
this._listeners.push(listener);

Expand All @@ -49,10 +52,14 @@ class LocationManager {

removeListener(listener) {
this._listeners = this._listeners.filter((l) => l !== listener);
if (this._listeners.length === 0) {
this.stop();
}
}

removeAllListeners() {
this._listeners = [];
this.stop();
}

start(displacement = 0) {
Expand Down