mirror of
https://github.com/kennethnym/aris.git
synced 2026-02-02 13:11:17 +00:00
refactor(core): rename getCurrentValue to fetchCurrentValue
Also use Promise.allSettled in ContextBridge.refresh() to handle provider errors gracefully. Co-authored-by: Ona <no-reply@ona.com>
This commit is contained in:
@@ -107,7 +107,7 @@ class LocationProvider implements ContextProvider<Location> {
|
||||
return () => navigator.geolocation.clearWatch(watchId)
|
||||
}
|
||||
|
||||
async getCurrentValue(): Promise<Location> {
|
||||
async fetchCurrentValue(): Promise<Location> {
|
||||
const pos = await new Promise<GeolocationPosition>((resolve, reject) => {
|
||||
navigator.geolocation.getCurrentPosition(resolve, reject)
|
||||
})
|
||||
|
||||
@@ -55,15 +55,21 @@ export class ContextBridge {
|
||||
/**
|
||||
* Gathers current values from all providers and pushes to controller.
|
||||
* Use for manual refresh when user pulls to refresh.
|
||||
* Errors from individual providers are silently ignored.
|
||||
*/
|
||||
async refresh(): Promise<void> {
|
||||
const updates: Partial<Context> = {}
|
||||
|
||||
const entries = Array.from(this.providers.entries())
|
||||
const values = await Promise.all(entries.map(([_, provider]) => provider.getCurrentValue()))
|
||||
const results = await Promise.allSettled(
|
||||
entries.map(([_, provider]) => provider.fetchCurrentValue()),
|
||||
)
|
||||
|
||||
entries.forEach(([key], i) => {
|
||||
updates[key] = values[i]
|
||||
const result = results[i]
|
||||
if (result?.status === "fulfilled") {
|
||||
updates[key] = result.value
|
||||
}
|
||||
})
|
||||
|
||||
this.controller.pushContextUpdate(updates)
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
* return () => navigator.geolocation.clearWatch(watchId)
|
||||
* }
|
||||
*
|
||||
* async getCurrentValue(): Promise<Location> {
|
||||
* async fetchCurrentValue(): Promise<Location> {
|
||||
* const pos = await getCurrentPosition()
|
||||
* return { lat: pos.coords.latitude, lng: pos.coords.longitude, accuracy: pos.coords.accuracy }
|
||||
* }
|
||||
@@ -30,6 +30,6 @@ export interface ContextProvider<T = unknown> {
|
||||
/** Subscribe to value changes. Returns cleanup function. */
|
||||
onUpdate(callback: (value: T) => void): () => void
|
||||
|
||||
/** Get current value on-demand (used for manual refresh). */
|
||||
getCurrentValue(): Promise<T>
|
||||
/** Fetch current value on-demand (used for manual refresh). */
|
||||
fetchCurrentValue(): Promise<T>
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ function createLocationProvider(): SimulatedLocationProvider {
|
||||
callback = null
|
||||
}
|
||||
},
|
||||
async getCurrentValue() {
|
||||
async fetchCurrentValue() {
|
||||
return currentLocation
|
||||
},
|
||||
simulateUpdate(location: Location) {
|
||||
|
||||
Reference in New Issue
Block a user