Skip to content

Commit

Permalink
fix: polling status call
Browse files Browse the repository at this point in the history
  • Loading branch information
drochetti committed Sep 24, 2023
1 parent f075fd9 commit 2aac7fc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions apps/demo-app/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
54 changes: 53 additions & 1 deletion libs/client/src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,68 @@ export async function run<Input, Output>(
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<Input>(id: string, options: RunOptions<Input>): Promise<EnqueueResult>;

/**
* 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<QueueStatus>;

/**
* 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<Output>(id: string, requestId: string): Promise<Output>;

/**
* 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<Input, Output>(
id: string,
options: RunOptions<Input> & QueueSubscribeOptions
Expand Down Expand Up @@ -185,7 +237,7 @@ export const queue: Queue = {
reject(error);
}
};
timeoutId = setTimeout(poll, pollInterval);
poll().catch(reject);
});
},
};

0 comments on commit 2aac7fc

Please sign in to comment.