Skip to content

Commit 868f65f

Browse files
authored
Add vue useProcedure hook (#4999)
# Description of Changes Tin. Just mechanically mirrors the `useReducer` hook just like it is for React Would be cool if this could get in #4998 # API and ABI breaking changes None. # Expected complexity level and risk 1 <!-- How complicated do you think these changes are? Grade on a scale from 1 to 5, where 1 is a trivial change, and 5 is a deep-reaching and complex change. This complexity rating applies not only to the complexity apparent in the diff, but also to its interactions with existing and future code. If you answered more than a 2, explain what is complex about the PR, and what other components it interacts with in potentially concerning ways. --> # Testing <!-- Describe any testing you've done, and any testing you'd like your reviewers to do, so that you're confident that all the changes work as expected! --> - [x] Works in my project
1 parent 451fc78 commit 868f65f

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

crates/bindings-typescript/src/vue/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './SpacetimeDBProvider.ts';
22
export { useSpacetimeDB } from './useSpacetimeDB.ts';
33
export { useTable } from './useTable.ts';
44
export { useReducer } from './useReducer.ts';
5+
export { useProcedure } from './useProcedure.ts';
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { shallowRef, watch, onUnmounted } from 'vue';
2+
import { useSpacetimeDB } from './useSpacetimeDB';
3+
import type { UntypedProcedureDef } from '../sdk/procedures';
4+
import type {
5+
ProcedureParamsType,
6+
ProcedureReturnType,
7+
} from '../sdk/type_utils';
8+
9+
export function useProcedure<ProcedureDef extends UntypedProcedureDef>(
10+
procedureDef: ProcedureDef
11+
): (
12+
...params: ProcedureParamsType<ProcedureDef>
13+
) => Promise<ProcedureReturnType<ProcedureDef>> {
14+
const conn = useSpacetimeDB();
15+
const procedureName = procedureDef.accessorName;
16+
17+
const queueRef = shallowRef<
18+
{
19+
params: ProcedureParamsType<ProcedureDef>;
20+
resolve: (val: any) => void;
21+
reject: (err: unknown) => void;
22+
}[]
23+
>([]);
24+
25+
const stopWatch = watch(
26+
() => conn.isActive,
27+
() => {
28+
const connection = conn.getConnection();
29+
if (!connection) return;
30+
31+
const fn = (connection.procedures as any)[procedureName] as (
32+
...p: ProcedureParamsType<ProcedureDef>
33+
) => Promise<ProcedureReturnType<ProcedureDef>>;
34+
if (queueRef.value.length) {
35+
const pending = queueRef.value.splice(0);
36+
for (const item of pending) {
37+
fn(...item.params).then(item.resolve, item.reject);
38+
}
39+
}
40+
},
41+
{ immediate: true }
42+
);
43+
44+
onUnmounted(() => {
45+
stopWatch();
46+
});
47+
48+
return (...params: ProcedureParamsType<ProcedureDef>) => {
49+
const connection = conn.getConnection();
50+
if (!connection) {
51+
return new Promise<ProcedureReturnType<ProcedureDef>>(
52+
(resolve, reject) => {
53+
queueRef.value.push({ params, resolve, reject });
54+
}
55+
);
56+
}
57+
const fn = (connection.procedures as any)[procedureName] as (
58+
...p: ProcedureParamsType<ProcedureDef>
59+
) => Promise<ProcedureReturnType<ProcedureDef>>;
60+
return fn(...params);
61+
};
62+
}

0 commit comments

Comments
 (0)