File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -10,13 +10,18 @@ const DEFAULT_OPTIONS: AudioRecorderOptions = {
1010 channelCount : 1 ,
1111}
1212
13- let workletModulePromise : Promise < void > | null = null
13+ const workletModulePromises = new WeakMap < AudioContext , Promise < void > > ( )
1414
1515function ensureWorkletLoaded ( ctx : AudioContext ) : Promise < void > {
16- if ( ! workletModulePromise ) {
17- workletModulePromise = ctx . audioWorklet . addModule ( '/audio-worklet-processor.js' )
16+ const existingPromise = workletModulePromises . get ( ctx )
17+
18+ if ( existingPromise ) {
19+ return existingPromise
1820 }
19- return workletModulePromise
21+
22+ const promise = ctx . audioWorklet . addModule ( '/audio-worklet-processor.js' )
23+ workletModulePromises . set ( ctx , promise )
24+ return promise
2025}
2126
2227export function downsampleAndConvert ( input : Float32Array , inputRate : number , targetRate : number ) : Int16Array {
@@ -164,7 +169,6 @@ export class AudioRecorder {
164169 }
165170 this . source . connect ( this . workletNode )
166171 } catch ( error ) {
167- workletModulePromise = null
168172 this . audioContext . close ( )
169173 this . audioContext = null
170174 throw new Error ( 'Failed to load audio worklet processor' , { cause : error } )
You can’t perform that action at this time.
0 commit comments