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 11 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
48 changes: 25 additions & 23 deletions __tests__/components/UserLocation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,12 @@ describe('UserLocation', () => {
const {UNSAFE_queryByType} = render(
<UserLocation onPress={onPressCallback} />,
);

const shapeSource = UNSAFE_queryByType(ShapeSource);
fireEvent(shapeSource, 'onPress');
fireEvent(shapeSource, 'onPress');
expect(onPressCallback).toHaveBeenCalledTimes(2);
setTimeout(() => {
const shapeSource = UNSAFE_queryByType(ShapeSource);
fireEvent(shapeSource, 'onPress');
fireEvent(shapeSource, 'onPress');
expect(onPressCallback).toHaveBeenCalledTimes(2);
});
ferdicus marked this conversation as resolved.
Show resolved Hide resolved
});

test('correctly unmounts', async () => {
Expand Down Expand Up @@ -171,20 +172,20 @@ describe('UserLocation', () => {
};

expect(ul.state).toStrictEqual(initialState);
expect(ul.locationManagerRunning).toStrictEqual(false);
expect(ul._isLocationManagerRequired).toStrictEqual(false);
});

// TODO: replace object { running: boolean } argument with simple boolean
// TODO: replace object { required: boolean } argument with simple boolean
describe('#setLocationManager', () => {
test('called with "running" true', async () => {
test('called with "required" true', async () => {
const lastKnownLocation = [4.1036916, 51.5462244];
const heading = 251.5358428955078;

expect(ul.locationManagerRunning).toStrictEqual(false);
expect(ul._isLocationManagerRequired).toStrictEqual(false);

await ul.setLocationManager({running: true});
await ul.setLocationManager({required: true});

expect(ul.locationManagerRunning).toStrictEqual(true);
expect(ul._isLocationManagerRequired).toStrictEqual(true);
expect(locationManager.start).toHaveBeenCalledTimes(1);
expect(locationManager.getLastKnownLocation).toHaveBeenCalledTimes(1);
expect(ul.setState).toHaveBeenCalledTimes(1);
Expand All @@ -195,48 +196,49 @@ describe('UserLocation', () => {
expect(locationManager.stop).not.toHaveBeenCalled();
});

test('called with "running" false', async () => {
test('called with "required" false', async () => {
// start
expect(ul.locationManagerRunning).toStrictEqual(false);
await ul.setLocationManager({running: true});
expect(ul.locationManagerRunning).toStrictEqual(true);
expect(ul._isLocationManagerRequired).toStrictEqual(false);
await ul.setLocationManager({required: true});
expect(ul._isLocationManagerRequired).toStrictEqual(true);

// stop
await ul.setLocationManager({running: false});
await ul.setLocationManager({required: false});

expect(ul.locationManagerRunning).toStrictEqual(false);
expect(ul._isLocationManagerRequired).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();
});
});

describe('#needsLocationManagerRunning', () => {
describe('#isLocationManagerRequired', () => {
test('returns true correctly', () => {
// default props "onUpdate: undefined, visible: true"
expect(ul.needsLocationManagerRunning()).toStrictEqual(true);
expect(ul.isLocationManagerRequired()).toStrictEqual(true);

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

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

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

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

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

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

Expand Down
10 changes: 6 additions & 4 deletions __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 @@ -88,17 +89,18 @@ describe('LocationManager', () => {
expect(myListener).not.toHaveBeenCalled();
});

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

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

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');
Expand Down Expand Up @@ -142,7 +144,7 @@ describe('LocationManager', () => {
jest.spyOn(MapboxGLLocationManager, 'start');
jest.spyOn(LocationModuleEventEmitter, 'addListener');

afterEach(() => {
beforeEach(() => {
locationManager._isListening = false;
});

Expand Down
10 changes: 5 additions & 5 deletions docs/UserLocation.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@
| children | `any` | `none` | `false` | Custom location icon of type mapbox-gl-native components |

### methods
#### setLocationManager({running})
ferdicus marked this conversation as resolved.
Show resolved Hide resolved
#### setLocationManager({required})

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 |
| ---- | :--: | :------: | :----------: |
| `{running}` | `n/a` | `Yes` | undefined |
| `{required}` | `n/a` | `Yes` | undefined |


#### needsLocationManagerRunning()
#### isLocationManagerRequired()

If locationManager should be running
If locationManager is required.

##### arguments
| Name | Type | Required | Description |
Expand Down
12 changes: 6 additions & 6 deletions docs/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -5164,13 +5164,13 @@
"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} required - Object with key `required` and `boolean` value\n@return {Promise<void>}",
"modifiers": [
"async"
],
"params": [
{
"name": "{running}"
"name": "{required}"
}
],
"returns": {
Expand All @@ -5184,12 +5184,12 @@
]
}
},
"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": []
},
{
"name": "needsLocationManagerRunning",
"docblock": "If locationManager should be running\n\n@return {boolean}",
"name": "isLocationManagerRequired",
"docblock": "If locationManager is required.\n\n@return {boolean}",
"modifiers": [],
"params": [],
"returns": {
Expand All @@ -5198,7 +5198,7 @@
"name": "boolean"
}
},
"description": "If locationManager should be running",
"description": "If locationManager is required.",
"examples": []
}
],
Expand Down
34 changes: 15 additions & 19 deletions javascript/components/UserLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,13 @@ class UserLocation extends React.Component {
// after component unmount
_isMounted = null;

locationManagerRunning = false;
_isLocationManagerRequired = false;

async componentDidMount() {
this._isMounted = true;

locationManager.addListener(this._onLocationUpdate);

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

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

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

if (this.props.minDisplacement !== prevProps.minDisplacement) {
Expand All @@ -167,41 +165,39 @@ 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});
await this.setLocationManager({required: 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
* @param {Object} running - Object with key `running` and `boolean` value
* @param {Object} required - Object with key `required` and `boolean` value
* @return {Promise<void>}
*/
async setLocationManager({running}) {
if (this.locationManagerRunning !== running) {
this.locationManagerRunning = running;
if (running) {
locationManager.start();

async setLocationManager({required}) {
if (this._isLocationManagerRequired !== required) {
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.stop();
locationManager.removeListener(this._onLocationUpdate);
}
}
}

/**
*
* If locationManager should be running
* If locationManager is required.
*
* @return {boolean}
*/
needsLocationManagerRunning() {
isLocationManagerRequired() {
ferdicus marked this conversation as resolved.
Show resolved Hide resolved
if (this.props.renderMode === UserLocation.RenderMode.Native) {
return false;
}
Expand Down
11 changes: 7 additions & 4 deletions javascript/modules/location/locationManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,24 @@ class LocationManager {
}

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

if (this._lastKnownLocation) {
Copy link
Contributor

Choose a reason for hiding this comment

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

why was this removed?!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change reverted.
Reason for removing this, I was trying to keep addListener() and the method call listener() independent, but didn't work out.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mfazekas Please let me know if any more changes are required with this PR.
Thanks.

listener(this._lastKnownLocation);
}
}
}

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