diff --git a/apps/demo-app/pages/index.tsx b/apps/demo-app/pages/index.tsx index 56482c0..71b4b66 100644 --- a/apps/demo-app/pages/index.tsx +++ b/apps/demo-app/pages/index.tsx @@ -75,6 +75,7 @@ export function Index() { model_name: 'stabilityai/stable-diffusion-xl-base-1.0', image_size: 'square_hd', }, + pollInterval: 5000, // Default is 1000 (every 1s) onQueueUpdate(update) { setElapsedTime(Date.now() - start); if ( diff --git a/libs/client/src/function.ts b/libs/client/src/function.ts index 1bae678..2332898 100644 --- a/libs/client/src/function.ts +++ b/libs/client/src/function.ts @@ -112,16 +112,68 @@ export async function run( return await responseHandler(response); } +/** + * Options for subscribing to the request queue. + */ type QueueSubscribeOptions = { + /** + * The interval (in milliseconds) at which to poll for updates. + * If not provided, a default value of `1000` will be used. + */ pollInterval?: number; + + /** + * Callback function that is called when a request is enqueued. + * @param requestId - The unique identifier for the enqueued request. + */ onEnqueue?: (requestId: string) => void; + + /** + * Callback function that is called when the status of the queue changes. + * @param status - The current status of the queue. + */ onQueueUpdate?: (status: QueueStatus) => void; }; +/** + * Represents a request queue with methods for submitting requests, + * checking their status, retrieving results, and subscribing to updates. + */ interface Queue { + /** + * Submits a request to the queue. + * + * @param id - The ID or URL of the function web endpoint. + * @param options - Options to configure how the request is run. + * @returns A promise that resolves to the result of enqueuing the request. + */ submit(id: string, options: RunOptions): Promise; + + /** + * Retrieves the status of a specific request in the queue. + * + * @param id - The ID or URL of the function web endpoint. + * @param requestId - The unique identifier for the enqueued request. + * @returns A promise that resolves to the status of the request. + */ status(id: string, requestId: string): Promise; + + /** + * Retrieves the result of a specific request from the queue. + * + * @param id - The ID or URL of the function web endpoint. + * @param requestId - The unique identifier for the enqueued request. + * @returns A promise that resolves to the result of the request. + */ result(id: string, requestId: string): Promise; + + /** + * Subscribes to updates for a specific request in the queue. + * + * @param id - The ID or URL of the function web endpoint. + * @param options - Options to configure how the request is run and how updates are received. + * @returns A promise that resolves to the result of the request once it's completed. + */ subscribe( id: string, options: RunOptions & QueueSubscribeOptions @@ -185,7 +237,7 @@ export const queue: Queue = { reject(error); } }; - timeoutId = setTimeout(poll, pollInterval); + poll().catch(reject); }); }, };