Skip to content

Commit

Permalink
Add correct safe calls
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Aug 1, 2024
1 parent a00daf0 commit fce73a4
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 22 deletions.
96 changes: 96 additions & 0 deletions packages/web-api/src/geolocation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,99 @@ describe('trackGeolocation, providers as a Store', () => {
`);
});
});

describe('trackGeolocation, failure of provider', () => {
const brokenProvider = () => ({
async getCurrentPosition() {
throw new Error('Wow, government do not want you to use this provider!');
},
watchPosition(success: any, error: any) {
throw new Error('Wow, government do not want you to use this provider!');
},
});

const slightlyBrokenProvider = () => ({
async getCurrentPosition() {
throw new Error('Not this time, buddy!');
},
watchPosition(success: any, error: any) {
error(new Error('Not this time, buddy!'));

return () => {};
},
});

const okProvider = () => ({
async getCurrentPosition() {
return {
coords: { latitude: 2, longitude: 2 },
timestamp: Date.now(),
};
},
watchPosition(success: any, error: any) {
success({
coords: { latitude: 2, longitude: 2 },
timestamp: Date.now(),
});
return () => {};
},
});

const geo = trackGeolocation({
providers: [brokenProvider, slightlyBrokenProvider, okProvider],
});

test('request', async () => {
const scope = fork();

const failedWatcher = vi.fn();
createWatch({ unit: geo.reporting.failed, fn: failedWatcher, scope });

await allSettled(geo.request, { scope });

expect(scope.getState(geo.$location)).toMatchInlineSnapshot(`
{
"latitude": 2,
"longitude": 2,
}
`);

expect(failedWatcher.mock.calls).toMatchInlineSnapshot(`
[
[
[Error: Wow, government do not want you to use this provider!],
],
[
[Error: Not this time, buddy!],
],
]
`);
});

test('watching', async () => {
const scope = fork();

const failedWatcher = vi.fn();
createWatch({ unit: geo.reporting.failed, fn: failedWatcher, scope });

await allSettled(geo.watching.start, { scope });

expect(scope.getState(geo.$location)).toMatchInlineSnapshot(`
{
"latitude": 2,
"longitude": 2,
}
`);

expect(failedWatcher.mock.calls).toMatchInlineSnapshot(`
[
[
[Error: Wow, government do not want you to use this provider!],
],
[
[Error: Not this time, buddy!],
],
]
`);
});
});
57 changes: 35 additions & 22 deletions packages/web-api/src/geolocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,20 @@ export function trackGeolocation(
| CustomGeolocationPosition
| null = null;

const boundFailed = scopeBind(failed, { safe: true });

for (const provider of providers ?? []) {
if (isDefaultProvider(provider)) {
geolocation = await new Promise<GeolocationPosition>(
(resolve, reject) =>
provider.getCurrentPosition(resolve, reject, params)
);
} else {
geolocation = await provider.getCurrentPosition();
try {
if (isDefaultProvider(provider)) {
geolocation = await new Promise<GeolocationPosition>(
(resolve, reject) =>
provider.getCurrentPosition(resolve, reject, params)
);
} else {
geolocation = await provider.getCurrentPosition();
}
} catch (e: any) {
boundFailed(e);
}
}

Expand Down Expand Up @@ -217,21 +223,28 @@ export function trackGeolocation(
const customUnwatchSet = new Set<Unsubscribe>();

for (const provider of providers ?? []) {
if (isDefaultProvider(provider)) {
const watchId = provider.watchPosition(
boundNewPosition,
boundFailed,
params
);

defaultUnwatchMap.set(
(id: number) => provider.clearWatch(id),
watchId
);
} else {
const unwatch = provider.watchPosition(boundNewPosition, boundFailed);

customUnwatchSet.add(unwatch);
try {
if (isDefaultProvider(provider)) {
const watchId = provider.watchPosition(
boundNewPosition,
boundFailed,
params
);

defaultUnwatchMap.set(
(id: number) => provider.clearWatch(id),
watchId
);
} else {
const unwatch = provider.watchPosition(
boundNewPosition,
boundFailed
);

customUnwatchSet.add(unwatch);
}
} catch (e: any) {
boundFailed(e);
}
}

Expand Down

0 comments on commit fce73a4

Please sign in to comment.