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,8 +142,12 @@ 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')
// write current state as update
const encoder = new BinaryEncoder()
encodeStructsDS(y, encoder)
return rtop(updatesStore.put(encoder.createBuffer())).then(() => {
// read persisted state
return rtop(updatesStore.getAll()).then(updates => { return rtop(updatesStore.getAll()).then(updates => {
// apply all previous updates before deleting them
room.mutex(() => { room.mutex(() => {
y.transact(() => { y.transact(() => {
updates.forEach(update => { updates.forEach(update => {
@ -157,10 +155,6 @@ export default class IndexedDBPersistence {
}) })
}, true) }, true)
}) })
return Promise.all(room.createdStructs.map(update => {
return saveUpdate(room, update)
})).then(() => {
room.createdStructs = []
}) })
}) })
}) })