# Recording and replaying code changes.

**URL:** <https://discuss.codemirror.net/t/recording-and-replaying-code-changes/3502>\
**Category:** v6\
**Created:** [September 4, 2021, 6:02pm UTC](https://discuss.codemirror.net/t/recording-and-replaying-code-changes/3502 "2021-09-04T18:02:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anishk](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/anishk/32/1470_2.png) [@anishk](https://discuss.codemirror.net/u/anishk)\
**Post date:** [September 4, 2021, 6:02pm UTC](https://discuss.codemirror.net/t/recording-and-replaying-code-changes/3502/1 "2021-09-04T18:02:49Z")

</div>

I am trying to figure out how can record and replay an array of `ChangeSet` (or is there a better way?).  
In my current implementation I am using a `cursor` state (ReactJS state) to track where state lies at any point of time, in `ChangeSet[]`. Based on new state I am able to determine if inverted or actual changesets are needed to be applied.  
Moving forward and backward through changes slowly works fine, but when called too frequently, this error is thrown:

```auto
Unhandled Rejection (RangeError): Applying change set to a document with the wrong length

```

I am currently using something like this:

```javascript
 useEffect(() => {
        updateEditor()
    }, [cursor])

function updateEditor() {
        if (lastCursor > cursor) {
            for (let i = lastCursor; i > cursor; i--) {
                codemirror.view.dispatch({
                    changes: editStream[i].invert()
                })
            }
        } else {

            for (let i = lastCursor + 1; i <= cursor; i++) {
                codemirror.view.dispatch({
                    changes: editStream[i]
                })
            }
        }
    }

```

Is there a better way of handling this usecase?

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)\
**Post date:** [September 5, 2021, 6:34am UTC](https://discuss.codemirror.net/t/recording-and-replaying-code-changes/3502/2 "2021-09-05T06:34:29Z")

</div>

Yes, you can use change sets to move forward and backward through history like this. The error you’re getting happens when you somehow try to apply a changeset to a document that doesn’t match the document it was created for (in length), which suggests some issue in your implementation.
