Skip to content

Commit

Permalink
handling pagination in getting the sketches (arduino#875)
Browse files Browse the repository at this point in the history
Co-authored-by: Umberto Sgueglia <umberto.sgueglia@external.fcagroup.com>
  • Loading branch information
ulemons and Umberto Sgueglia authored Mar 8, 2022
1 parent bfe6835 commit b416e5f
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions arduino-ide-extension/src/browser/create/create-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,29 @@ export class CreateApi {
return result;
}

async sketches(): Promise<Create.Sketch[]> {
async sketches(limit = 50): Promise<Create.Sketch[]> {
const url = new URL(`${this.domain()}/sketches`);
url.searchParams.set('user_id', 'me');
url.searchParams.set('limit', limit.toString());
const headers = await this.headers();
const result = await this.run<{ sketches: Create.Sketch[] }>(url, {
method: 'GET',
headers,
});
const result: { sketches: Create.Sketch[] } = { sketches: [] };

let partialSketches: Create.Sketch[] = [];
let currentOffset = 0;
do {
url.searchParams.set('offset', currentOffset.toString());
partialSketches = (
await this.run<{ sketches: Create.Sketch[] }>(url, {
method: 'GET',
headers,
})
).sketches;
if (partialSketches.length != 0) {
result.sketches = result.sketches.concat(partialSketches);
}
currentOffset = currentOffset + limit;
} while (partialSketches.length != 0);

result.sketches.forEach((sketch) => this.sketchCache.addSketch(sketch));
return result.sketches;
}
Expand Down

0 comments on commit b416e5f

Please sign in to comment.