Spaces:
Sleeping
Sleeping
File size: 1,644 Bytes
90cbf22 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import { ConvexReactClient, useConvex } from 'convex/react';
import { InputArgs, InputReturnValue, Inputs } from '../../convex/aiTown/inputs';
import { api } from '../../convex/_generated/api';
import { Id } from '../../convex/_generated/dataModel';
export async function waitForInput(convex: ConvexReactClient, inputId: Id<'inputs'>) {
const watch = convex.watchQuery(api.aiTown.main.inputStatus, { inputId });
let result = watch.localQueryResult();
// The result's undefined if the query's loading and null if the input hasn't
// been processed yet.
if (result === undefined || result === null) {
let dispose: undefined | (() => void);
try {
await new Promise<void>((resolve, reject) => {
dispose = watch.onUpdate(() => {
try {
result = watch.localQueryResult();
} catch (e: any) {
reject(e);
return;
}
if (result !== undefined && result !== null) {
resolve();
}
});
});
} finally {
if (dispose) {
dispose();
}
}
}
if (!result) {
throw new Error(`Input ${inputId} was never processed.`);
}
if (result.kind === 'error') {
throw new Error(result.message);
}
return result.value;
}
export function useSendInput<Name extends keyof Inputs>(
engineId: Id<'engines'>,
name: Name,
): (args: InputArgs<Name>) => Promise<InputReturnValue<Name>> {
const convex = useConvex();
return async (args) => {
const inputId = await convex.mutation(api.world.sendWorldInput, { engineId, name, args });
return await waitForInput(convex, inputId);
};
}
|