add persistence decoder

This commit is contained in:
Kevin Jahns 2018-06-02 22:14:48 +02:00
parent 417d0ef3b5
commit a1fb1a6258
3 changed files with 57 additions and 0 deletions

2
.gitignore vendored
View File

@ -3,3 +3,5 @@ bower_components
docs
/y.*
/examples/yjs-dist.js*
.vscode
.yjsPersisted

View File

@ -0,0 +1,55 @@
import { integrateRemoteStructs } from '../MessageHandler/integrateRemoteStructs.mjs'
import { writeStructs } from '../MessageHandler/syncStep1.mjs'
import { writeDeleteSet, readDeleteSet } from '../MessageHandler/deleteSet.mjs'
const PERSIST_UPDATE = 0
/**
* Write an update to an encoder.
*
* @param {Yjs} y A Yjs instance
* @param {BinaryEncoder} updateEncoder I.e. transaction.encodedStructs
*/
export function encodeUpdate (y, updateEncoder, encoder) {
encoder.writeVarUint(PERSIST_UPDATE)
encoder.writeBinaryEncoder(updateEncoder)
}
const PERSIST_STRUCTS_DS = 1
/**
* Write the current Yjs data model to an encoder.
*
* @param {Yjs} y A Yjs instance
* @param {BinaryEncoder} encoder An encoder to write to
*/
export function encodeStructsDS (y, encoder) {
encoder.writeVarUint(PERSIST_STRUCTS_DS)
writeStructs(y, encoder, new Map())
writeDeleteSet(y, encoder)
}
/**
*Feed the Yjs instance with the persisted state
* @param {Yjs} y A Yjs instance.
* @param {BinaryDecoder} decoder A Decoder instance that holds the file content.
*/
export function decodePersisted (y, decoder) {
y.transact(() => {
while (decoder.hasContent()) {
const contentType = decoder.readVarUint()
switch (contentType) {
case PERSIST_UPDATE:
y.transact(() => {
integrateRemoteStructs(y, decoder)
})
break
case PERSIST_STRUCTS_DS:
y.transact(() => {
integrateRemoteStructs(y, decoder)
readDeleteSet(y, decoder)
})
break
}
}
})
}

View File