function applyPatchToDoc(doc, patch, state, fromBackend) {
  const actor = getActorId(doc)
  const updated = {}
  interpretPatch(patch.diffs, doc, updated)

  if (fromBackend) {
    if (!patch.clock) throw new RangeError('patch is missing clock field')
    if (patch.clock[actor] && patch.clock[actor] > state.seq) {
      state.seq = patch.clock[actor]
    }
    state.clock = patch.clock
    state.deps  = patch.deps
    state.maxOp = Math.max(state.maxOp, patch.maxOp)
  }
  return updateRootObject(doc, updated, state)
}
Applies the changes described in `patch` to the document with root object
`doc`. The state object `state` is attached to the new root object.
`fromBackend` should be set to `true` if the patch came from the backend,
and to `false` if the patch is a transient local (optimistically applied)
change from the frontend.