Skip to content

Commit 7f89c85

Browse files
authored
only add audioworklet modules once (#381)
This Promise `return ac.audioWorklet.addModule(objectURL)` was throwing an error if the worklet with that name had already been registered on the page. We run into this issue when running p5 in "instance mode" (i.e. on the documentation examples at p5js.org).
1 parent 98ee5b7 commit 7f89c85

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/audioWorklet/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const moduleSources = [
66
];
77
const ac = p5sound.audiocontext;
88

9+
let initializedAudioWorklets = false;
10+
911
function loadAudioWorkletModules() {
1012
return Promise.all(moduleSources.map(function(moduleSrc) {
1113
const blob = new Blob([moduleSrc], { type: 'application/javascript' });
@@ -15,13 +17,16 @@ function loadAudioWorkletModules() {
1517
}
1618

1719
p5.prototype.registerMethod('init', function() {
20+
if (initializedAudioWorklets) return;
1821
// ensure that a preload function exists so that p5 will wait for preloads to finish
1922
if (!this.preload && !window.preload) {
2023
this.preload = function() {};
2124
}
25+
2226
// use p5's preload system to load necessary AudioWorklet modules before setup()
23-
this._preloadCount++;
27+
this._incrementPreload();
2428
const onWorkletModulesLoad = function() {
29+
initializedAudioWorklets = true;
2530
this._decrementPreload();
2631
}.bind(this);
2732
loadAudioWorkletModules().then(onWorkletModulesLoad);

0 commit comments

Comments
 (0)