Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js): ensure scan is freed after fetchAll #108

Merged
merged 3 commits into from
Mar 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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