Fixed flickering when inserting properties from library by ensuring consistent ID generation

This commit is contained in:
Stefan Zermatten
2021-02-12 00:43:56 +02:00
parent ae373330ab
commit b116be1238
2 changed files with 14 additions and 7 deletions

View File

@@ -59,8 +59,10 @@ const insertPropertyFromLibraryNode = new ValidatedMethod({
'ancestors.id': nodeId,
removed: {$ne: true},
}).fetch();
// The root node is last in the array of nodes
nodes.push(node);
// The root node is first in the array of nodes
// It must get the first generated ID to prevent flickering
nodes = [node, ...nodes];
// re-map all the ancestors
setLineageOfDocs({
@@ -82,10 +84,10 @@ const insertPropertyFromLibraryNode = new ValidatedMethod({
});
// Insert the creature properties
let insertedDocIds = CreatureProperties.batchInsert(nodes);
CreatureProperties.batchInsert(nodes);
// get the root inserted doc
let rootId = insertedDocIds[insertedDocIds.length - 1];
let rootId = node._id;
// Tree structure changed by inserts, reorder the tree
reorderDocs({
@@ -97,7 +99,6 @@ const insertPropertyFromLibraryNode = new ValidatedMethod({
recomputeInactiveProperties(rootId);
// Inserting a creature property invalidates dependencies: full recompute
recomputeCreatureByDoc(rootCreature);
// Return the docId of the last property, the inserted root property
return rootId;
},

View File

@@ -120,13 +120,19 @@ export function setLineageOfDocs({docArray, oldParent, newAncestry}){
export function renewDocIds({docArray, collectionMap}){
// map of {oldId: newId}
let idMap = {};
// Give new ids and map the changes
// Get a random generator that's consistent on client and server
let randomSrc = DDP.randomStream('renewDocIds');
// Give new ids and map the changes as {oldId: newId}
docArray.forEach(doc => {
let oldId = doc._id;
let newId = Random.id();
let newId = randomSrc.id();
doc._id = newId;
idMap[oldId] = newId;
});
// Remap all references using the new IDs
const remapReference = ref => {
if (idMap[ref.id]){
ref.id = idMap[ref.id];