Skip to content

Commit

Permalink
fix(js): ensure scan is freed after fetchAll (#108)
Browse files Browse the repository at this point in the history
Signed-off-by: Ariel Gentile <gentilester@gmail.com>
  • Loading branch information
genaris authored Mar 7, 2023
1 parent 1a5cb16 commit 15a748a
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions wrappers/javascript/aries-askar-shared/src/store/Scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 15a748a

Please sign in to comment.