-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
970 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import type { PartialGeolocationCoordinates } from "./geolocation-coordinates.js"; | ||
import { type GeolocationPositionErrorCode } from "./geolocation-position-error.js"; | ||
|
||
export type GeolocationObserver = { | ||
waitForCoordinates( | ||
matcherOrMatchers?: | ||
| PartialGeolocationCoordinates | ||
| PartialGeolocationCoordinates[], | ||
task?: () => Promise<void>, | ||
positionOptions?: PositionOptions, | ||
): Promise<void>; | ||
waitForPositionError( | ||
codeOrCodes?: GeolocationPositionErrorCode | GeolocationPositionErrorCode[], | ||
task?: () => Promise<void>, | ||
positionOptions?: PositionOptions, | ||
): Promise<void>; | ||
}; | ||
|
||
export function createGeolocationObserver( | ||
geolocation: Geolocation, | ||
): GeolocationObserver { | ||
return { | ||
async waitForCoordinates(matcherOrMatchers = [], task, positionOptions) { | ||
const matchers = normalizeMatchers(matcherOrMatchers); | ||
|
||
await Promise.all([ | ||
new Promise<void>((resolve) => { | ||
const stop = watchPositionRetry( | ||
({ coords }) => { | ||
if (!isMatchingCoords(matchers, coords)) return; | ||
stop(); | ||
resolve(); | ||
}, | ||
undefined, | ||
positionOptions, | ||
); | ||
}), | ||
Promise.resolve(task?.()), | ||
]); | ||
}, | ||
|
||
async waitForPositionError(codeOrCodes = [], task, positionOptions) { | ||
const codes = normalizeCodes(codeOrCodes); | ||
|
||
await Promise.all([ | ||
new Promise<void>((resolve) => { | ||
const stop = watchPositionRetry( | ||
undefined, | ||
(error) => { | ||
if (!isMatchingError(codes, error)) return; | ||
stop(); | ||
resolve(); | ||
}, | ||
positionOptions, | ||
); | ||
}), | ||
Promise.resolve(task?.()), | ||
]); | ||
}, | ||
}; | ||
|
||
function isMatchingCoords( | ||
matchers: PartialGeolocationCoordinates[], | ||
coords: GeolocationCoordinates, | ||
): boolean { | ||
if (matchers.length < 1) return true; | ||
|
||
nextMatcher: for (const matcher of matchers) { | ||
for (const property in matcher) { | ||
if ( | ||
matcher[property as keyof typeof matcher] !== | ||
coords[property as keyof typeof coords] | ||
) { | ||
continue nextMatcher; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function isMatchingError( | ||
codes: GeolocationPositionErrorCode[], | ||
error: GeolocationPositionError, | ||
): boolean { | ||
if (codes.length < 1) return true; | ||
|
||
return codes.includes(error.code as GeolocationPositionErrorCode); | ||
} | ||
|
||
function watchPositionRetry( | ||
successCallback?: PositionCallback, | ||
errorCallback?: PositionErrorCallback, | ||
options: PositionOptions = {}, | ||
): () => void { | ||
let isStopped = false; | ||
let watchId: number = 0; | ||
|
||
startWatch(); | ||
|
||
function startWatch() { | ||
if (isStopped) return; | ||
|
||
watchId = geolocation.watchPosition( | ||
(p) => { | ||
/* v8 ignore next: race condition failsafe */ | ||
if (isStopped) return; | ||
|
||
successCallback?.(p); | ||
}, | ||
(e) => { | ||
/* v8 ignore next: race condition failsafe */ | ||
if (isStopped) return; | ||
|
||
geolocation.clearWatch(watchId); | ||
setTimeout(startWatch, 20); | ||
errorCallback?.(e); | ||
}, | ||
options, | ||
); | ||
} | ||
|
||
return () => { | ||
isStopped = true; | ||
geolocation.clearWatch(watchId); | ||
}; | ||
} | ||
} | ||
|
||
function normalizeMatchers( | ||
matchers: PartialGeolocationCoordinates | PartialGeolocationCoordinates[], | ||
): PartialGeolocationCoordinates[] { | ||
return Array.isArray(matchers) ? matchers : [matchers]; | ||
} | ||
|
||
function normalizeCodes( | ||
codes: GeolocationPositionErrorCode | GeolocationPositionErrorCode[], | ||
): GeolocationPositionErrorCode[] { | ||
return Array.isArray(codes) ? codes : [codes]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.