Skip to content

Commit

Permalink
Merge pull request #11 from soumyashisPR/maps-issue-915-ss
Browse files Browse the repository at this point in the history
Maps issue 915: Stop/start Location manager based on listeners registered.
  • Loading branch information
soumyashisPR committed Aug 23, 2020
2 parents 8af090c + ccf3c1c commit b61268b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
36 changes: 15 additions & 21 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,37 @@ class UserLocation extends React.Component {

async componentWillUnmount() {
this._isMounted = false;
locationManager.removeListener(this._onLocationUpdate);
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();

const location = await locationManager.getLastKnownLocation();
this._onLocationUpdate(location);
async setLocationManager({required}) {
if (this._isLocationManagerRequired !== required) {
this._isLocationManagerRequired = required;
if (required) {
locationManager.addListener(this._onLocationUpdate);
} else {
locationManager.stop();
locationManager.removeListener(this._onLocationUpdate);
}
}
}

/**
*
* If locationManager should be running
* If locationManager is required.
*
* @return {boolean}
*/
needsLocationManagerRunning() {
isLocationManagerRequired() {
if (this.props.renderMode === UserLocation.RenderMode.Native) {
return false;
}
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

0 comments on commit b61268b

Please sign in to comment.