From 7f0e25dcba6db294804942ce78c59e34e80e0250 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Tue, 10 Dec 2019 17:18:57 +0100 Subject: [PATCH] permanent user store writes updates in separate transaction --- src/structs/Item.js | 2 +- src/utils/PermanentUserData.js | 50 ++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/structs/Item.js b/src/structs/Item.js index 10e0e841..23287ec2 100644 --- a/src/structs/Item.js +++ b/src/structs/Item.js @@ -55,7 +55,7 @@ export const followRedone = (store, id) => { item = getItem(store, nextID) diff = nextID.clock - item.id.clock nextID = item.redone - } while (nextID !== null) + } while (nextID !== null && item instanceof Item) return { item, diff } diff --git a/src/utils/PermanentUserData.js b/src/utils/PermanentUserData.js index 289a3755..0b2c072d 100644 --- a/src/utils/PermanentUserData.js +++ b/src/utils/PermanentUserData.js @@ -83,33 +83,37 @@ export class PermanentUserData { } user.get('ids').push([clientid]) users.observe(event => { - const userOverwrite = users.get(userDescription) - if (userOverwrite !== user) { - // user was overwritten, port all data over to the next user object - // @todo Experiment with Y.Sets here - user = userOverwrite - // @todo iterate over old type - this.clients.forEach((_userDescription, clientid) => { - if (userDescription === _userDescription) { - user.get('ids').push([clientid]) + setTimeout(() => { + const userOverwrite = users.get(userDescription) + if (userOverwrite !== user) { + // user was overwritten, port all data over to the next user object + // @todo Experiment with Y.Sets here + user = userOverwrite + // @todo iterate over old type + this.clients.forEach((_userDescription, clientid) => { + if (userDescription === _userDescription) { + user.get('ids').push([clientid]) + } + }) + const encoder = encoding.createEncoder() + const ds = this.dss.get(userDescription) + if (ds) { + writeDeleteSet(encoder, ds) + user.get('ds').push([encoding.toUint8Array(encoder)]) } - }) - const encoder = encoding.createEncoder() - const ds = this.dss.get(userDescription) - if (ds) { - writeDeleteSet(encoder, ds) - user.get('ds').push([encoding.toUint8Array(encoder)]) } - } + }, 0) }) doc.on('afterTransaction', /** @param {Transaction} transaction */ transaction => { - const yds = user.get('ds') - const ds = transaction.deleteSet - if (transaction.local && ds.clients.size > 0) { - const encoder = encoding.createEncoder() - writeDeleteSet(encoder, ds) - yds.push([encoding.toUint8Array(encoder)]) - } + setTimeout(() => { + const yds = user.get('ds') + const ds = transaction.deleteSet + if (transaction.local && ds.clients.size > 0) { + const encoder = encoding.createEncoder() + writeDeleteSet(encoder, ds) + yds.push([encoding.toUint8Array(encoder)]) + } + }) }) } /**