Skip to content

fix: deep-flatten nested options so useWavesurfer does not recreate forever - #15

Merged
katspaugh merged 2 commits into
katspaugh:mainfrom
SahinurDEV:fix/4226-nested-options-rerender
Oct 19, 2026
Merged

katspaugh merged 2 commits into
katspaugh:mainfrom
SahinurDEV:fix/4226-nested-options-rerender

Conversation

@SahinurDEV

Copy link
Copy Markdown
Contributor

Summary

useWavesurfer / useWavesurferInstance flattened option deps with Object.entries(options).flat(), which only compares top-level values. Nested plain objects such as fetchParams: { credentials: 'include' } get a new identity every render when authored inline, so the create effect re-ran, destroyed the player, called setWavesurfer, 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 fresh fetchParams refs must not call WaveSurfer.create again
  • npm run build

…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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread src/index.tsx Outdated
Comment on lines +69 to +70
const flatOptions = useMemo(
() => Object.entries(optionsWithoutPlugins).flatMap(([key, value]) => flattenOptionEntries(value, key)),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@katspaugh
katspaugh merged commit 8171d3d into katspaugh:main Oct 19, 2026
1 of 2 checks passed
@katspaugh

Copy link
Copy Markdown
Owner

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[@wavesurfer/react] useWavesurfer hook triggers continuous rerendering with nested options

3 participants