-
Notifications
You must be signed in to change notification settings - Fork 87
/
executes.ts
69 lines (56 loc) · 1.58 KB
/
executes.ts
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
import { API, APIRequest } from '../api';
import { ExecuteError } from '../errors';
import { getChainReturn, resolveExecuteTask } from '../utils/helpers';
export interface IExecutesPayload {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
response: any[];
errors: ExecuteError[];
}
export interface IExecutesOptions {
api: API;
method: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
queue: Record<string, any>[];
}
export const executeRequests = async (
api: API,
queue: APIRequest[],
): Promise<IExecutesPayload> => {
const out: IExecutesPayload = {
response: [],
errors: [],
};
if (queue.length === 0) {
return out;
}
while (queue.length > 0) {
const tasks = queue.splice(0, 25);
const code = getChainReturn(tasks.map(String));
try {
const response = await api.execute({ code });
resolveExecuteTask(tasks, response);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
out.response.push(...response.response);
out.errors.push(...response.errors);
} catch (error) {
for (const task of tasks) {
task.reject(error);
}
throw error;
}
}
return out;
};
export const executes = ({
api,
method,
queue,
}: IExecutesOptions): Promise<IExecutesPayload> => (
executeRequests(api, queue.map(params => (
new APIRequest({
api,
method,
params,
})
)))
);