Skip to content

Commit

Permalink
fix(ts): last progress event possibly not trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
jhen0409 committed Aug 10, 2023
1 parent 067ba10 commit 0342dc1
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,29 +133,41 @@ export class WhisperContext {

const { onProgress, ...rest } = options
let progressListener: any
let lastProgress: number = 0
if (onProgress) {
progressListener = EventEmitter.addListener(
EVENT_ON_TRANSCRIBE_PROGRESS,
(evt: TranscribeProgressNativeEvent) => {
const { contextId, progress } = evt
if (contextId !== this.id || evt.jobId !== jobId) return
lastProgress = progress
onProgress(progress)
},
)
}
const removeProgressListener = () => {
if (progressListener) {
progressListener.remove()
progressListener = null
}
}
return {
stop: async () => {
await RNWhisper.abortTranscribe(this.id, jobId)
progressListener?.remove()
removeProgressListener()
},
promise: RNWhisper.transcribeFile(this.id, jobId, path, {
...rest,
onProgress: !!onProgress
}).then((result) => {
progressListener?.remove()
removeProgressListener()
if (!result.isAborted && lastProgress !== 100) {
// Handle the case that the last progress event is not triggered
onProgress?.(100)
}
return result
}).catch((e) => {
progressListener?.remove()
removeProgressListener()
throw e
}),
}
Expand Down

0 comments on commit 0342dc1

Please sign in to comment.