Skip to content

Commit

Permalink
Merge branch 'bcameron1231-v3-chunkedBatching' into version-3
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-rodgers committed Mar 8, 2024
2 parents 4a19bfa + 27ed506 commit a9b55f2
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions packages/graph/batching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ export function createBatch(base: IGraphQueryable, props?: IGraphBatchProps): [T
const completePromises: Promise<void>[] = [];
const requests: RequestRecord[] = [];
const batchId = getGUID();
const batchQuery = new BatchQueryable(base);
const refQuery = new BatchQueryable(base);
const batchQuery = new BatchQueryable(base);

const { maxRequests } = {
maxRequests: 30,
maxRequests: 20,
...props,
};

Expand All @@ -172,6 +172,7 @@ export function createBatch(base: IGraphQueryable, props?: IGraphBatchProps): [T
const requestsWorkingCopy = requests.slice();

// this is the root of our promise chain
let chunkIndex = 0;
while (requestsWorkingCopy.length > 0) {

const requestsChunk = requestsWorkingCopy.splice(0, maxRequests);
Expand All @@ -182,30 +183,17 @@ export function createBatch(base: IGraphQueryable, props?: IGraphBatchProps): [T

const response: ParsedGraphResponse = await graphPost(batchQuery, body(batchRequest));

return new Promise<void>((res, rej) => {

for (let index = 0; index < response.responses.length; index++) {
const [, , , resolve, reject] = requests[index + chunkIndex];
try {

for (let index = 0; index < response.responses.length; index++) {
const [, , , resolve, reject] = requests[index];
try {
resolve(response.responses[index]);
} catch (e) {
reject(e);
}
}

// this small delay allows the promises to resolve correctly in order by dropping this resolve behind
// the other work in the event loop. Feels hacky, but it works so 🤷
setTimeout(res, 0);

resolve(response.responses[index]);
} catch (e) {

setTimeout(() => rej(e), 0);
reject(e);
}

}).then(() => Promise.all(completePromises)).then(() => void (0));
}
chunkIndex += requestsChunk.length;
}
await Promise.all(completePromises).then(() => void (0));
};

const register = (instance: Queryable) => {
Expand Down

0 comments on commit a9b55f2

Please sign in to comment.