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
Maps: location manager start/stop listening fix
  • Loading branch information
soumyashisPR committed Aug 20, 2020
commit 5cf5ee9e6ba3b87e8e2ede347b09b76137f70f61
37 changes: 19 additions & 18 deletions javascript/components/UserLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ class UserLocation extends React.Component {
// after component unmount
_isMounted = null;

locationManagerRunning = false;
locationManagerRequired = false;

async componentDidMount() {
this._isMounted = true;

locationManager.addListener(this._onLocationUpdate);
// locationManager.addListener(this._onLocationUpdate);

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

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

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

if (this.props.minDisplacement !== prevProps.minDisplacement) {
Expand All @@ -167,30 +167,31 @@ 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});
// locationManager.removeListener(this._onLocationUpdate);
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();

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);
async setLocationManager({required}) {
if (this.locationManagerRequired !== required) {
this.locationManagerRequired = required;
if (required) {
// locationManager.start();
locationManager.addListener(this._onLocationUpdate);
// const location = await locationManager.getLastKnownLocation();
// this._onLocationUpdate(location);
} else {
locationManager.stop();
// 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