Skip to content

Commit c0ecc42

Browse files
committed
Add debugger
1 parent 4cba1bb commit c0ecc42

File tree

9 files changed

+138
-16
lines changed

9 files changed

+138
-16
lines changed

.changeset/flat-candles-cry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@viamrobotics/svelte-sdk': patch
3+
---
4+
5+
Add debugger through `ViamProvider.debug`, `window.enableDebug`, and `window.disableDebug`

src/app.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ declare global {
88
// interface PageState {}
99
// interface Platform {}
1010
}
11+
12+
interface Window {
13+
enableDebug?: () => unknown;
14+
disableDebug?: () => unknown;
15+
}
1116
}
1217

1318
export {};

src/lib/components/provider.svelte

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,25 @@ import type { Snippet } from 'svelte';
33
import { QueryClientProvider, QueryClient } from '@tanstack/svelte-query';
44
import type { DialConf } from '@viamrobotics/sdk';
55
import InternalProvider from './internal-provider.svelte';
6+
import { enableDebug, disableDebug } from '../debug';
67
78
interface Props {
89
dialConfigs: Record<string, DialConf>;
910
client?: QueryClient;
1011
machineStatusRefetchInterval?: number;
12+
debug?: boolean;
1113
children: Snippet;
1214
}
1315
1416
let {
1517
dialConfigs,
1618
client = new QueryClient(),
1719
machineStatusRefetchInterval,
20+
debug = false,
1821
children,
1922
}: Props = $props();
23+
24+
$effect(() => (debug ? enableDebug() : disableDebug()));
2025
</script>
2126

2227
<QueryClientProvider {client}>

src/lib/debug.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
let enabled = false;
2+
3+
export const enableDebug = () => {
4+
enabled = true;
5+
};
6+
7+
export const disableDebug = () => {
8+
enabled = false;
9+
};
10+
11+
const debugLog = (...args: Parameters<typeof console.log>) => {
12+
if (enabled) {
13+
queueMicrotask(console.log.bind(console, ...args));
14+
}
15+
};
16+
17+
export const debugLogQuery = (
18+
index: number,
19+
type: 'REQ' | 'RES' | 'ERR',
20+
name: string | undefined,
21+
methodName: string,
22+
output: unknown
23+
) => {
24+
debugLog(
25+
`${index}\t${new Date().toISOString()}\t${type} \t${name ?? 'unknown'}\t${methodName}\n\t${JSON.stringify(output, null, 2)}`
26+
);
27+
};
28+
29+
window.enableDebug = enableDebug;
30+
window.disableDebug = disableDebug;

src/lib/hooks/create-resource-mutation.svelte.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
ResolvedReturnType,
77
} from './create-resource-query.svelte';
88
import { fromStore, toStore } from 'svelte/store';
9+
import { debugLogQuery } from '$lib/debug';
910

1011
export const createResourceMutation = <T extends Resource, K extends keyof T>(
1112
client: { current: T | undefined },
@@ -14,14 +15,19 @@ export const createResourceMutation = <T extends Resource, K extends keyof T>(
1415
type MutArgs = ArgumentsType<T[K]>;
1516
type MutReturn = ResolvedReturnType<T[K]>;
1617

18+
const name = $derived(client.current?.name);
19+
const methodName = $derived(String(method));
20+
21+
let index = 0;
22+
1723
const mutationOptions = $derived({
1824
mutationKey: [
1925
'viam-svelte-sdk',
2026
'partID',
2127
(client.current as T & { partID: string })?.partID,
2228
'resource',
23-
client.current?.name,
24-
String(method),
29+
name,
30+
methodName,
2531
],
2632
mutationFn: async (request) => {
2733
const clientFunc = client.current?.[method];
@@ -32,7 +38,22 @@ export const createResourceMutation = <T extends Resource, K extends keyof T>(
3238
);
3339
}
3440

35-
return clientFunc.apply(client.current, request) as Promise<MutReturn>;
41+
debugLogQuery(index, 'REQ', name, methodName, request);
42+
43+
try {
44+
const result = (await clientFunc.apply(
45+
client.current,
46+
request
47+
)) as Promise<MutReturn>;
48+
49+
debugLogQuery(index++, 'RES', name, methodName, result);
50+
51+
return result;
52+
} catch (error) {
53+
debugLogQuery(index++, 'ERR', name, methodName, error);
54+
55+
throw error;
56+
}
3657
},
3758
} satisfies MutationOptions<MutReturn, Error, MutArgs>);
3859

src/lib/hooks/create-resource-query.svelte.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
import type { Resource } from '@viamrobotics/sdk';
77
import { toStore, fromStore } from 'svelte/store';
88
import { usePolling } from './use-polling.svelte';
9+
import { debugLogQuery } from '../debug';
910

1011
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1112
export type ArgumentsType<T> = T extends (...args: infer U) => any ? U : never;
@@ -46,15 +47,20 @@ export const createResourceQuery = <T extends Resource, K extends keyof T>(
4647
);
4748
const _args = $derived(typeof args === 'function' ? args() : args);
4849

50+
const name = $derived(client.current?.name);
51+
const methodName = $derived(String(method));
52+
53+
let index = 0;
54+
4955
const queryOptions = $derived(
5056
createQueryOptions({
5157
queryKey: [
5258
'viam-svelte-sdk',
5359
'partID',
5460
(client.current as T & { partID: string })?.partID,
5561
'resource',
56-
client.current?.name,
57-
String(method),
62+
name,
63+
methodName,
5864
...(_args ? [_args] : []),
5965
],
6066
enabled: client.current !== undefined && _options?.enabled !== false,
@@ -68,9 +74,20 @@ export const createResourceQuery = <T extends Resource, K extends keyof T>(
6874
);
6975
}
7076

71-
return clientFunc?.apply(client.current, _args) as Promise<
72-
ResolvedReturnType<T[K]>
73-
>;
77+
debugLogQuery(index, 'REQ', name, methodName, _args);
78+
79+
try {
80+
const result = (await clientFunc?.apply(
81+
client.current,
82+
_args
83+
)) as Promise<ResolvedReturnType<T[K]>>;
84+
85+
debugLogQuery(index++, 'RES', name, methodName, result);
86+
return result;
87+
} catch (error) {
88+
debugLogQuery(index++, 'ERR', name, methodName, error);
89+
throw error;
90+
}
7491
},
7592
..._options,
7693
refetchInterval: false,

src/lib/hooks/create-robot-mutation.svelte.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
ResolvedReturnType,
77
} from './create-resource-query.svelte';
88
import { fromStore, toStore } from 'svelte/store';
9+
import { debugLogQuery } from '$lib/debug';
910

1011
export const createRobotMutation = <T extends RobotClient, K extends keyof T>(
1112
client: { current: T | undefined },
@@ -14,13 +15,17 @@ export const createRobotMutation = <T extends RobotClient, K extends keyof T>(
1415
type MutArgs = ArgumentsType<T[K]>;
1516
type MutReturn = ResolvedReturnType<T[K]>;
1617

18+
const methodName = $derived(String(method));
19+
20+
let index = 0;
21+
1722
const mutationOptions = $derived({
1823
mutationKey: [
1924
'viam-svelte-sdk',
2025
'partID',
2126
(client.current as T & { partID: string })?.partID,
2227
'robotClient',
23-
String(method),
28+
methodName,
2429
],
2530
mutationFn: async (request) => {
2631
const clientFunc = client.current?.[method];
@@ -31,7 +36,21 @@ export const createRobotMutation = <T extends RobotClient, K extends keyof T>(
3136
);
3237
}
3338

34-
return clientFunc.apply(client.current, request) as Promise<MutReturn>;
39+
debugLogQuery(index, 'REQ', 'robot', methodName, request);
40+
41+
try {
42+
const result = (await clientFunc.apply(
43+
client.current,
44+
request
45+
)) as Promise<MutReturn>;
46+
debugLogQuery(index++, 'RES', 'robot', methodName, result);
47+
48+
return result;
49+
} catch (error) {
50+
debugLogQuery(index++, 'ERR', 'robot', methodName, error);
51+
52+
throw error;
53+
}
3554
},
3655
} satisfies MutationOptions<MutReturn, Error, MutArgs>);
3756

src/lib/hooks/create-robot-query.svelte.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import type { RobotClient } from '@viamrobotics/sdk';
88
import { toStore, fromStore } from 'svelte/store';
99
import { usePolling } from './use-polling.svelte';
10+
import { debugLogQuery } from '$lib/debug';
1011

1112
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1213
export type ArgumentsType<T> = T extends (...args: infer U) => any ? U : never;
@@ -47,14 +48,18 @@ export const createRobotQuery = <T extends RobotClient, K extends keyof T>(
4748
);
4849
const _args = $derived(typeof args === 'function' ? args() : args);
4950

51+
const methodName = $derived(String(method));
52+
53+
let index = 0;
54+
5055
const queryOptions = $derived(
5156
createQueryOptions({
5257
queryKey: [
5358
'viam-svelte-sdk',
5459
'partID',
5560
(client.current as T & { partID: string })?.partID,
5661
'robotClient',
57-
String(method),
62+
methodName,
5863
...(_args ? [_args] : []),
5964
],
6065
enabled: client.current !== undefined && _options?.enabled !== false,
@@ -68,10 +73,22 @@ export const createRobotQuery = <T extends RobotClient, K extends keyof T>(
6873
);
6974
}
7075

71-
// Call entity.resource.func(args).
72-
return clientFunc?.apply(client.current, _args) as Promise<
73-
ResolvedReturnType<T[K]>
74-
>;
76+
debugLogQuery(index, 'REQ', 'robot', methodName, _args);
77+
78+
try {
79+
const result = (await clientFunc?.apply(
80+
client.current,
81+
_args
82+
)) as Promise<ResolvedReturnType<T[K]>>;
83+
84+
debugLogQuery(index++, 'RES', 'robot', methodName, result);
85+
86+
return result;
87+
} catch (error) {
88+
debugLogQuery(index++, 'ERR', 'robot', methodName, error);
89+
90+
throw error;
91+
}
7592
},
7693
..._options,
7794
refetchInterval: false,

src/routes/+layout.svelte

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ let { children }: Props = $props();
4343
{/each}
4444
</div>
4545

46-
<ViamProvider {dialConfigs}>
46+
<ViamProvider
47+
{dialConfigs}
48+
debug
49+
>
4750
<Parts />
4851
{@render children()}
4952
<SvelteQueryDevtools />

0 commit comments

Comments
 (0)