Implementing persisting action result mutations

This commit is contained in:
Thaum Rystra
2024-04-02 17:46:31 +02:00
parent 2cbfc5d099
commit 1fb1eb83c7
15 changed files with 285 additions and 144 deletions

View File

@@ -1,7 +1,6 @@
import { Meteor } from 'meteor/meteor'
import { EJSON } from 'meteor/ejson';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties';
import propertySchemasIndex from '/imports/api/properties/computedOnlyPropertySchemasIndex';
import bulkWrite, { addSetOp, addUnsetOp, newOperation } from '/imports/api/engine/shared/bulkWrite';
export default function writeAlteredProperties(computation) {
let bulkWriteOperations = [];
@@ -33,7 +32,7 @@ export default function writeAlteredProperties(computation) {
bulkWriteOperations.push(op);
}
});
bulkWriteProperties(bulkWriteOperations);
bulkWrite(bulkWriteOperations, CreatureProperties);
//if (bulkWriteOperations.length) console.log(`Wrote ${bulkWriteOperations.length} props`);
}
@@ -42,7 +41,7 @@ function addChangedKeysToOp(op, keys, original, changed) {
// and compile an operation that sets all those keys
for (let key of keys) {
if (!EJSON.equals(original[key], changed[key])) {
if (!op) op = newOperation(original._id, changed.type);
if (!op) op = newOperation(original._id);
let value = changed[key];
if (value === undefined) {
// Unset values that become undefined
@@ -55,70 +54,3 @@ function addChangedKeysToOp(op, keys, original, changed) {
}
return op;
}
function newOperation(_id, type) {
let newOp = {
updateOne: {
filter: { _id },
update: {},
}
};
if (Meteor.isClient) {
newOp.type = type;
}
return newOp;
}
function addSetOp(op, key, value) {
if (op.updateOne.update.$set) {
op.updateOne.update.$set[key] = value;
} else {
op.updateOne.update.$set = { [key]: value };
}
}
function addUnsetOp(op, key) {
if (op.updateOne.update.$unset) {
op.updateOne.update.$unset[key] = 1;
} else {
op.updateOne.update.$unset = { [key]: 1 };
}
}
// If we re-enable client-side sheet recalculation, this needs to be run on
// both client and server to preserve latency compensation. Bulkwrite breaks
// latency compensation and causes flickering
function writePropertiesSequentially(bulkWriteOps) {
bulkWriteOps.forEach(op => {
let updateOneOrMany = op.updateOne || op.updateMany;
CreatureProperties.update(updateOneOrMany.filter, updateOneOrMany.update, {
// The bulk code is bypassing validation, so do the same here
// selector: {type: op.type} // include this if bypass is off
bypassCollection2: true,
});
});
//if (bulkWriteOps.length) console.log(`Wrote ${bulkWriteOps.length} props`);
}
// This is more efficient on the database, but significantly less efficient
// in the UI because of incompatibility with latency compensation. If the
// duplicate redraws can be fixed, this is a strictly better way of processing
// writes
function bulkWriteProperties(bulkWriteOps) {
if (!bulkWriteOps.length) return;
// bulkWrite is only available on the server
if (Meteor.isServer) {
CreatureProperties.rawCollection().bulkWrite(
bulkWriteOps,
{ ordered: false },
function (e) {
if (e) {
console.error('Bulk write failed: ');
console.error(e);
}
}
);
} else {
writePropertiesSequentially(bulkWriteOps);
}
}