From b416e5f9e81ff2880527f1dcdadb348632977778 Mon Sep 17 00:00:00 2001 From: ulemons Date: Tue, 8 Mar 2022 17:11:18 +0100 Subject: [PATCH] handling pagination in getting the sketches (#875) Co-authored-by: Umberto Sgueglia --- .../src/browser/create/create-api.ts | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/arduino-ide-extension/src/browser/create/create-api.ts b/arduino-ide-extension/src/browser/create/create-api.ts index 8c2e4281a..77090071a 100644 --- a/arduino-ide-extension/src/browser/create/create-api.ts +++ b/arduino-ide-extension/src/browser/create/create-api.ts @@ -100,14 +100,29 @@ export class CreateApi { return result; } - async sketches(): Promise { + async sketches(limit = 50): Promise { 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; }