save current state at the beginning in YIndexedDB

This commit is contained in:
Kevin Jahns 2018-06-03 22:39:22 +02:00
parent 9df20fac8a
commit 6f9ae0c4fc

View File

@ -79,9 +79,6 @@ function saveUpdate (room, updateBuffer) {
} }
}) })
return updatePut return updatePut
} else {
room.createdStructs.push(update)
return room.dbPromise
} }
} }
@ -109,8 +106,7 @@ export default class IndexedDBPersistence {
dbPromise: null, dbPromise: null,
channel: null, channel: null,
mutex: createMutualExclude(), mutex: createMutualExclude(),
y, y
createdStructs: [] // document updates before db created
} }
if (typeof BroadcastChannel !== 'undefined') { if (typeof BroadcastChannel !== 'undefined') {
room.channel = new BroadcastChannel('__yjs__' + room) room.channel = new BroadcastChannel('__yjs__' + room)
@ -134,9 +130,7 @@ export default class IndexedDBPersistence {
room.channel.postMessage(updateBuffer) room.channel.postMessage(updateBuffer)
} }
if (transaction.encodedStructsLen > 0) { if (transaction.encodedStructsLen > 0) {
if (room.db === null) { if (room.db !== null) {
room.createdStructs.push(updateBuffer)
} else {
saveUpdate(room, updateBuffer) saveUpdate(room, updateBuffer)
} }
} }
@ -148,19 +142,19 @@ export default class IndexedDBPersistence {
room.db = db room.db = db
const t = room.db.transaction(['updates'], 'readwrite') const t = room.db.transaction(['updates'], 'readwrite')
const updatesStore = t.objectStore('updates') const updatesStore = t.objectStore('updates')
return rtop(updatesStore.getAll()).then(updates => { // write current state as update
// apply all previous updates before deleting them const encoder = new BinaryEncoder()
room.mutex(() => { encodeStructsDS(y, encoder)
y.transact(() => { return rtop(updatesStore.put(encoder.createBuffer())).then(() => {
updates.forEach(update => { // read persisted state
decodePersisted(y, new BinaryDecoder(update)) return rtop(updatesStore.getAll()).then(updates => {
}) room.mutex(() => {
}, true) y.transact(() => {
}) updates.forEach(update => {
return Promise.all(room.createdStructs.map(update => { decodePersisted(y, new BinaryDecoder(update))
return saveUpdate(room, update) })
})).then(() => { }, true)
room.createdStructs = [] })
}) })
}) })
}) })