Skip to content

Commit 7e2e66f

Browse files
authored
Merge pull request #20 from NavAbility/jhr/19/async
Async throughout
2 parents f299a05 + 438d80d commit 7e2e66f

9 files changed

Lines changed: 109 additions & 11 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "navabilitysdk",
3-
"version": "0.3.1",
3+
"version": "0.4.0",
44
"description": "NavAbility SDK: Access NavAbility Cloud factor graph features from JavaScript. Note that this SDK and the related API are still in development. Please let us know if you have any issues at info@navability.io.",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
export * from './navability/graphql/QueriesDeprecated';
33
export * from './navability/graphql/Variable';
44
export * from './navability/graphql/Factor';
5+
export * from './navability/graphql/Status';
56

67
// Entities
78
export * from './navability/entities/Blob';
@@ -19,3 +20,5 @@ export * from './navability/services/Solve';
1920
export * from './navability/services/Example';
2021
export * from './navability/services/Blob';
2122
export * from './navability/services/Dataproc';
23+
export * from './navability/services/Status';
24+
export * from './navability/services/Utils';

src/navability/graphql/Status.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export const GQL_GETSTATUSMESSAGES = `
2+
query sdk_ls_statusmessages($id: ID!) {
3+
statusMessages(id: $id) {
4+
requestId,
5+
action,
6+
state,
7+
timestamp,
8+
client {
9+
userId,
10+
robotId,
11+
sessionId
12+
}
13+
}
14+
}
15+
`;
16+
17+
export const GQL_GETSTATUSLATEST = `
18+
query sdk_get_statuslatest($id: ID!) {
19+
statusLatest(id: $id) {
20+
requestId,
21+
action,
22+
state,
23+
timestamp,
24+
client {
25+
userId,
26+
robotId,
27+
sessionId
28+
}
29+
}
30+
}
31+
`;

src/navability/services/Dataproc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { NavAbilityClient } from '../entities/NavAbilityClient';
44
import { CalibrationResult } from '../entities/Dataproc';
55
import { MUTATION_PROC_CALIBRATION, QUERY_CALIBRATION } from '../graphql/QueriesDeprecated';
66

7-
export function procCalibration(navAbilityClient: NavAbilityClient, fileId: string) {
8-
navAbilityClient.mutate({
7+
export async function procCalibration(navAbilityClient: NavAbilityClient, fileId: string): Promise<void> {
8+
await navAbilityClient.mutate({
99
mutation: gql(MUTATION_PROC_CALIBRATION),
1010
variables: {
1111
fileId,

src/navability/services/Example.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { NavAbilityClient } from '../entities/NavAbilityClient';
44
import { Client } from '../entities/Client';
55
import { MUTATION_DEMOCANONICALHEXAGONAL } from '../graphql/QueriesDeprecated';
66

7-
export function demoCanonicalHexagonal(navAbilityClient: NavAbilityClient, client: Client) {
8-
navAbilityClient.mutate({
7+
export async function demoCanonicalHexagonal(navAbilityClient: NavAbilityClient, client: Client) {
8+
await navAbilityClient.mutate({
99
mutation: gql(MUTATION_DEMOCANONICALHEXAGONAL),
1010
variables: {
1111
client,

src/navability/services/Solve.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import { NavAbilityClient } from '../entities/NavAbilityClient';
44
import { Client, Scope } from '../entities/Client';
55
import { MUTATION_SOLVESESSION, MUTATION_SOLVEFEDERATED } from '../graphql/QueriesDeprecated';
66

7-
export function solveSession(navAbilityClient: NavAbilityClient, client: Client) {
8-
navAbilityClient.mutate({
7+
export async function solveSession(navAbilityClient: NavAbilityClient, client: Client) {
8+
await navAbilityClient.mutate({
99
mutation: gql(MUTATION_SOLVESESSION),
1010
variables: {
1111
client,
1212
},
1313
});
1414
}
1515

16-
export function solveFederated(navAbilityClient: NavAbilityClient, scope: Scope) {
17-
navAbilityClient.mutate({
16+
export async function solveFederated(navAbilityClient: NavAbilityClient, scope: Scope) {
17+
await navAbilityClient.mutate({
1818
mutation: gql(MUTATION_SOLVEFEDERATED),
1919
variables: {
2020
scope,

src/navability/services/Status.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { gql } from '@apollo/client';
2+
import { NavAbilityClient, GQL_GETSTATUSLATEST, GQL_GETSTATUSMESSAGES } from '../..';
3+
4+
export async function getStatusMessages(navAbilityClient: NavAbilityClient, id: string) {
5+
const response = await navAbilityClient.query({
6+
query: gql(GQL_GETSTATUSMESSAGES),
7+
variables: {
8+
id,
9+
},
10+
});
11+
if (response.data.errors) {
12+
throw Error(`Error: ${response.Data.errors[0]}`);
13+
} else {
14+
return response.data?.statusMessages || [];
15+
}
16+
}
17+
18+
export async function getStatusLatest(navAbilityClient: NavAbilityClient, id: string) {
19+
const response = await navAbilityClient.query({
20+
query: gql(GQL_GETSTATUSLATEST),
21+
variables: {
22+
id,
23+
},
24+
});
25+
if (response.data.errors) {
26+
throw Error(`Error: ${response.Data.errors[0]}`);
27+
} else {
28+
return response.data?.statusLatest || [];
29+
}
30+
}
31+
32+
export async function getStatusesLatest(navAbilityClient: NavAbilityClient, ids: string[]) {
33+
const statusPromises = ids.map((id) => getStatusLatest(navAbilityClient, id));
34+
return await Promise.all(statusPromises);
35+
}

src/navability/services/Utils.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { NavAbilityClient, getStatusesLatest } from '../..';
2+
3+
export function sleep(ms: number) {
4+
return new Promise((resolve) => setTimeout(resolve, ms));
5+
}
6+
7+
export async function waitForCompletion(
8+
navAbilityClient: NavAbilityClient,
9+
requestIds: string[],
10+
maxSeconds: number = 60,
11+
expectedStatuses: string[] = ['Complete', 'Failed'],
12+
exceptionMessage: string = 'Requests did not complete in time',
13+
) {
14+
let waitTime = maxSeconds;
15+
let tasksComplete = false;
16+
while (!tasksComplete) {
17+
const statuses = await getStatusesLatest(navAbilityClient, requestIds);
18+
tasksComplete = statuses.every((s: any) => expectedStatuses.includes(s.state));
19+
if (tasksComplete) {
20+
break;
21+
} else {
22+
await sleep(2000);
23+
waitTime -= 2;
24+
if (waitTime <= 0) {
25+
throw Error(exceptionMessage);
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)