-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathtask.ts
More file actions
197 lines (175 loc) · 6.13 KB
/
task.ts
File metadata and controls
197 lines (175 loc) · 6.13 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { MeilisearchTaskTimeOutError } from "./errors/index.js";
import type {
WaitOptions,
TasksOrBatchesQuery,
TasksResults,
Task,
DeleteOrCancelTasksQuery,
EnqueuedTask,
EnqueuedTaskPromise,
TaskUidOrEnqueuedTask,
ExtraRequestInit,
} from "./types/index.js";
import type { HttpRequests } from "./http-requests.js";
/**
* Used to identify whether an error is a timeout error in
* {@link TaskClient.waitForTask}.
*/
const TIMEOUT_ID = Symbol("<task timeout>");
/**
* @returns A function which defines an extra function property on a
* {@link Promise}, which resolves to {@link EnqueuedTask}, which awaits it and
* resolves to a {@link Task}.
*/
function getWaitTaskApplier(
taskClient: TaskClient,
): (enqueuedTaskPromise: Promise<EnqueuedTask>) => EnqueuedTaskPromise {
return function (
enqueuedTaskPromise: Promise<EnqueuedTask>,
): EnqueuedTaskPromise {
return Object.defineProperty(
enqueuedTaskPromise,
"waitTask" satisfies keyof Pick<EnqueuedTaskPromise, "waitTask">,
{
async value(waitOptions?: WaitOptions): Promise<Task> {
return await taskClient.waitForTask(
await enqueuedTaskPromise,
waitOptions,
);
},
},
) as EnqueuedTaskPromise;
};
}
const getTaskUid = (taskUidOrEnqueuedTask: TaskUidOrEnqueuedTask): number =>
typeof taskUidOrEnqueuedTask === "number"
? taskUidOrEnqueuedTask
: taskUidOrEnqueuedTask.taskUid;
/**
* Class for handling tasks.
*
* @see {@link https://www.meilisearch.com/docs/reference/api/tasks}
*/
export class TaskClient {
readonly #httpRequest: HttpRequests;
readonly #defaultTimeout: number;
readonly #defaultInterval: number;
readonly #applyWaitTask: ReturnType<typeof getWaitTaskApplier>;
constructor(httpRequest: HttpRequests, defaultWaitOptions?: WaitOptions) {
this.#httpRequest = httpRequest;
this.#defaultTimeout = defaultWaitOptions?.timeout ?? 5_000;
this.#defaultInterval = defaultWaitOptions?.interval ?? 50;
this.#applyWaitTask = getWaitTaskApplier(this);
}
/** {@link https://www.meilisearch.com/docs/reference/api/tasks#get-one-task} */
async getTask(
uid: number,
// TODO: Need to do this for all other methods: https://github.com/meilisearch/meilisearch-js/issues/1476
extraRequestInit?: ExtraRequestInit,
): Promise<Task> {
return await this.#httpRequest.get({
path: `tasks/${uid}`,
extraRequestInit,
});
}
/** {@link https://www.meilisearch.com/docs/reference/api/tasks#get-tasks} */
async getTasks(params?: TasksOrBatchesQuery): Promise<TasksResults> {
return await this.#httpRequest.get({ path: "tasks", params });
}
/**
* Wait for an enqueued task to be processed. This is done through polling
* with {@link TaskClient.getTask}.
*
* @remarks
* If an {@link EnqueuedTask} needs to be awaited instantly, it is recommended
* to instead use {@link EnqueuedTaskPromise.waitTask}, which is available on
* any method that returns an {@link EnqueuedTaskPromise}.
*/
async waitForTask(
taskUidOrEnqueuedTask: TaskUidOrEnqueuedTask,
options?: WaitOptions,
): Promise<Task> {
const taskUid = getTaskUid(taskUidOrEnqueuedTask);
const timeout = options?.timeout ?? this.#defaultTimeout;
const interval = options?.interval ?? this.#defaultInterval;
const ac = timeout > 0 ? new AbortController() : null;
const toId =
ac !== null ? setTimeout(() => ac.abort(TIMEOUT_ID), timeout) : undefined;
try {
for (;;) {
const task = await this.getTask(taskUid, { signal: ac?.signal });
if (task.status !== "enqueued" && task.status !== "processing") {
clearTimeout(toId);
return task;
}
if (interval > 0) {
await new Promise((resolve) => setTimeout(resolve, interval));
}
}
} catch (error) {
throw Object.is((error as Error).cause, TIMEOUT_ID)
? new MeilisearchTaskTimeOutError(taskUid, timeout)
: error;
}
}
/**
* Lazily wait for multiple enqueued tasks to be processed.
*
* @remarks
* In this case {@link WaitOptions.timeout} is the maximum time to wait for any
* one task, not for all of the tasks to complete.
*/
async *waitForTasksIter(
taskUidsOrEnqueuedTasks:
| Iterable<TaskUidOrEnqueuedTask>
| AsyncIterable<TaskUidOrEnqueuedTask>,
options?: WaitOptions,
): AsyncGenerator<Task, void, undefined> {
for await (const taskUidOrEnqueuedTask of taskUidsOrEnqueuedTasks) {
yield await this.waitForTask(taskUidOrEnqueuedTask, options);
}
}
/** Wait for multiple enqueued tasks to be processed. */
async waitForTasks(
...params: Parameters<typeof this.waitForTasksIter>
): Promise<Task[]> {
const tasks: Task[] = [];
for await (const task of this.waitForTasksIter(...params)) {
tasks.push(task);
}
return tasks;
}
/** {@link https://www.meilisearch.com/docs/reference/api/tasks#cancel-tasks} */
cancelTasks(params: DeleteOrCancelTasksQuery): EnqueuedTaskPromise {
return this.#applyWaitTask(
this.#httpRequest.post({ path: "tasks/cancel", params }),
);
}
/** {@link https://www.meilisearch.com/docs/reference/api/tasks#delete-tasks} */
deleteTasks(params: DeleteOrCancelTasksQuery): EnqueuedTaskPromise {
return this.#applyWaitTask(
this.#httpRequest.delete({ path: "tasks", params }),
);
}
}
type PickedHttpRequestMethods = Pick<
HttpRequests,
"post" | "put" | "patch" | "delete"
>;
export type HttpRequestsWithEnqueuedTaskPromise = {
[TKey in keyof PickedHttpRequestMethods]: (
...params: Parameters<PickedHttpRequestMethods[TKey]>
) => EnqueuedTaskPromise;
};
export function getHttpRequestsWithEnqueuedTaskPromise(
httpRequest: HttpRequests,
taskClient: TaskClient,
): HttpRequestsWithEnqueuedTaskPromise {
const applyWaitTask = getWaitTaskApplier(taskClient);
return {
post: (...params) => applyWaitTask(httpRequest.post(...params)),
put: (...params) => applyWaitTask(httpRequest.put(...params)),
patch: (...params) => applyWaitTask(httpRequest.patch(...params)),
delete: (...params) => applyWaitTask(httpRequest.delete(...params)),
};
}