Skip to content

Commit 6dcf90f

Browse files
committed
add a note about loadSound vs mousePressed race when testing
1 parent 93ae8ac commit 6dcf90f

1 file changed

Lines changed: 31 additions & 1 deletion

File tree

tests/integration/about-these-tests.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,34 @@ These apply to the **web editor** suite only - the local suite sidesteps all of
7979
- There's a **race**: the editor ships the sketch code to the preview sandbox over `postMessage`; pressing Play before that channel is established means the code never arrives and no canvas renders. A short fixed settle wait before Play (`SETTLE_BEFORE_PLAY_MS`) is a brittle-but-readable workaround. (A better fix would be for the editor to disable Play until it's ready.)
8080
- p5 marks the canvas `data-hidden="true"` during setup/preload, so the test waits for the canvas to be **`attached`** (not `visible`) before deciding the sketch started.
8181

82-
Locally, the only comparable wrinkle is that p5 v2 numbers the default canvas inconsistently (`defaultCanvas1` for 2D sketches, `defaultCanvas0` for WEBGL ones), so the local spec matches the canvas by its `p5Canvas` class rather than by id.
82+
## Other testing "gotchas"
83+
p5 v2 numbers the default canvas inconsistently (`defaultCanvas1` for 2D sketches, `defaultCanvas0` for WEBGL ones), so the local spec matches the canvas by its `p5Canvas` class rather than by id.
84+
85+
As the tests click the canvas very soon after it is marked attached, there's a chance that the following pattern in examples will hit on an undefined mySound because loadSound hasn't completed before the mouse click:
86+
```js
87+
let mySound;
88+
async function setup(){
89+
createCanvas(400,400);
90+
mySound = await loadSound(someURL);
91+
}
92+
93+
function mousePressed(){
94+
//mySound could still be undefined
95+
mySound.play();
96+
}
97+
```
98+
Either check if mySound is defined in mousePressed, or register the mousePressed event handler synchronously in line with awaiting loadSound:
99+
100+
```js
101+
let mySound;
102+
async function setup(){
103+
const cnv = createCanvas(400,400);
104+
mySound = await loadSound(someURL);
105+
//only register the mouse handler afer the sound has finished loading!
106+
cnv.mousePressed(playSound);
107+
}
108+
109+
function playSound(){
110+
mySound.play();
111+
}
112+
```

0 commit comments

Comments
 (0)