How to recover to the specified version

I believe the code you provided restores a snapshot, but doesn’t revert it into the same document (create new history).

By using the examples written in Is there a way to revert to specific version? - #6 by Himself65, I found that this code works:

export function revertChangesSinceSnapshot(doc: Doc, snapshot: Uint8Array) {
  const snap = Y.decodeSnapshot(snapshot);
  doc.gc = false;
  const tempdoc = Y.createDocFromSnapshot(doc, snap);

  const currentStateVector = Y.encodeStateVector(doc);
  const snapshotStateVector = Y.encodeStateVector(tempdoc);

  const changesSinceSnapshotUpdate = Y.encodeStateAsUpdate(doc, snapshotStateVector);

  const um = new Y.UndoManager(

  Y.applyUpdate(tempdoc, changesSinceSnapshotUpdate);

  const revertChangesSinceSnapshotUpdate = Y.encodeStateAsUpdate(tempdoc, currentStateVector);
  Y.applyUpdate(doc, revertChangesSinceSnapshotUpdate, {
    user: {
      id: 'revert'
  doc.gc = true;

However I am unsure if handling doc.gc like this is the correct way when the document might be getting edited at the same time. There is quite limited information documented about “gc”. Is it fine to enable it and disable it as such? Or should this be kind of a permanent setting on the document when it’s first created? Would this ever cause snapshots to break or format to be invalid?