From fecfcf106dc3e472e7c26c2415a45c60ac289bb8 Mon Sep 17 00:00:00 2001 From: Ariel Gentile Date: Tue, 7 Mar 2023 13:10:14 -0300 Subject: [PATCH] fix(js): ensure scan is freed after fetchAll (#108) Signed-off-by: Ariel Gentile --- .../aries-askar-shared/src/store/Scan.ts | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/wrappers/javascript/aries-askar-shared/src/store/Scan.ts b/wrappers/javascript/aries-askar-shared/src/store/Scan.ts index a9065f8a3..a88601378 100644 --- a/wrappers/javascript/aries-askar-shared/src/store/Scan.ts +++ b/wrappers/javascript/aries-askar-shared/src/store/Scan.ts @@ -57,31 +57,33 @@ export class Scan { }) } - // Allow max of 256 per fetch operation - const chunk = this.limit ? Math.min(256, this.limit) : 256 - let recordCount = 0 - // Loop while limit not reached (or no limit specified) - while (!this.limit || recordCount < this.limit) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this._listHandle = await ariesAskar.scanNext({ scanHandle: this._handle! }) + try { + // Allow max of 256 per fetch operation + const chunk = this.limit ? Math.min(256, this.limit) : 256 + let recordCount = 0 + // Loop while limit not reached (or no limit specified) + while (!this.limit || recordCount < this.limit) { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this._listHandle = await ariesAskar.scanNext({ scanHandle: this._handle! }) - const list = new EntryList({ handle: this._listHandle }) + const list = new EntryList({ handle: this._listHandle }) - recordCount = recordCount + list.length - for (let index = 0; index < list.length; index++) { - const entry = list.getEntryByIndex(index) - cb(entry) - } + recordCount = recordCount + list.length + for (let index = 0; index < list.length; index++) { + const entry = list.getEntryByIndex(index) + cb(entry) + } - // If the number of records returned is less than chunk - // It means we reached the end of the iterator (no more records) - if (list.length < chunk) { - break + // If the number of records returned is less than chunk + // It means we reached the end of the iterator (no more records) + if (list.length < chunk) { + break + } } + } finally { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + ariesAskar.scanFree({ scanHandle: this._handle! }) } - - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - ariesAskar.scanFree({ scanHandle: this._handle! }) } public async fetchAll() {