docs: fix non-working example in the `Observables` section (#34705)
PR Close #34705
This commit is contained in:
parent
0083443050
commit
99cc7cdc15
|
@ -5,28 +5,39 @@ import { Observable } from 'rxjs';
|
||||||
// Create an Observable that will start listening to geolocation updates
|
// Create an Observable that will start listening to geolocation updates
|
||||||
// when a consumer subscribes.
|
// when a consumer subscribes.
|
||||||
const locations = new Observable((observer) => {
|
const locations = new Observable((observer) => {
|
||||||
// Get the next and error callbacks. These will be passed in when
|
let watchId: number;
|
||||||
// the consumer subscribes.
|
|
||||||
const {next, error} = observer;
|
|
||||||
let watchId;
|
|
||||||
|
|
||||||
// Simple geolocation API check provides values to publish
|
// Simple geolocation API check provides values to publish
|
||||||
if ('geolocation' in navigator) {
|
if ('geolocation' in navigator) {
|
||||||
watchId = navigator.geolocation.watchPosition(next, error);
|
watchId = navigator.geolocation.watchPosition((position: Position) => {
|
||||||
|
observer.next(position);
|
||||||
|
}, (error: PositionError) => {
|
||||||
|
observer.error(error);
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
error('Geolocation not available');
|
observer.error('Geolocation not available');
|
||||||
}
|
}
|
||||||
|
|
||||||
// When the consumer unsubscribes, clean up data ready for next subscription.
|
// When the consumer unsubscribes, clean up data ready for next subscription.
|
||||||
return {unsubscribe() { navigator.geolocation.clearWatch(watchId); }};
|
return {
|
||||||
|
unsubscribe() {
|
||||||
|
navigator.geolocation.clearWatch(watchId);
|
||||||
|
}
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// Call subscribe() to start listening for updates.
|
// Call subscribe() to start listening for updates.
|
||||||
const locationsSubscription = locations.subscribe({
|
const locationsSubscription = locations.subscribe({
|
||||||
next(position) { console.log('Current Position: ', position); },
|
next(position) {
|
||||||
error(msg) { console.log('Error Getting Location: ', msg); }
|
console.log('Current Position: ', position);
|
||||||
|
},
|
||||||
|
error(msg) {
|
||||||
|
console.log('Error Getting Location: ', msg);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Stop listening for location after 10 seconds
|
// Stop listening for location after 10 seconds
|
||||||
setTimeout(() => { locationsSubscription.unsubscribe(); }, 10000);
|
setTimeout(() => {
|
||||||
|
locationsSubscription.unsubscribe();
|
||||||
|
}, 10000);
|
||||||
// #enddocregion
|
// #enddocregion
|
||||||
|
|
Loading…
Reference in New Issue