Skip to content

Commit

Permalink
fix(gainmaploader): fix GainMapLoader progress handler not being calc…
Browse files Browse the repository at this point in the history
…ulated correctly
  • Loading branch information
daniele-pelagatti committed Nov 15, 2023
1 parent 1f0e1ab commit a8e556a
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions src/decode/loaders/GainMapLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,24 @@ export class GainMapLoader extends LoaderBase<[string, string, string]> {
}
}

const progressHandler = (e: ProgressEvent) => {
let sdrLengthComputable = true
let sdrTotal = 0
let sdrLoaded = 0

let gainMapLengthComputable = true
let gainMapTotal = 0
let gainMapLoaded = 0

let metadataLengthComputable = true
let metadataTotal = 0
let metadataLoaded = 0

const progressHandler = () => {
if (typeof onProgress === 'function') {
// TODO: progress / 3
const total = sdrTotal + gainMapTotal + metadataTotal
const loaded = sdrLoaded + gainMapLoaded + metadataLoaded
const lengthComputable = sdrLengthComputable && gainMapLengthComputable && metadataLengthComputable
onProgress(new ProgressEvent('progress', { lengthComputable, loaded, total }))
}
}

Expand All @@ -101,7 +116,12 @@ export class GainMapLoader extends LoaderBase<[string, string, string]> {
if (typeof buffer === 'string') throw new Error('Invalid sdr buffer')
sdr = buffer
loadCheck()
}, progressHandler, onError)
}, (e: ProgressEvent) => {
sdrLengthComputable = e.lengthComputable
sdrLoaded = e.loaded
sdrTotal = e.total
progressHandler()
}, onError)

const gainMapLoader = new FileLoader(this.manager)
gainMapLoader.setResponseType('arraybuffer')
Expand All @@ -112,7 +132,12 @@ export class GainMapLoader extends LoaderBase<[string, string, string]> {
if (typeof buffer === 'string') throw new Error('Invalid gainmap buffer')
gainMap = buffer
loadCheck()
}, progressHandler, onError)
}, (e: ProgressEvent) => {
gainMapLengthComputable = e.lengthComputable
gainMapLoaded = e.loaded
gainMapTotal = e.total
progressHandler()
}, onError)

const metadataLoader = new FileLoader(this.manager)
// metadataLoader.setResponseType('json')
Expand All @@ -123,7 +148,12 @@ export class GainMapLoader extends LoaderBase<[string, string, string]> {
if (typeof json !== 'string') throw new Error('Invalid metadata string')
metadata = JSON.parse(json)
loadCheck()
}, progressHandler, onError)
}, (e: ProgressEvent) => {
metadataLengthComputable = e.lengthComputable
metadataLoaded = e.loaded
metadataTotal = e.total
progressHandler()
}, onError)

return quadRenderer
}
Expand Down

0 comments on commit a8e556a

Please sign in to comment.