2026-01-18 00:58:29 +00:00
|
|
|
/**
|
|
|
|
|
* Provides context values reactively and on-demand.
|
|
|
|
|
*
|
|
|
|
|
* Implementations push updates when values change (reactive) and
|
|
|
|
|
* return current values when requested (for manual refresh).
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* ```ts
|
|
|
|
|
* class LocationProvider implements ContextProvider<Location> {
|
|
|
|
|
* readonly key = LocationKey
|
|
|
|
|
*
|
|
|
|
|
* onUpdate(callback: (value: Location) => void): () => void {
|
|
|
|
|
* const watchId = navigator.geolocation.watchPosition(pos => {
|
|
|
|
|
* callback({ lat: pos.coords.latitude, lng: pos.coords.longitude, accuracy: pos.coords.accuracy })
|
|
|
|
|
* })
|
|
|
|
|
* return () => navigator.geolocation.clearWatch(watchId)
|
|
|
|
|
* }
|
|
|
|
|
*
|
2026-01-18 20:23:54 +00:00
|
|
|
* async fetchCurrentValue(): Promise<Location> {
|
2026-01-18 00:58:29 +00:00
|
|
|
* const pos = await getCurrentPosition()
|
|
|
|
|
* return { lat: pos.coords.latitude, lng: pos.coords.longitude, accuracy: pos.coords.accuracy }
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
|
|
|
|
export interface ContextProvider<T = unknown> {
|
|
|
|
|
/** The context key this provider populates */
|
|
|
|
|
readonly key: string
|
|
|
|
|
|
|
|
|
|
/** Subscribe to value changes. Returns cleanup function. */
|
|
|
|
|
onUpdate(callback: (value: T) => void): () => void
|
|
|
|
|
|
2026-01-18 20:23:54 +00:00
|
|
|
/** Fetch current value on-demand (used for manual refresh). */
|
|
|
|
|
fetchCurrentValue(): Promise<T>
|
2026-01-18 00:58:29 +00:00
|
|
|
}
|