Class GeolocationWatcher
- All Implemented Interfaces:
Serializable
Geolocation.watchPosition(Component) and its overload.
Two ways to consume the stream of readings:
addPositionListener(SerializableConsumer, SerializableConsumer)— non-reactive callback pair. Convenient when the destination is a service, repository, or anything that is not a UI component (e.g. a sports tracker that writes points to a database).positionSignal()— reactive signal ofGeolocationResult. Convenient when binding component state to the position viaSignal.effectorcomponent.bindText(...).
stop() to cancel sooner and
resume() to restart on the same handle. Bindings and listeners
survive stop/resume cycles.
The watch starts as soon as the owning component is attached to a UI: if the
component is already attached when the watcher is created the watch starts
immediately, otherwise it starts on first attach. Calling stop()
before the first attach cancels the pending activation; the watcher can still
be activated later by calling resume() on an attached owner.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionReturns a read-only signal indicating whether the watcher is currently receiving updates.addPositionListener(SerializableConsumer<GeolocationPosition> onPosition, SerializableConsumer<GeolocationError> onError) Subscribes to position and error pushes from the watch.Returns a read-only signal that holds the most recent reading.voidresume()Starts, or resumes, the underlying browser watch.voidstop()Cancels the underlying browser watch and tears down the server-side subscriptions.
-
Method Details
-
positionSignal
Returns a read-only signal that holds the most recent reading.Starts as
GeolocationPendinguntil the browser reports its first value, then transitions toGeolocationPositionon every successful reading orGeolocationErroron failure. Afterstop()the signal stops receiving updates but its last value stays readable;resume()resets the value toGeolocationPendingand resumes updates. Subscribers stay attached across stop/resume cycles.- Returns:
- a read-only signal reporting the latest reading
-
activeSignal
Returns a read-only signal indicating whether the watcher is currently receiving updates. Flips totrueonresume()and tofalseonstop()(or when the owner detaches). Useful for binding a "Stop tracking" toggle's state without tracking a separate flag.- Returns:
- a read-only signal reporting whether the watch is active
-
addPositionListener
public Registration addPositionListener(SerializableConsumer<GeolocationPosition> onPosition, SerializableConsumer<GeolocationError> onError) Subscribes to position and error pushes from the watch.onPositionfires for everyGeolocationPositionthe browser reports.onErrorfires for everyGeolocationErrorthe browser reports. Neither fires for the initialGeolocationPendingstate. Listeners stay attached acrossstop()/resume()cycles; remove them through the returnedRegistration.Both consumers are required and must be non-null. To opt out of either notification, pass
pos -> {}orerr -> {}explicitly.- Parameters:
onPosition- invoked on every successful reading, nevernullonError- invoked on every error reading, nevernull- Returns:
- a registration that removes both listeners when removed
- Throws:
NullPointerException- if either consumer isnull
-
resume
public void resume()Starts, or resumes, the underlying browser watch.Called automatically when the owner is attached to a UI: immediately if it is already attached when the watcher is created, otherwise on first attach. Call again after
stop()to resume on the same handle — bindings and listeners stay attached and start receiving updates again. The signal resets toGeolocationPendingon every resume. Callingresume()on an already-running watcher is a no-op.- Throws:
IllegalStateException- if the owner is not attached to a UI
-
stop
public void stop()Cancels the underlying browser watch and tears down the server-side subscriptions.The browser stops reporting updates and
positionSignal()stops changing. The last value remains readable. Detaching the owning component calls this automatically, so most applications never need to call it from a detach listener.Idempotent: calling it twice, or after the owner has already detached, does nothing extra. After
stop()the watcher can be resumed withresume().
-