Repository navigation
fix: deep-flatten nested options so useWavesurfer does not recreate forever - #15
Conversation
…orever Object.entries(...).flat() only compared top-level values, so a new fetchParams (or similar nested plain object) identity on every render re-ran the create effect, destroyed the player, and setState-looped. Flatten nested plain objects/arrays into primitive path/value deps. Regression covers five parent re-renders with fresh fetchParams refs. Fixes katspaugh/wavesurfer.js#4226
There was a problem hiding this comment.
🟡 Changes recommended
Variable-length effect dependencies can miss nested option additions or removals.
1 open finding
What changed in this PR
Adds value-based comparison for nested WaveSurfer options to prevent unnecessary player recreation.
Changes:
- Deep-flattens nested plain objects and arrays for effect dependencies.
- Adds a regression test for stable nested option values.
| File | Description |
|---|---|
src/index.tsx |
Adds nested option dependency flattening. |
tests/index.test.js |
Tests repeated renders with equivalent fetchParams. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| const flatOptions = useMemo( | ||
| () => Object.entries(optionsWithoutPlugins).flatMap(([key, value]) => flattenOptionEntries(value, key)), |
There was a problem hiding this comment.
Good catch, thanks. Spreading the flattened entries gave the deps array a variable length, so appending or removing a nested leaf (e.g. peaks: [[0.1]] → [[0.1, 0.2]]) could go unnoticed. Fixed in 5cade88: the hook now keeps a ref-held version number that bumps only when the options stop being deep-equal. Plain objects and arrays are compared structurally, and everything else (functions, elements, class instances) by identity. The effect depends on that one number. I added regression tests for appending, removing and changing a nested leaf, alongside the existing stable re-render test. The append and remove tests fail on the previous code.
…ed options Spreading a variable-length array into effect deps breaks React's fixed-size deps contract, so appending/removing nested leaves could go undetected. Track a ref-held version that bumps only when options stop being deep-equal.
|
Thank you! |

Summary
useWavesurfer/useWavesurferInstanceflattened option deps withObject.entries(options).flat(), which only compares top-level values. Nested plain objects such asfetchParams: { credentials: 'include' }get a new identity every render when authored inline, so the create effect re-ran, destroyed the player, calledsetWavesurfer, and looped.This deep-flattens nested plain objects and arrays into path/value primitive deps so equal nested values do not recreate the player. Non-plain objects (class instances, DOM nodes, functions) stay as opaque refs.
Fixes katspaugh/wavesurfer.js#4226 (filed there because wavesurfer-react has issues disabled).
Test plan
npm test— 5/5 pass, including new regression: five re-renders with freshfetchParamsrefs must not callWaveSurfer.createagainnpm run build