Skip to content

Commit 93ad1ed

Browse files
authored
Implement async server-side search (#1045)
1 parent 62f7c12 commit 93ad1ed

File tree

3 files changed

+551
-375
lines changed

3 files changed

+551
-375
lines changed

src/api/atelier.d.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export interface Response<T = any> {
1515
status: ResponseStatus;
1616
console: string[];
1717
result: T;
18+
/** Value of the `Retry-After` response header, if present */
19+
retryafter?: string;
1820
}
1921

2022
interface ServerInfoFeature {
@@ -77,3 +79,30 @@ export interface DeleteStatus {
7779
db: string;
7880
status: string;
7981
}
82+
83+
interface AsyncCompileRequest {
84+
request: "compile";
85+
documents: string[];
86+
source?: boolean;
87+
flags?: string;
88+
}
89+
90+
interface AsyncSearchRequest {
91+
request: "search";
92+
query: string;
93+
regex?: boolean;
94+
project?: string;
95+
word?: boolean;
96+
case?: boolean;
97+
wild?: boolean;
98+
documents?: string;
99+
system?: boolean;
100+
generated?: boolean;
101+
mapped?: boolean;
102+
max?: number;
103+
include?: string;
104+
exclude?: string;
105+
console: false;
106+
}
107+
108+
export type AsyncRequest = AsyncCompileRequest | AsyncSearchRequest;

src/api/index.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ export class AtelierAPI {
416416
} else if (originalPath && /^[^/]+\/work\/[^/]+$/.test(originalPath)) {
417417
// This is a GET or DELETE /work request, so we need to check the Retry-After header
418418
if (response.headers.has("Retry-After")) {
419-
data.result.retryafter = response.headers.get("Retry-After");
419+
data.retryafter = response.headers.get("Retry-After");
420420
}
421421
}
422422

@@ -617,30 +617,32 @@ export class AtelierAPI {
617617
}
618618

619619
// v1+
620-
private queueAsync(request: any): Promise<Atelier.Response> {
620+
public queueAsync(request: Atelier.AsyncRequest): Promise<Atelier.Response> {
621621
return this.request(1, "POST", `${this.ns}/work`, request);
622622
}
623623

624624
// v1+
625-
private pollAsync(id: string): Promise<Atelier.Response> {
625+
public pollAsync(id: string): Promise<Atelier.Response> {
626626
return this.request(1, "GET", `${this.ns}/work/${id}`);
627627
}
628628

629629
// v1+
630-
private cancelAsync(id: string): Promise<Atelier.Response> {
630+
public cancelAsync(id: string): Promise<Atelier.Response> {
631631
return this.request(1, "DELETE", `${this.ns}/work/${id}`);
632632
}
633633

634634
/**
635635
* Calls `cancelAsync()` repeatedly until the cancellation is confirmed.
636636
* The wait time between requests is 1 second.
637637
*/
638-
private async verifiedCancel(id: string): Promise<Atelier.Response> {
639-
outputChannel.appendLine(
640-
"\nWARNING: Compilation was cancelled. Partially-compiled documents may result in unexpected behavior."
641-
);
638+
public async verifiedCancel(id: string, compile = true): Promise<Atelier.Response> {
639+
if (compile) {
640+
outputChannel.appendLine(
641+
"\nWARNING: Compilation was cancelled. Partially-compiled documents may result in unexpected behavior."
642+
);
643+
}
642644
let cancelResp = await this.cancelAsync(id);
643-
while (cancelResp.result.retryafter) {
645+
while (cancelResp.retryafter) {
644646
await new Promise((resolve) => {
645647
setTimeout(resolve, 1000);
646648
});
@@ -659,7 +661,7 @@ export class AtelierAPI {
659661
// The user cancelled the request, so cancel it on the server
660662
return this.verifiedCancel(id);
661663
}
662-
if (pollResp.result.retryafter) {
664+
if (pollResp.retryafter) {
663665
await new Promise((resolve) => {
664666
setTimeout(resolve, wait);
665667
});

0 commit comments

Comments
 (0)