uncatchable out of range errors when document changes

Hello and thanks for creating this lovely tool!

I am writing a music live coding editor where decoration changes can be dispatched every frame.
When the document gets shorter, it can happen that changes are fired that still receive the old document state, leading to errors like:

Position 281 is out of range for changeset of length 180

When that error happens, the application crashes and gets into an unusable state. It would be really helpful to be able to catch that error and just ignore it, because it is not critical if a highlight is missed.

I created a minimal reproduction here: react-codemirror-range-error - CodeSandbox the error will be thrown whenever the document content is deleted all at once.

I assume the problem is that the document changes between the call to view.dispatch and the actual execution, possibly because requestAnimationFrame is not fired synchronously.


  1. Is there a way to hook into codemirrors requestAnimationFrame loop or a way to always get the latest state of the document?
  2. If this is a just a problem with the react component, wouldn’t it still make sense to be able to catch critical errors?

Greetings, Felix

It seems I have fixed it like this… I have to test this with my actual setup to verify that this was the problem…

dispatch happens entirely synchronously. If you can reproduce an issue like this without react and react-codemirror, I’d be interested in debugging that. As it is, I don’t know what magic might be going on inside that React component.