add example to createDocFromSnapshot - #159
This commit is contained in:
		
							parent
							
								
									1f2f08ef7e
								
							
						
					
					
						commit
						adaa95ebb8
					
				@ -153,6 +153,14 @@ export const splitSnapshotAffectedStructs = (transaction, snapshot) => {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 | 
					 * @example
 | 
				
			||||||
 | 
					 *  const ydoc = new Y.Doc({ gc: false })
 | 
				
			||||||
 | 
					 *  ydoc.getText().insert(0, 'world!')
 | 
				
			||||||
 | 
					 *  const snapshot = Y.snapshot(ydoc)
 | 
				
			||||||
 | 
					 *  ydoc.getText().insert(0, 'hello ')
 | 
				
			||||||
 | 
					 *  const restored = Y.createDocFromSnapshot(ydoc, snapshot)
 | 
				
			||||||
 | 
					 *  assert(restored.getText().toString() === 'world!')
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 * @param {Doc} originDoc
 | 
					 * @param {Doc} originDoc
 | 
				
			||||||
 * @param {Snapshot} snapshot
 | 
					 * @param {Snapshot} snapshot
 | 
				
			||||||
 * @param {Doc} [newDoc] Optionally, you may define the Yjs document that receives the data from originDoc
 | 
					 * @param {Doc} [newDoc] Optionally, you may define the Yjs document that receives the data from originDoc
 | 
				
			||||||
@ -161,7 +169,7 @@ export const splitSnapshotAffectedStructs = (transaction, snapshot) => {
 | 
				
			|||||||
export const createDocFromSnapshot = (originDoc, snapshot, newDoc = new Doc()) => {
 | 
					export const createDocFromSnapshot = (originDoc, snapshot, newDoc = new Doc()) => {
 | 
				
			||||||
  if (originDoc.gc) {
 | 
					  if (originDoc.gc) {
 | 
				
			||||||
    // we should not try to restore a GC-ed document, because some of the restored items might have their content deleted
 | 
					    // we should not try to restore a GC-ed document, because some of the restored items might have their content deleted
 | 
				
			||||||
    throw new Error('originDoc must not be garbage collected')
 | 
					    throw new Error('Garbage-collection must be disabled in `originDoc`!')
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  const { sv, ds } = snapshot
 | 
					  const { sv, ds } = snapshot
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -2,6 +2,18 @@ import * as Y from '../src/index.js'
 | 
				
			|||||||
import * as t from 'lib0/testing'
 | 
					import * as t from 'lib0/testing'
 | 
				
			||||||
import { init } from './testHelper.js'
 | 
					import { init } from './testHelper.js'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @param {t.TestCase} tc
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					export const testBasic = tc => {
 | 
				
			||||||
 | 
					  const ydoc = new Y.Doc({ gc: false })
 | 
				
			||||||
 | 
					  ydoc.getText().insert(0, 'world!')
 | 
				
			||||||
 | 
					  const snapshot = Y.snapshot(ydoc)
 | 
				
			||||||
 | 
					  ydoc.getText().insert(0, 'hello ')
 | 
				
			||||||
 | 
					  const restored = Y.createDocFromSnapshot(ydoc, snapshot)
 | 
				
			||||||
 | 
					  t.assert(restored.getText().toString() === 'world!')
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @param {t.TestCase} tc
 | 
					 * @param {t.TestCase} tc
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user