ionic-team / capacitor-plugins

Official plugins for Capacitor ⚡️
530 stars 599 forks source link

bug: when called subsequently, Geolocation.getCurrentPosition() doesn't resolve #413

Open aprat84 opened 4 years ago

aprat84 commented 4 years ago

Bug Report

Capacitor Version

npx cap doctor output:

💊   Capacitor Doctor  💊 
Latest Dependencies:
  @capacitor/cli: 2.1.2
  @capacitor/core: 2.1.2
  @capacitor/android: 2.1.2
  @capacitor/electron: 2.1.2
  @capacitor/ios: 2.1.2
Installed Dependencies:
  @capacitor/ios not installed
  @capacitor/electron not installed
  @capacitor/cli 2.1.2
  @capacitor/android 2.1.2
  @capacitor/core 2.1.2
[success] Android looking great! 👌

Affected Platform(s)

Current Behavior

If multiple parallel calls to Geolocation.getCurrentPosition() are made, only last one returns any result, success or error.

alt text

Expected Behavior

Every call to Geolocation.getCurrentPosition() should have it's promise fullfilled or rejected. This shoud be the way, besides what the plugin does internally (which native API uses, cached position, ...)

Sample Code or Sample Application Repo

const params = {
    enableHighAccuracy: true,
    maximumAge: 20000,
    timeout: 50,
};

console.log('position 1 start')
Capacitor.Plugins.Geolocation.getCurrentPosition(params).then(
  position => console.log('position 1', position), 
  error => console.error('position 1 error', error)
);
console.log('position 2 start')
Capacitor.Plugins.Geolocation.getCurrentPosition(params).then(
  position => console.log('position 2', position), 
  error => console.error('position 2 error', error)
);
console.log('position 3 start')
Capacitor.Plugins.Geolocation.getCurrentPosition(params).then(
  position => console.log('position 3', position), 
  error => console.error('position 3 error', error)
);
console.log('position 4 start')
Capacitor.Plugins.Geolocation.getCurrentPosition(params).then(
  position => console.log('position 4', position), 
  error => console.error('position 4 error', error)
);

Reproduction Steps

Other Technical Details

npm --version output: 6.14.4 node --version output: v12.17.0

ahsan-alii commented 4 years ago

Having the same issue

AE1NS commented 4 years ago

Same here. No news on this?

Edit: A workaround could be a helper method, that returns a custom observable, that is resolved for all subscribers, when 'Plugins.Geolocation.getCurrentPosition' returns a value:

private _getCurrentPosition$: Subject<LatLng>;

...

getCurrentPosition() {
    if (!this._getCurrentPosition$) {
        this._getCurrentPosition$ = new Subject();
        Plugins.Geolocation.getCurrentPosition().then((currentPosition) => {
            this._getCurrentPosition$.next(currentPosition);
            this._getCurrentPosition$.complete();
            this._getCurrentPosition$ = null;
        });
    }

    return this._getCurrentPosition$;
}
alexcroox commented 1 year ago

I've noticed getCurrentPosition doesn't resolve at all if watchPosition() has been called before, not sure if related