5941ee7560
This is a fix to not include a null assert when it's possible the value was null. Fixes https://github.com/dart-lang/sdk/issues/49972 CoreLibraryReviewExempt: bug fix only affecting html library implementation Change-Id: I26e6ebf3dc2d1ea3f9f458a32f519c5235e6d273 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/350982 Commit-Queue: Sigmund Cherem <sigmund@google.com> Reviewed-by: Srujan Gaddam <srujzs@google.com>
98 lines
2.8 KiB
Plaintext
98 lines
2.8 KiB
Plaintext
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
part of $LIBRARYNAME;
|
|
|
|
$(ANNOTATIONS)$(NATIVESPEC)$(CLASS_MODIFIERS)class $CLASSNAME$EXTENDS$IMPLEMENTS {
|
|
|
|
Future<Geoposition> getCurrentPosition({bool$NULLABLE enableHighAccuracy,
|
|
Duration$NULLABLE timeout, Duration$NULLABLE maximumAge}) {
|
|
var options = {};
|
|
if (enableHighAccuracy != null) {
|
|
options['enableHighAccuracy'] = enableHighAccuracy;
|
|
}
|
|
if (timeout != null) {
|
|
options['timeout'] = timeout.inMilliseconds;
|
|
}
|
|
if (maximumAge != null) {
|
|
options['maximumAge'] = maximumAge.inMilliseconds;
|
|
}
|
|
var completer = new Completer<Geoposition>();
|
|
try {
|
|
_getCurrentPosition(
|
|
(position) {
|
|
completer.complete(_ensurePosition(position));
|
|
},
|
|
(error) {
|
|
completer.completeError(error);
|
|
},
|
|
options);
|
|
} catch (e, stacktrace) {
|
|
completer.completeError(e, stacktrace);
|
|
}
|
|
return completer.future;
|
|
}
|
|
|
|
Stream<Geoposition> watchPosition({bool$NULLABLE enableHighAccuracy,
|
|
Duration$NULLABLE timeout, Duration$NULLABLE maximumAge}) {
|
|
|
|
var options = {};
|
|
if (enableHighAccuracy != null) {
|
|
options['enableHighAccuracy'] = enableHighAccuracy;
|
|
}
|
|
if (timeout != null) {
|
|
options['timeout'] = timeout.inMilliseconds;
|
|
}
|
|
if (maximumAge != null) {
|
|
options['maximumAge'] = maximumAge.inMilliseconds;
|
|
}
|
|
|
|
int$NULLABLE watchId;
|
|
StreamController<Geoposition> controller =
|
|
new StreamController<Geoposition>(sync: true,
|
|
onCancel: () {
|
|
final id = watchId;
|
|
if (id != null) _clearWatch(id);
|
|
}
|
|
);
|
|
controller.onListen = () {
|
|
assert(watchId == null);
|
|
watchId = _watchPosition(
|
|
(position) {
|
|
controller.add(_ensurePosition(position));
|
|
},
|
|
(error) {
|
|
controller.addError(error);
|
|
},
|
|
options);
|
|
};
|
|
|
|
return controller.stream;
|
|
}
|
|
|
|
Geoposition _ensurePosition(domPosition) {
|
|
try {
|
|
// Firefox may throw on this.
|
|
if (domPosition is Geoposition) {
|
|
return domPosition;
|
|
}
|
|
} catch(e) {}
|
|
return new _GeopositionWrapper(domPosition);
|
|
}
|
|
|
|
$!MEMBERS}
|
|
|
|
/**
|
|
* Wrapper for Firefox- it returns an object which we cannot map correctly.
|
|
* Basically Firefox was returning a [xpconnect wrapped nsIDOMGeoPosition] but
|
|
* which has further oddities.
|
|
*/
|
|
class _GeopositionWrapper implements Geoposition {
|
|
var _ptr;
|
|
_GeopositionWrapper(this._ptr);
|
|
|
|
Coordinates get coords => JS('Coordinates', '#.coords', _ptr);
|
|
int get timestamp => JS('int', '#.timestamp', _ptr);
|
|
}
|