import type { QueryResult } from "@commands/types"; import type { Requester } from "@http"; import type { QueryCommandPayload } from "../query/types"; import { ResumeQueryCommand } from "./resume"; import { StartResumableQueryCommand, type StartResumableQueryResult } from "./start"; import { StopResumableQueryCommand } from "./stop"; type NAMESPACE = string; export type ResumableQueryEndpointVariants = | `resumable-query` | `resumable-query-data` | `resumable-query/${NAMESPACE}` | `resumable-query-data/${NAMESPACE}`; export type ResumableQueryPayload = { maxIdle: number } & QueryCommandPayload; export class ResumableQuery> { private uuid?: string; start: () => Promise>; fetchNext: (additionalK: number) => Promise; stop: () => Promise; constructor(payload: ResumableQueryPayload, client: Requester, namespace?: string) { this.start = async (): Promise> => { const result = await new StartResumableQueryCommand(payload, namespace).exec( client ); this.uuid = result.uuid; return result; }; this.fetchNext = (additionalK: number): Promise[]> => { if (!this.uuid) { throw new Error( "The resumable query has already been stopped. Please start another resumable query." ); } return new ResumeQueryCommand({ uuid: this.uuid, additionalK }).exec(client); }; this.stop = async (): Promise => { if (!this.uuid) { throw new Error("Resumable query has not been started. Call start() first."); } const result = await new StopResumableQueryCommand({ uuid: this.uuid }).exec(client); this.uuid = ""; return result; }; } }