Skip to content

Commit

Permalink
Throw error on catching memoized async error
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Jul 30, 2023
1 parent 56d1cb9 commit 55e5402
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 21 deletions.
24 changes: 11 additions & 13 deletions src/cramFile/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,28 +235,26 @@ export default class CramFile {
}

/**
* @returns {Promise[number]} the number of containers in the file
* @returns the number of containers in the file
*/
async containerCount(): Promise<number | undefined> {
async containerCount() {
const sectionParsers = await this.getSectionParsers()
const { size: fileSize } = await this.file.stat()
const { cramContainerHeader1 } = sectionParsers

let containerCount = 0
let position = sectionParsers.cramFileDefinition.maxLength
while (position + cramContainerHeader1.maxLength + 8 < fileSize) {
const currentHeader = await this.getContainerAtPosition(
position,
).getHeader()
if (!currentHeader) {
const currHeader = await this.getContainerAtPosition(position).getHeader()
if (!currHeader) {
break
}
// if this is the first container, read all the blocks in the
// container, because we cannot trust the container
// header's given length due to a bug somewhere in htslib
// if this is the first container, read all the blocks in the container,
// because we cannot trust the container header's given length due to a
// bug somewhere in htslib
if (containerCount === 0) {
position = currentHeader._endPosition
for (let j = 0; j < currentHeader.numBlocks; j += 1) {
position = currHeader._endPosition
for (let j = 0; j < currHeader.numBlocks; j += 1) {
const block = await this.readBlock(position)
if (block === undefined) {
return undefined
Expand All @@ -265,7 +263,7 @@ export default class CramFile {
}
} else {
// otherwise, just traverse to the next container using the container's length
position += currentHeader._size + currentHeader.length
position += currHeader._size + currHeader.length
}
containerCount += 1
}
Expand Down Expand Up @@ -357,7 +355,7 @@ export default class CramFile {
}
}

async readBlock(position: number): Promise<CramFileBlock | undefined> {
async readBlock(position: number) {
const { majorVersion } = await this.getDefinition()
const sectionParsers = await this.getSectionParsers()
const blockHeader = await this.readBlockHeader(position)
Expand Down
1 change: 1 addition & 0 deletions src/cramFile/sectionParsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ const versionedParsers = {
return { parser, maxLength: maxLengthFunc }
},

// eslint-disable-next-line @typescript-eslint/no-unused-vars
cramEncoding(majorVersion: number) {
const parser = new Parser()
.namely('cramEncoding')
Expand Down
2 changes: 1 addition & 1 deletion src/cramFile/slice/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export default class CramSlice {
constructor(
public container: CramContainer,
public containerPosition: number,
_unused: number,
public sliceSize: number,
) {
this.file = container.file
}
Expand Down
3 changes: 2 additions & 1 deletion src/cramFile/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,9 @@ export function tinyMemoize(_class: any, methodName: any) {
if (!(memoAttrName in this)) {
const res = method.call(this)
this[memoAttrName] = res
Promise.resolve(res).catch(() => {
Promise.resolve(res).catch(e => {
delete this[memoAttrName]
throw e
})
}
return this[memoAttrName]
Expand Down
6 changes: 0 additions & 6 deletions test/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ async function loadTestJSON(filename) {
return JSON.parse(text)
}

function JSONstringifyOrder(obj, space) {
const allKeys = new Set()
JSON.stringify(obj, (key, value) => (allKeys.add(key), value))
return JSON.stringify(obj, Array.from(allKeys).sort(), space)
}

export function JsonClone(obj) {
return JSON.parse(JSON.stringify(obj))
}
Expand Down
1 change: 1 addition & 0 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ describe('CRAM reader', () => {
refSeqStart: 0,
})
const {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
content,
parsedContent: compressionBlockData,
...compressionBlock
Expand Down

0 comments on commit 55e5402

Please sign in to comment.